added celery, channels, swagger, created base user, added auth endpoints

This commit is contained in:
Alexander Karpov 2022-10-08 00:48:53 +03:00
parent 33562ddc1b
commit 50a6c9d39c
22 changed files with 165 additions and 27 deletions

21
app/conf/api.py Normal file
View File

@ -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(
[
]
),
),
]

17
app/conf/celery.py Normal file
View File

@ -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()

View File

@ -60,10 +60,7 @@ DJANGO_APPS = [
"django.contrib.admin", "django.contrib.admin",
"django.forms", "django.forms",
] ]
THIRD_PARTY_APPS = [ THIRD_PARTY_APPS = ["rest_framework", "corsheaders", "django_celery_beat", "drf_yasg"]
"rest_framework",
"corsheaders",
]
LOCAL_APPS = ["users"] LOCAL_APPS = ["users"]
# https://docs.djangoproject.com/en/dev/ref/settings/#installed-apps # https://docs.djangoproject.com/en/dev/ref/settings/#installed-apps

View File

@ -1,21 +1,40 @@
"""conf URL Configuration from django.conf import settings
from django.conf.urls.static import static
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.contrib import admin 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 = [ schema_view = get_schema_view(
path("admin/", admin.site.urls), 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<format>\.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)
)

0
app/events/__init__.py Normal file
View File

3
app/events/admin.py Normal file
View File

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

6
app/events/apps.py Normal file
View File

@ -0,0 +1,6 @@
from django.apps import AppConfig
class EventsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'events'

View File

3
app/events/models.py Normal file
View File

@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

3
app/events/tests.py Normal file
View File

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

3
app/events/views.py Normal file
View File

@ -0,0 +1,3 @@
from django.shortcuts import render
# Create your views here.

View File

3
app/marketplace/admin.py Normal file
View File

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

6
app/marketplace/apps.py Normal file
View File

@ -0,0 +1,6 @@
from django.apps import AppConfig
class MarketplaceConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'marketplace'

View File

View File

@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

3
app/marketplace/tests.py Normal file
View File

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

3
app/marketplace/views.py Normal file
View File

@ -0,0 +1,3 @@
from django.shortcuts import render
# Create your views here.

View File

@ -1,9 +1,16 @@
celery==5.2.7
djangorestframework==3.14.0 djangorestframework==3.14.0
djangorestframework-simplejwt==5.2.1 djangorestframework-simplejwt==5.2.1
Django==4.1.2 Django==4.0.8
django-cors-headers==3.13.0 django-cors-headers==3.13.0
django-environ==0.9.0 django-environ==0.9.0
"drf-yasg[validation]"
redis==4.3.4 redis==4.3.4
psycopg2-binary==2.9.4 psycopg2-binary==2.9.4
channels==3.0.5
channels-redis==4.0.0
celery==5.2.7
django-celery-beat==2.3.0

View File

@ -1,3 +1,6 @@
from django.contrib import admin from django.contrib import admin
# Register your models here. # Register your models here.
from users.models import User
admin.site.register(User)

View File

@ -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),
),
]

View File

@ -3,7 +3,10 @@ from django.db import models
class User(AbstractUser): 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 first_name = None
last_name = None last_name = None
@ -12,9 +15,21 @@ class User(AbstractUser):
# image_cropped = models.ImageField(upload_to="cropped/", blank=True) # image_cropped = models.ImageField(upload_to="cropped/", blank=True)
about = models.TextField(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): def __str__(self):
return self.username 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: class Meta:
ordering = ["-id"] ordering = ["-id"]