Merge branch 'main' into clans

This commit is contained in:
Ilia vasilenko 2022-10-08 17:16:16 +03:00 committed by GitHub
commit 117b2f3bae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 187 additions and 140 deletions

View File

@ -2,6 +2,16 @@ from django.urls import path, include
from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView
from marketplace.api.views import ListCreateProductApi, RetireUpdateDestroyProductApi
from users.api.views import ListCreateUserApi, CreateSeasonApi
from users.api.views import (
ListCreateUserApi,
RetireUpdateDeleteUserApi,
ListCreateDepartmentApi,
RetireUpdateDeleteDepartmentApi,
ListCreateStreamApi,
RetireUpdateDeleteStreamApi,
ListCreateCommandApi,
RetireUpdateDeleteCommandApi,
)
urlpatterns = [
path(
@ -34,7 +44,47 @@ urlpatterns = [
"users/",
include(
[
path("", ListCreateUserApi.as_view(), name="user_list_create"),
path("", ListCreateUserApi.as_view(), name="list_create_user"),
path(
"<str:username>",
RetireUpdateDeleteUserApi.as_view(),
name="get_update_delete_user",
),
path(
"department/",
ListCreateDepartmentApi.as_view(),
name="list_create_department",
),
path(
"department/",
ListCreateDepartmentApi.as_view(),
name="list_create_department",
),
path(
"department/<int:pk>",
RetireUpdateDeleteDepartmentApi.as_view(),
name="get_update_delete_department",
),
path(
"stream/",
ListCreateStreamApi.as_view(),
name="list_create_stream",
),
path(
"stream/<int:pk>",
RetireUpdateDeleteStreamApi.as_view(),
name="get_update_delete_stream",
),
path(
"command/",
ListCreateCommandApi.as_view(),
name="list_create_command",
),
path(
"command/<int:pk>",
RetireUpdateDeleteCommandApi.as_view(),
name="get_update_delete_command",
),
]
),
),

View File

@ -22,8 +22,8 @@ class RetireUpdateDestroyProductApi(generics.RetrieveUpdateDestroyAPIView):
queryset = Product.objects.all()
def get_object(self):
block = get_object_or_404(
product = get_object_or_404(
Product,
slug=self.request.parser_context["kwargs"]["slug"],
)
return block
return product

View File

@ -1,23 +0,0 @@
# Generated by Django 4.0.8 on 2022-10-08 09:27
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('marketplace', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='product',
name='slug',
field=models.SlugField(max_length=20, null=True),
),
migrations.AlterField(
model_name='product',
name='description',
field=models.TextField(blank=True),
),
]

View File

@ -1,19 +0,0 @@
# Generated by Django 4.0.8 on 2022-10-08 11:42
import django.core.validators
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('marketplace', '0002_product_slug_alter_product_description'),
]
operations = [
migrations.AlterField(
model_name='product',
name='price',
field=models.IntegerField(validators=[django.core.validators.MinValueValidator(0)]),
),
]

View File

@ -1,6 +1,7 @@
from rest_framework import serializers
from ..service import create_season
from users.models import User
from users.models import User, Department, Stream, Command
class UserSerializer(serializers.ModelSerializer):
@ -15,10 +16,13 @@ class UserSerializer(serializers.ModelSerializer):
"salary",
"respect",
"wallet_public_key",
"command",
"department",
]
extra_kwargs = {
"password": {"write_only": True},
"wallet_public_key": {"read_only": True},
"department": {"read_only": True},
}
def create(self, validated_data):
@ -33,3 +37,41 @@ class CreateSeasonSerializer(serializers.Serializer):
def create(self, *args, **kwargs):
create_season()
return {'created': True}
class CommandSerializer(serializers.ModelSerializer):
workers = UserSerializer(many=True)
class Meta:
model = Command
fields = ["id", "name", "stream", "workers"]
extra_kwargs = {
"id": {"read_only": True},
"stream": {"write_only": True},
"workers": {"read_only": True},
}
class StreamSerializer(serializers.ModelSerializer):
commands = CommandSerializer(many=True)
class Meta:
model = Stream
fields = ["id", "name", "department", "commands"]
extra_kwargs = {
"id": {"read_only": True},
"department": {"write_only": True},
"commands": {"read_only": True},
}
class DepartmentSerializer(serializers.ModelSerializer):
streams = StreamSerializer(many=True)
class Meta:
model = Department
fields = ["id", "name", "streams"]
extra_kwargs = {
"id": {"read_only": True},
"streams": {"read_only": True},
}

View File

