create others schemas in one endpoint -> users

develop
TBS093A 2020-06-19 12:32:04 +02:00
parent 005d5cce95
commit 0b25a73127
92 changed files with 167 additions and 206 deletions

View File

@ -1,26 +0,0 @@
# Generated by Django 3.0.7 on 2020-06-17 12:26
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
('account', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='Album',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=255)),
('description', models.CharField(max_length=255)),
('image', models.TextField()),
('url_code', models.CharField(max_length=255)),
('user', models.ManyToManyField(to='account.Account')),
],
),
]

View File

@ -1,38 +0,0 @@
# Generated by Django 3.0.7 on 2020-06-17 12:26
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
('account', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='UserComment',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('text', models.CharField(max_length=255)),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='account.Account')),
],
options={
'abstract': False,
},
),
migrations.CreateModel(
name='GuestComment',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('text', models.CharField(max_length=255)),
('guest', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='account.Guest')),
],
options={
'abstract': False,
},
),
]

2
migrate.sh 100755
View File

@ -0,0 +1,2 @@
python manage.py makemigrations
python manage.py migrate

View File

@ -1,7 +1,6 @@
pip install django pip install django
pip install djangorestframework pip install djangorestframework
pip install django-filter pip install django-filter
pip install django-rest-swagger
pip install django-enumfield pip install django-enumfield
pip install pyjwt pip install pyjwt
pip install markdown pip install markdown

View File

@ -1,25 +0,0 @@
# Generated by Django 3.0.7 on 2020-06-17 12:26
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
('song', '0001_initial'),
('account', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='Playlist',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=255)),
('song', models.ManyToManyField(to='song.Song')),
('user', models.ManyToManyField(to='account.Account')),
],
),
]

View File

@ -22,27 +22,42 @@ class AbstractUser(models.Model):
class Account(User, AbstractUser): class Account(User, AbstractUser):
def register(username, email, password): @staticmethod
if Account.objects.get(username = username) is None and Account.objects.get(email = email) is None: def register(userDict) -> object:
Account.objects.create_user(username, email, password) account = Account.objects.create_user(
return Response(f'Account created: ') userDict['username'],
userDict['email'],
userDict['password'],
)
account.ip = userDict['ip']
account.city = userDict['city'],
account.country = userDict['country']
account.save()
return account
def login(username, password) -> dict: def login(self, username, password) -> dict:
tryLogin = authenticate(username = username, password = password) tryLogin = authenticate(username = username, password = password)
if tryLogin is not None: if tryLogin is not None:
user = Account.objects.get(username = username) user = Account.objects.get(username = username)
token = Token.objects.create(user = user) token = Token.objects.create(user = user)
return token.__dict__ return token.__dict__
else: else:
return { error: 'login failed'} return { 'error': 'login failed'}
def logout(): def logout(self):
pass pass
def update(self, userDict): def update(self, userDict):
if 'password' in userDict:
password = userDict.pop('password')
self.set_password(password)
self.fromDict(userDict) self.fromDict(userDict)
self.save self.save()
def set_password(self, raw_password):
return super().set_password(raw_password)
class Guest(AbstractUser): class Guest(AbstractUser):
pass pass

View File

@ -0,0 +1,43 @@
from .models import Account, Guest
from rest_framework import serializers
from django.core.paginator import Paginator
from django.http import JsonResponse
class AccountGetSerializer(serializers.ModelSerializer):
id = serializers.IntegerField(read_only = True)
username = serializers.CharField(max_length = 100)
email = serializers.EmailField()
ip = serializers.CharField(max_length = 12)
city = serializers.CharField(max_length = 255)
country = serializers.CharField(max_length = 255)
class Meta:
model = Account
fields = ['id', 'username', 'email', 'ip', 'city', 'country']
class AccountSerializer(AccountGetSerializer):
password = serializers.CharField(max_length = 100)
def create(self, validated_data):
return Account.register(validated_data)
def update(self, instance, validated_data):
return instance.update(instance, **validated_data)
class Meta:
model = Account
fields = ['id', 'username', 'password', 'email', 'ip', 'city', 'country']
class GuestSerializer(serializers.HyperlinkedModelSerializer):
id = serializers.IntegerField(read_only = True)
ip = serializers.CharField(max_length = 12)
city = serializers.CharField(max_length = 255)
country = serializers.CharField(max_length = 255)
class Meta:
model = Guest
fields = ['id', 'ip', 'city', 'country']

View File

