From 50a6c9d39c45975bf3d623da2234b190e2a68f42 Mon Sep 17 00:00:00 2001 From: Alexander-D-Karpov Date: Sat, 8 Oct 2022 00:48:53 +0300 Subject: [PATCH] added celery, channels, swagger, created base user, added auth endpoints --- app/conf/api.py | 21 +++++++ app/conf/celery.py | 17 ++++++ app/conf/settings/base.py | 5 +- app/conf/urls.py | 57 ++++++++++++------- app/events/__init__.py | 0 app/events/admin.py | 3 + app/events/apps.py | 6 ++ app/events/migrations/__init__.py | 0 app/events/models.py | 3 + app/events/tests.py | 3 + app/events/views.py | 3 + app/marketplace/__init__.py | 0 app/marketplace/admin.py | 3 + app/marketplace/apps.py | 6 ++ app/marketplace/migrations/__init__.py | 0 app/marketplace/models.py | 3 + app/marketplace/tests.py | 3 + app/marketplace/views.py | 3 + app/requirements/base.txt | 13 ++++- app/users/admin.py | 3 + .../migrations/0002_user_salary_user_type.py | 23 ++++++++ app/users/models.py | 17 +++++- 22 files changed, 165 insertions(+), 27 deletions(-) create mode 100644 app/conf/api.py create mode 100644 app/conf/celery.py create mode 100644 app/events/__init__.py create mode 100644 app/events/admin.py create mode 100644 app/events/apps.py create mode 100644 app/events/migrations/__init__.py create mode 100644 app/events/models.py create mode 100644 app/events/tests.py create mode 100644 app/events/views.py create mode 100644 app/marketplace/__init__.py create mode 100644 app/marketplace/admin.py create mode 100644 app/marketplace/apps.py create mode 100644 app/marketplace/migrations/__init__.py create mode 100644 app/marketplace/models.py create mode 100644 app/marketplace/tests.py create mode 100644 app/marketplace/views.py create mode 100644 app/users/migrations/0002_user_salary_user_type.py diff --git a/app/conf/api.py b/app/conf/api.py new file mode 100644 index 0000000..009c580 --- /dev/null +++ b/app/conf/api.py @@ -0,0 +1,21 @@ +from django.urls import path, include +from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView + +urlpatterns = [ + path( + "auth/", + include( + [ + path("token/", TokenObtainPairView.as_view(), name="token_obtain_pair"), + path("refresh/", TokenRefreshView.as_view(), name="token_refresh"), + ] + ), + ), + path( + "user/", + include( + [ + ] + ), + ), +] diff --git a/app/conf/celery.py b/app/conf/celery.py new file mode 100644 index 0000000..ef00351 --- /dev/null +++ b/app/conf/celery.py @@ -0,0 +1,17 @@ +import os + +from celery import Celery + +# Set the default Django settings module for the 'celery' program. +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "conf.settings") + +app = Celery("conf") + +# Using a string here means the worker doesn't have to serialize +# the configuration object to child processes. +# - namespace='CELERY' means all celery-related configuration keys +# should have a `CELERY_` prefix. +app.config_from_object("django.conf:settings", namespace="CELERY") + +# Load task modules from all registered Django apps. +app.autodiscover_tasks() diff --git a/app/conf/settings/base.py b/app/conf/settings/base.py index b813523..8340bd4 100644 --- a/app/conf/settings/base.py +++ b/app/conf/settings/base.py @@ -60,10 +60,7 @@ DJANGO_APPS = [ "django.contrib.admin", "django.forms", ] -THIRD_PARTY_APPS = [ - "rest_framework", - "corsheaders", -] +THIRD_PARTY_APPS = ["rest_framework", "corsheaders", "django_celery_beat", "drf_yasg"] LOCAL_APPS = ["users"] # https://docs.djangoproject.com/en/dev/ref/settings/#installed-apps diff --git a/app/conf/urls.py b/app/conf/urls.py index f7faa20..049fdcc 100644 --- a/app/conf/urls.py +++ b/app/conf/urls.py @@ -1,21 +1,40 @@ -"""conf URL Configuration - -The `urlpatterns` list routes URLs to views. For more information please see: - https://docs.djangoproject.com/en/4.1/topics/http/urls/ -Examples: -Function views - 1. Add an import: from my_app import views - 2. Add a URL to urlpatterns: path('', views.home, name='home') -Class-based views - 1. Add an import: from other_app.views import Home - 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') -Including another URLconf - 1. Import the include() function: from django.urls import include, path - 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) -""" +from django.conf import settings +from django.conf.urls.static import static from django.contrib import admin -from django.urls import path +from django.urls import path, include, re_path +from drf_yasg import openapi +from drf_yasg.views import get_schema_view +from rest_framework.permissions import AllowAny -urlpatterns = [ - path("admin/", admin.site.urls), -] +schema_view = get_schema_view( + openapi.Info( + title="API", + default_version="v1", + description="description", + terms_of_service="https://akarpov.ru/about", + contact=openapi.Contact(email="alexander.d.karpov@gmail.com"), + license=openapi.License(name="License"), + ), + validators=["ssv"], + public=True, + permission_classes=[AllowAny], +) + +urlpatterns = ( + [ + path("admin/", admin.site.urls), + path("api/", include("conf.api")), + re_path( + r"^swagger(?P\.json|\.yaml)$", + schema_view.without_ui(cache_timeout=0), + name="schema-json", + ), + re_path( + r"^swagger/$", + schema_view.with_ui("swagger", cache_timeout=0), + name="schema-swagger-ui", + ), + ] + + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) +) diff --git a/app/events/__init__.py b/app/events/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/events/admin.py b/app/events/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/app/events/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/app/events/apps.py b/app/events/apps.py new file mode 100644 index 0000000..20f48f2 --- /dev/null +++ b/app/events/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class EventsConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'events' diff --git a/app/events/migrations/__init__.py b/app/events/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/events/models.py b/app/events/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/app/events/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/app/events/tests.py b/app/events/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/app/events/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/app/events/views.py b/app/events/views.py new file mode 100644 index 0000000..91ea44a --- /dev/null +++ b/app/events/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here. diff --git a/app/marketplace/__init__.py b/app/marketplace/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/marketplace/admin.py b/app/marketplace/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/app/marketplace/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/app/marketplace/apps.py b/app/marketplace/apps.py new file mode 100644 index 0000000..c753ff1 --- /dev/null +++ b/app/marketplace/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class MarketplaceConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'marketplace' diff --git a/app/marketplace/migrations/__init__.py b/app/marketplace/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/marketplace/models.py b/app/marketplace/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/app/marketplace/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/app/marketplace/tests.py b/app/marketplace/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/app/marketplace/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/app/marketplace/views.py b/app/marketplace/views.py new file mode 100644 index 0000000..91ea44a --- /dev/null +++ b/app/marketplace/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here. diff --git a/app/requirements/base.txt b/app/requirements/base.txt index ece643a..a6f2396 100644 --- a/app/requirements/base.txt +++ b/app/requirements/base.txt @@ -1,9 +1,16 @@ -celery==5.2.7 djangorestframework==3.14.0 djangorestframework-simplejwt==5.2.1 -Django==4.1.2 +Django==4.0.8 django-cors-headers==3.13.0 django-environ==0.9.0 +"drf-yasg[validation]" + redis==4.3.4 -psycopg2-binary==2.9.4 \ No newline at end of file +psycopg2-binary==2.9.4 + +channels==3.0.5 +channels-redis==4.0.0 + +celery==5.2.7 +django-celery-beat==2.3.0 \ No newline at end of file diff --git a/app/users/admin.py b/app/users/admin.py index 8c38f3f..9a8de1d 100644 --- a/app/users/admin.py +++ b/app/users/admin.py @@ -1,3 +1,6 @@ from django.contrib import admin # Register your models here. +from users.models import User + +admin.site.register(User) diff --git a/app/users/migrations/0002_user_salary_user_type.py b/app/users/migrations/0002_user_salary_user_type.py new file mode 100644 index 0000000..10a3fb3 --- /dev/null +++ b/app/users/migrations/0002_user_salary_user_type.py @@ -0,0 +1,23 @@ +# Generated by Django 4.0.8 on 2022-10-07 21:20 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('users', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='user', + name='salary', + field=models.IntegerField(default=0), + ), + migrations.AddField( + model_name='user', + name='type', + field=models.CharField(choices=[('WORKER', 'worker'), ('HR', 'human resources'), ('ADMIN', 'administrator')], default='WORKER', max_length=6), + ), + ] diff --git a/app/users/models.py b/app/users/models.py index a36e606..f4d22ae 100644 --- a/app/users/models.py +++ b/app/users/models.py @@ -3,7 +3,10 @@ from django.db import models class User(AbstractUser): - """Base user model, to store all user info""" + class WorkerType(models.TextChoices): + WORKER = "WORKER", "worker" + HR = "HR", "human resources" + ADMIN = "ADMIN", "administrator" first_name = None last_name = None @@ -12,9 +15,21 @@ class User(AbstractUser): # image_cropped = models.ImageField(upload_to="cropped/", blank=True) about = models.TextField(blank=True) + type = models.CharField( + max_length=6, choices=WorkerType.choices, default=WorkerType.WORKER + ) + salary = models.IntegerField(default=0) def __str__(self): return self.username + def save(self, *args, **kwargs): + self.set_password(self.password) + super(AbstractUser, self).save(*args, **kwargs) + + @property + def can_create_events(self): + return self.type in [self.WorkerType.HR, self.WorkerType.ADMIN] + class Meta: ordering = ["-id"]