@ -1,9 +1,17 @@
from rest_framework import generics
from rest_framework.generics import get_object_or_404
from rest_framework.permissions import IsAuthenticated
from common.permissions import IsAdmin
from users.api.serializers import UserSerializer, CreateSeasonSerializer
from users.models import User
from users.api.serializers import (
UserSerializer,
DepartmentSerializer,
StreamSerializer,
CommandSerializer,
)
from users.models import User, Department, Stream, Command
class ListCreateUserApi(generics.ListCreateAPIView):
@ -15,3 +23,48 @@ class ListCreateUserApi(generics.ListCreateAPIView):
class CreateSeasonApi(generics.CreateAPIView):
serializer_class = CreateSeasonSerializer
#permission_classes = [IsAuthenticated, IsAdmin]
class RetireUpdateDeleteUserApi(generics.RetrieveUpdateDestroyAPIView):
def get_object(self):
user = get_object_or_404(
User,
username=self.request.parser_context["kwargs"]["username"],
)
return user
serializer_class = UserSerializer
permission_classes = [IsAuthenticated, IsAdmin]
queryset = User.objects.all()
class ListCreateDepartmentApi(generics.ListCreateAPIView):
serializer_class = DepartmentSerializer
permission_classes = [IsAuthenticated, IsAdmin]
queryset = Department.objects.all()
class RetireUpdateDeleteDepartmentApi(generics.RetrieveUpdateDestroyAPIView):
lookup_field = "pk"
serializer_class = DepartmentSerializer
permission_classes = [IsAuthenticated, IsAdmin]
queryset = Department.objects.all()
class ListCreateStreamApi(ListCreateDepartmentApi):
serializer_class = StreamSerializer
queryset = Stream.objects.all()
class RetireUpdateDeleteStreamApi(RetireUpdateDeleteDepartmentApi):
serializer_class = StreamSerializer
queryset = Stream.objects.all()
class ListCreateCommandApi(ListCreateDepartmentApi):
serializer_class = CommandSerializer
queryset = Command.objects.all()
class RetireUpdateDeleteCommandApi(RetireUpdateDeleteDepartmentApi):
serializer_class = CommandSerializer
queryset = Command.objects.all()

View File

@ -1,36 +0,0 @@
# Generated by Django 4.0.8 on 2022-10-08 09:27
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('users', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='Clan',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=100, null=True)),
],
),
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),
),
migrations.AddField(
model_name='user',
name='clan',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='users.clan'),
),
]

View File

@ -1,44 +0,0 @@
# Generated by Django 4.0.8 on 2022-10-08 11:42
import django.core.validators
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('users', '0002_clan_user_salary_user_type_user_clan'),
]
operations = [
migrations.AddField(
model_name='user',
name='name',
field=models.CharField(max_length=120, null=True),
),
migrations.AddField(
model_name='user',
name='respect',
field=models.IntegerField(default=0, validators=[django.core.validators.MinValueValidator(0)]),
),
migrations.AddField(
model_name='user',
name='telegram',
field=models.CharField(max_length=100, null=True, unique=True),
),
migrations.AddField(
model_name='user',
name='wallet_private_key',
field=models.CharField(max_length=96, null=True, unique=True),
),
migrations.AddField(
model_name='user',
name='wallet_public_key',
field=models.CharField(max_length=96, null=True, unique=True),
),
migrations.AlterField(
model_name='user',
name='salary',
field=models.IntegerField(default=0, validators=[django.core.validators.MinValueValidator(0)]),
),
]

View File

@ -1,16 +1,6 @@
from django.contrib.auth.models import AbstractUser
from django.core.validators import MinValueValidator
from django.db import models
from faker import Faker
class Clan(models.Model):
name = models.CharField(max_length=100, null=True)
def save(self, *args, **kwargs):
name = Faker().name()
self.name = name + "'s clan"
super(Clan, self).save(*args, **kwargs)
class User(AbstractUser):
@ -26,17 +16,20 @@ class User(AbstractUser):
# image_cropped = models.ImageField(upload_to="cropped/", blank=True)
about = models.TextField(blank=True)
name = models.CharField(max_length=120, null=True)
name = models.CharField(max_length=120)
type = models.CharField(
max_length=6, choices=WorkerType.choices, default=WorkerType.WORKER
)
salary = models.IntegerField(default=0)
clan = models.ForeignKey(Clan, on_delete=models.SET_NULL, null=True)
command = models.ForeignKey(
"users.Command", related_name="workers", on_delete=models.CASCADE
)
salary = models.IntegerField(default=0, validators=[MinValueValidator(0)])
respect = models.IntegerField(default=0, validators=[MinValueValidator(0)])
wallet_private_key = models.CharField(max_length=96, unique=True, null=True)
wallet_public_key = models.CharField(max_length=96, unique=True, null=True)
telegram = models.CharField(max_length=100, unique=True, null=True)
wallet_private_key = models.CharField(max_length=96, unique=True)
wallet_public_key = models.CharField(max_length=96, unique=True)
telegram = models.CharField(max_length=100, unique=True)
def __str__(self):
return self.username
@ -49,5 +42,36 @@ class User(AbstractUser):
def is_admin(self):
return self.type == self.WorkerType.ADMIN
@property
def department(self):
return self.command.stream.department.name
class Meta:
ordering = ["-id"]
ordering = ["-id"]
class Department(models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return self.name
class Stream(models.Model):
name = models.CharField(max_length=100)
department = models.ForeignKey(
Department, related_name="streams", on_delete=models.CASCADE
)
def __str__(self):
return self.name
class Command(models.Model):
name = models.CharField(max_length=100)
stream = models.ForeignKey(
Stream, related_name="commands", on_delete=models.CASCADE
)
def __str__(self):
return self.name