Skip to content

Commit

Permalink
Merge pull request #4 from MrHamedi/chat_models
Browse files Browse the repository at this point in the history
Chat models
  • Loading branch information
MrHamedi authored Nov 23, 2023
2 parents 07580d2 + ad58ce1 commit cd7f0b8
Show file tree
Hide file tree
Showing 16 changed files with 224 additions and 18 deletions.
17 changes: 17 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"editor.defaultFormatter": null,
"editor.formatOnSave": true,
"python.formatting.provider": "black",
"python.linting.enabled": true,
"python.linting.lintOnSave": true,
"python.linting.flake8Enabled": true,
"python.linting.flake8Args": [
"--max-line-length",
"119",
],
"[python]": {
"editor.codeActionsOnSave": {
"source.organizeImports": true
}
}
}
16 changes: 7 additions & 9 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ services:
- DB_HOST=${DB_HOST}
- DB_PASSWORD=${DB_PASSWORD}
- DB_PORT=${DB_PORT}
command: >
sh -c "python manage.py makemigrations &&
python manage.py migrate &&
gunicorn gap.wsgi:application --bind 0.0.0.0:8000 --reload"
command: >
sh -c "python3 manage.py call_db && python manage.py makemigrations && python manage.py migrate && gunicorn gap.wsgi:application --bind 0.0.0.0:8000 --reload"
depends_on:
- db
tty: true
Expand All @@ -27,9 +25,9 @@ services:
volumes:
- postgres_data:/var/lib/postgresql/data/
environment:
- POSTGRES_DB=${DB_NAME}
- POSTGRES_USER=${DB_USER}
- POSTGRES_PASSWORD=${DB_PASSWORD}
- POSTGRES_DB=${DB_NAME}
- POSTGRES_USER=${DB_USER}
- POSTGRES_PASSWORD=${DB_PASSWORD}

volumes:
postgres_data:
postgres_data:
3 changes: 2 additions & 1 deletion requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ pytz==2023.3.post1
sqlparse==0.4.4
typing_extensions==4.8.0
gunicorn==21.2.0
psycopg2-binary==2.9.9
psycopg2-binary==2.9.9
drf-spectacular==0.26.5
Empty file added src/core/management/__init__.py
Empty file.
Empty file.
21 changes: 21 additions & 0 deletions src/core/management/commands/call_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import time

from django.db import connections
from django.db.utils import OperationalError
from django.core.management.base import BaseCommand


class Command(BaseCommand):
"""
Wait until the database is available
"""
def handle(self, *args ,**options):
db_con=None
while not db_con:
try:
db_con=connections["default"]
except OperationalError:
self.stdout.write("waiting for database connection...")
time.sleep(1)

self.stdout.write("database available!")
29 changes: 21 additions & 8 deletions src/gap/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,20 @@
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.environ.get("SECRET_KEY")



# Application definition

INSTALLED_APPS = [
#django apps
# django apps
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',

#cutome apps
"core.apps.CoreConfig"
# 3rd party
'drf_spectacular',
# cutome apps
"core.apps.CoreConfig",
"server.apps.ServerConfig",
]

