<file_sep>from django.db import models
from django.contrib.auth.models import User


TYPE_1=1
TYPE_2=2
TYPE_3=3
TYPE_4=4
TYPES = (
    (TYPE_1, 'Bullet'),
    (TYPE_2, 'Food'),
    (TYPE_3, 'Travel'),
    (TYPE_4, 'Sport'),
)



# Create your models here.
class BookJournalBase(models.Model):
    name=models.CharField(max_length=255, verbose_name="название")
    price=models.IntegerField(default=0, verbose_name='Стоимость')
    description=models.TextField(null=True, blank=True, verbose_name="Описание")
    created_at=models.DateTimeField(auto_now_add=True,verbose_name="Добавлен")

    class Meta:
        abstract = True

class Book(BookJournalBase):
    num_pages=models.IntegerField(default=0, verbose_name='Количество страниц')
    genre=models.CharField(max_length=255, verbose_name="Жанр")

class Journal(BookJournalBase):
    type=models.SmallIntegerField(choices=TYPES, default=TYPE_1)
    publisher=models.ForeignKey(User,on_delete=models.CASCADE,verbose_name="Владелец")<file_sep>from django.db import models

from django.contrib.auth.models import User
# Create your models here.
USER_1=1
USER_2=2

USER_ROLES=(
    (USER_1, 'SuperAdmin'),
    (USER_2, 'Guest'),

)



class Profile(models.Model):
    user = models.OneToOneField(User,on_delete= models.CASCADE)
    first_name = models.CharField( max_length=30, blank=True)
    last_name = models.CharField( max_length=30, blank=True)
    date_joined = models.DateTimeField( auto_now_add=True)
    role = models.SmallIntegerField(choices=USER_ROLES, default=USER_2)



<file_sep>from django.urls import path
from . import views
from rest_framework import routers

router = routers.SimpleRouter()
router.register('books', views.BookViewSet, basename='book')
router.register('journals', views.JournalViewSet, basename='book')
app_name = "book"
urlpatterns = [

]
urlpatterns += router.urls<file_sep>from django.shortcuts import render
from .models import *
from rest_framework import generics, mixins, viewsets
from .serializers import BookSerializer, JournalSerializer
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.decorators import action


class BookViewSet(mixins.ListModelMixin,
                  mixins.CreateModelMixin,
                  mixins.UpdateModelMixin,
                  mixins.RetrieveModelMixin,
                  viewsets.ViewSet):
    permission_classes = (IsAuthenticated,)
    # queryset = Book.objects.all()
    serializer_class = BookSerializer

    def get_queryset(self):
        return Book.objects.all()

    def list(self, request):
        serializer = BookSerializer(self.get_queryset(), many=True)
        return Response(serializer.data)

class JournalViewSet(mixins.ListModelMixin,
                  mixins.CreateModelMixin,
                  mixins.UpdateModelMixin,
                  mixins.RetrieveModelMixin,
                  viewsets.ViewSet):
    permission_classes = (IsAuthenticated,)
    # queryset = Book.objects.all()
    serializer_class = JournalSerializer

    def get_queryset(self):
        return Journal.objects.all()

    def list(self, request):
        serializer = JournalSerializer(self.get_queryset(), many=True)
        return Response(serializer.data)<file_sep># Generated by Django 3.1.7 on 2021-03-20 05:46

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='Book',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.CharField(max_length=255, verbose_name='название')),
                ('price', models.IntegerField(default=0, verbose_name='Стоимость')),
                ('description', models.TextField(blank=True, null=True, verbose_name='Описание')),
                ('created_at', models.DateTimeField(auto_now_add=True, verbose_name='Добавлен')),
                ('num_pages', models.IntegerField(default=0, verbose_name='Количество страниц')),
                ('genre', models.CharField(max_length=255, verbose_name='Жанр')),
            ],
            options={
                'verbose_name': 'Книга',
                'verbose_name_plural': 'Книги',
                'abstract': False,
            },
        ),
        migrations.CreateModel(
            name='Journal',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.CharField(max_length=255, verbose_name='название')),
                ('price', models.IntegerField(default=0, verbose_name='Стоимость')),
                ('description', models.TextField(blank=True, null=True, verbose_name='Описание')),
                ('created_at', models.DateTimeField(auto_now_add=True, verbose_name='Добавлен')),
                ('type', models.SmallIntegerField(choices=[(1, 'Bullet'), (2, 'Food'), (3, 'Travel'), (4, 'Sport')], default=1)),
                ('publisher', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL, verbose_name='Владелец')),
            ],
            options={
                'verbose_name': 'Книга',
                'verbose_name_plural': 'Книги',
                'abstract': False,
            },
        ),
    ]
<file_sep>from django.contrib import admin
from .models import *
# Register your models here.

class BookAdmin(admin.ModelAdmin):
    list_display = ['genre']
    search_fields = ['genre']


class JournalAdmin(admin.ModelAdmin):
    list_display = ['type','publisher']
    search_fields = ['publisher']


admin.site.register(Book,BookAdmin)
admin.site.register(Journal,JournalAdmin)
<file_sep>from django.urls import path,include
from django.contrib.auth import views
app_name = 'auth'
urlpatterns = [
    path('login/', views.LoginView.as_view(), name='login'),

]