@ -0,0 +1,46 @@
from rest_framework import viewsets
from rest_framework.views import APIView
from rest_framework.decorators import action
from rest_framework import mixins
from rest_framework.response import Response
from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema
from django.core.paginator import Paginator
from django.http import JsonResponse
from django.shortcuts import get_object_or_404
from .models import Account, Guest
from .serializers import AccountSerializer, GuestSerializer, AccountGetSerializer
class AccountViewSet(viewsets.ModelViewSet):
queryset = Account.objects.all()
serializer_class = AccountSerializer
@swagger_auto_schema(responses={ 200: AccountGetSerializer })
def retrieve(self, request, pk=None):
print(pk)
account = get_object_or_404(self.queryset, pk=pk)
serializer = AccountGetSerializer(account)
return Response(serializer.data)
@swagger_auto_schema(responses={ 200: AccountGetSerializer })
def list(self, request, *args, **kwargs):
serializer = AccountGetSerializer(self.queryset, many=True)
return Response(serializer.data)
class GuestViewSet(viewsets.ModelViewSet):
queryset = Guest.objects.all()
serializer_class = GuestSerializer
# @swagger_auto_schema(request_body = AccountSerializer, responses = {
# 200: openapi.Response("OK", schema = AccountSerializer),
# 400: openapi.Response("Empty")
# })

View File

@ -1,4 +1,4 @@
# Generated by Django 3.0.7 on 2020-06-17 12:26 # Generated by Django 3.0.7 on 2020-06-18 17:25
from django.conf import settings from django.conf import settings
import django.contrib.auth.models import django.contrib.auth.models

View File

@ -38,15 +38,9 @@ INSTALLED_APPS = [
'django.contrib.messages', 'django.contrib.messages',
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'rest_framework', 'rest_framework',
'rest_framework_swagger', 'drf_yasg',
'rest_framework.authtoken', 'rest_framework.authtoken',
'account',
'album',
'comment',
'playlist',
'portfolio', 'portfolio',
'rating',
'song'
] ]
MIDDLEWARE = [ MIDDLEWARE = [
@ -140,3 +134,15 @@ USE_TZ = True
# https://docs.djangoproject.com/en/3.0/howto/static-files/ # https://docs.djangoproject.com/en/3.0/howto/static-files/
STATIC_URL = '/static/' STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
SWAGGER_SETTINGS = {
'JSON_EDITOR': True,
'SECURITY_DEFINITIONS': {
'Bearer': {
'type': 'apiKey',
'name': 'Authorization',
'in': 'header'
}
}
}

View File

@ -13,13 +13,45 @@ Including another URLconf
1. Import the include() function: from django.urls import include, path 1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
""" """
from django.conf.urls import url, include
from django.contrib import admin from django.contrib import admin
from django.urls import path from django.urls import path, re_path, include
from django.conf.urls.static import static
from drf_yasg import openapi
from drf_yasg.views import get_schema_view
from rest_framework import routers, permissions
from rest_framework_swagger.views import get_swagger_view from rest_framework_swagger.views import get_swagger_view
from portfolio import settings
from .account import views
schema_view = get_schema_view(
openapi.Info(
title="API",
default_version='0.1',
description="API",
contact=openapi.Contact(email="zukkamil.44@gmail.com"),
license=openapi.License(name="All rights reserved"),
),
public=True,
permission_classes=(permissions.AllowAny,),
)
router = routers.DefaultRouter()
router.register(r'users', views.AccountViewSet, basename='user')
router.register(r'guests', views.GuestViewSet)
urlpatterns = [ urlpatterns = [
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
url(r'^$', get_swagger_view(title='API Endpoints')), path('', include(router.urls)),
url(r'^api-auth/', include('rest_framework.urls')) path('api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]
if settings.DEBUG:
urlpatterns = [
path('admin/', admin.site.urls),
path('', include(router.urls)),
path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
path('swagger/', schema_view.with_ui('swagger', cache_timeout=0))
] ]

View File

@ -1,57 +0,0 @@
# Generated by Django 3.0.7 on 2020-06-17 12:26
from django.db import migrations, models
import django.db.models.deletion
import django_enumfield.db.fields
import rating.models
class Migration(migrations.Migration):
initial = True
dependencies = [
('comment', '0001_initial'),
('song', '0001_initial'),
('album', '0001_initial'),
('account', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='SongRating',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('value', django_enumfield.db.fields.EnumField(enum=rating.models.RatingValue)),
('song', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='song.Song')),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='account.Account')),
],
options={
'abstract': False,
},
),
migrations.CreateModel(
name='CommentRating',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('value', django_enumfield.db.fields.EnumField(enum=rating.models.RatingValue)),
('comment', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='comment.UserComment')),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='account.Account')),
],
options={
'abstract': False,
},
),
migrations.CreateModel(
name='AlbumRating',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('value', django_enumfield.db.fields.EnumField(enum=rating.models.RatingValue)),
('album', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='album.Album')),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='account.Account')),
],
options={
'abstract': False,
},
),
]

2
run.sh
View File

@ -1,3 +1 @@
python manage.py makemigrations
python manage.py migrate
python manage.py runserver 9090 python manage.py runserver 9090

View File

@ -1,31 +0,0 @@
# Generated by Django 3.0.7 on 2020-06-17 12:26
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
('album', '0001_initial'),
('account', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='Song',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=255)),
('description', models.CharField(max_length=1000)),
('text', models.TextField()),
('image', models.TextField()),
('audio', models.TextField()),
('url_code', models.CharField(max_length=255)),
('album', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='album.Album')),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='account.Account')),
],
),
]

View File

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