MIDDLEWARE = [
Expand Down Expand Up @@ -125,4 +124,18 @@

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

AUTH_USER_MODEL="core.CustomeUser"
AUTH_USER_MODEL = "core.CustomeUser"

REST_FRAMEWORK = {
'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema',
"DEFAULT_AUTHENTICATION_CLASSES": [
"rest_framework.authentication.SessionAuthentication",
],
}

SPECTACULAR_SETTINGS = {
'TITLE': 'Gap backend API',
'DESCRIPTION': 'A chat application',
'VERSION': '1.0.0',
'SERVE_INCLUDE_SCHEMA': False,
}
4 changes: 4 additions & 0 deletions src/gap/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
"""
from django.contrib import admin
from django.urls import path
from drf_spectacular.views import SpectacularAPIView, SpectacularSwaggerView


urlpatterns = [
path('admin/', admin.site.urls),
path('api/schema/', SpectacularAPIView.as_view(), name='schema'),
path('api/schema/ui/', SpectacularSwaggerView.as_view(url_name='schema'), name='swagger-ui'),
]
Empty file added src/server/__init__.py
Empty file.
7 changes: 7 additions & 0 deletions src/server/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.contrib import admin

from .models import Server, Channel, Category
# Register your models here.
admin.register(Channel)
admin.register(Category)
admin.register(Server)
6 changes: 6 additions & 0 deletions src/server/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class ServerConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'server'
60 changes: 60 additions & 0 deletions src/server/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Generated by Django 4.2.7 on 2023-11-23 19:59

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

initial = True

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name='Category',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=300, verbose_name='عنوان')),
('description', models.TextField(verbose_name='توضیحات')),
],
options={
'verbose_name': 'نوع',
'verbose_name_plural': 'انواع',
'ordering': ('name',),
},
),
migrations.CreateModel(
name='Server',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=300, verbose_name='عنوان')),
('description', models.TextField(verbose_name='توضیحات')),
('category', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='server.category', verbose_name='نوع')),
('owner', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL, verbose_name='مالک')),
],
options={
'verbose_name': 'سرور',
'verbose_name_plural': 'سرور ها',
'ordering': ('name',),
},
),
migrations.CreateModel(
name='Channel',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=300, verbose_name='عنوان')),
('topic', models.CharField(max_length=300, verbose_name='موضوع')),
('owner', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='channel_owner', to=settings.AUTH_USER_MODEL, verbose_name='مالک')),
('server', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='channel_server', to='server.server', verbose_name='سرور')),
],
options={
'verbose_name': 'کانال',
'verbose_name_plural': 'کانال ها',
'ordering': ('name',),
},
),
]
Empty file.
52 changes: 52 additions & 0 deletions src/server/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from django.db import models
from django.contrib.auth import get_user_model


class Category(models.Model):
name = models.CharField(max_length=300, verbose_name="عنوان")
description = models.TextField(verbose_name="توضیحات")

class Meta:
ordering = ('name',)
verbose_name = "نوع"
verbose_name_plural = "انواع"

def __str__(self):
return self.name


class Server(models.Model):
name = models.CharField(max_length=300, verbose_name="عنوان")
owner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE,
verbose_name="مالک")
description = models.TextField(verbose_name="توضیحات")
category = models.ForeignKey(Category, on_delete=models.CASCADE,
verbose_name="نوع")

class Meta:
ordering = ('name',)
verbose_name = "سرور"
verbose_name_plural = "سرور ها"

def __str__(self):
return self.name


class Channel(models.Model):
name = models.CharField(max_length=300, verbose_name="عنوان")
topic = models.CharField(max_length=300, verbose_name="موضوع")
owner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE,
related_name="channel_owner", verbose_name="مالک")
server = models.ForeignKey(Server, on_delete=models.CASCADE, verbose_name="سرور",
related_name="channel_server")

class Meta:
ordering = ("name",)
verbose_name = "کانال"
verbose_name_plural = "کانال ها"

def save(self, *args, **kwargs):
self.name = self.name.lower()

def __str__(self):
return self.name
24 changes: 24 additions & 0 deletions src/server/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from django.test import TestCase
from django.contrib.auth import get_user_model

from .models import Server, Channel, Category


def user_creator(username="username", email="test@mail.com", password="test_password"):
return get_user_model().objects.create_user(username=username, email=email, password=password)


class ServerModelTests(TestCase):

def setUp(self):
self.owner = user_creator()
self.category = Category.objects.create(name="نوع ۱", description="نوع اول")
self.server = Server.objects.create(name="سرور ۱",
owner=self.owner,
description="سرور ۱",
category=self.category
)

def test_Server_created(self):
self.assertEqual(Server.objects.count(), 1)

3 changes: 3 additions & 0 deletions src/server/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.shortcuts import render

# Create your views here.

0 comments on commit cd7f0b8

Please sign in to comment.