From 9214c386c790c1fcb72c2a803bb814592858c362 Mon Sep 17 00:00:00 2001 From: Alexander-D-Karpov Date: Sat, 8 Oct 2022 13:52:49 +0300 Subject: [PATCH] added user endpoints, connected blockchain --- app/conf/api.py | 9 +++++++++ app/conf/settings/local.py | 2 +- app/marketplace/models.py | 3 ++- app/users/api/__init__.py | 0 app/users/api/serializers.py | 28 ++++++++++++++++++++++++++++ app/users/api/views.py | 12 ++++++++++++ app/users/apps.py | 3 +++ app/users/models.py | 12 +++++++----- app/users/signals.py | 19 +++++++++++++++++++ app/users/tasks.py | 6 ++++++ app/users/views.py | 3 --- 11 files changed, 87 insertions(+), 10 deletions(-) create mode 100644 app/users/api/__init__.py create mode 100644 app/users/api/serializers.py create mode 100644 app/users/api/views.py create mode 100644 app/users/signals.py create mode 100644 app/users/tasks.py delete mode 100644 app/users/views.py diff --git a/app/conf/api.py b/app/conf/api.py index f42e499..3df281e 100644 --- a/app/conf/api.py +++ b/app/conf/api.py @@ -2,6 +2,7 @@ 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 urlpatterns = [ path( @@ -30,4 +31,12 @@ urlpatterns = [ ] ), ), + path( + "users/", + include( + [ + path("", ListCreateUserApi.as_view(), name="user_list_create"), + ] + ), + ), ] diff --git a/app/conf/settings/local.py b/app/conf/settings/local.py index a5a6c14..4a7e34c 100644 --- a/app/conf/settings/local.py +++ b/app/conf/settings/local.py @@ -13,7 +13,7 @@ SECRET_KEY = env( default="7IFkV9gOD8gP3RbPfBuFmcUE4rPVvlnTlVScHr8OBCmHQrA1OIl2la2TJqKIBkTu", ) # https://docs.djangoproject.com/en/dev/ref/settings/#allowed-hosts -ALLOWED_HOSTS = ["*"] +ALLOWED_HOSTS = ["dev.akarpov.ru", "127.0.0.1"] # CACHES # ------------------------------------------------------------------------------ diff --git a/app/marketplace/models.py b/app/marketplace/models.py index 3601e73..aad4181 100644 --- a/app/marketplace/models.py +++ b/app/marketplace/models.py @@ -1,3 +1,4 @@ +from django.core.validators import MinValueValidator from django.db import models from users.models import User @@ -12,7 +13,7 @@ class Product(models.Model): image_cropped = models.ImageField(upload_to="cropped/", blank=True) nft = models.CharField(max_length=500, blank=True) - price = models.IntegerField() + price = models.IntegerField(validators=[MinValueValidator(0)]) creator = models.ForeignKey(User, related_name="products", on_delete=models.CASCADE) def __str__(self): diff --git a/app/users/api/__init__.py b/app/users/api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/users/api/serializers.py b/app/users/api/serializers.py new file mode 100644 index 0000000..1da84b7 --- /dev/null +++ b/app/users/api/serializers.py @@ -0,0 +1,28 @@ +from rest_framework import serializers + +from users.models import User + + +class UserSerializer(serializers.ModelSerializer): + class Meta: + model = User + fields = [ + "about", + "name", + "type", + "telegram", + "password", + "salary", + "respect", + "wallet_public_key", + ] + extra_kwargs = { + "password": {"write_only": True}, + "wallet_public_key": {"read_only": True}, + } + + def create(self, validated_data): + user = User.objects.create( + **validated_data, username=validated_data["telegram"] + ) + return user diff --git a/app/users/api/views.py b/app/users/api/views.py new file mode 100644 index 0000000..b00e165 --- /dev/null +++ b/app/users/api/views.py @@ -0,0 +1,12 @@ +from rest_framework import generics +from rest_framework.permissions import IsAuthenticated + +from common.permissions import IsAdmin +from users.api.serializers import UserSerializer +from users.models import User + + +class ListCreateUserApi(generics.ListCreateAPIView): + serializer_class = UserSerializer + permission_classes = [IsAuthenticated, IsAdmin] + queryset = User.objects.all() diff --git a/app/users/apps.py b/app/users/apps.py index 88f7b17..4697735 100644 --- a/app/users/apps.py +++ b/app/users/apps.py @@ -4,3 +4,6 @@ from django.apps import AppConfig class UsersConfig(AppConfig): default_auto_field = "django.db.models.BigAutoField" name = "users" + + def ready(self): + import users.signals diff --git a/app/users/models.py b/app/users/models.py index 404b25f..00a39b4 100644 --- a/app/users/models.py +++ b/app/users/models.py @@ -1,4 +1,5 @@ from django.contrib.auth.models import AbstractUser +from django.core.validators import MinValueValidator from django.db import models @@ -15,18 +16,19 @@ class User(AbstractUser): # image_cropped = models.ImageField(upload_to="cropped/", blank=True) about = models.TextField(blank=True) + name = models.CharField(max_length=120) type = models.CharField( max_length=6, choices=WorkerType.choices, default=WorkerType.WORKER ) - salary = models.IntegerField(default=0) + 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) + wallet_public_key = models.CharField(max_length=96, unique=True) + telegram = models.CharField(max_length=100, unique=True) def __str__(self): return self.username - def save(self, *args, **kwargs): - self.set_password(self.password) - super(AbstractUser, self).save(*args, **kwargs) - @property def is_manager(self): return self.type in [self.WorkerType.HR, self.WorkerType.ADMIN] diff --git a/app/users/signals.py b/app/users/signals.py new file mode 100644 index 0000000..9b96108 --- /dev/null +++ b/app/users/signals.py @@ -0,0 +1,19 @@ +from django.db.models.signals import pre_save, post_save +from django.dispatch import receiver + +from users.models import User +from utils.blockchain import create_wallet + + +@receiver(pre_save, sender=User) +def create_user(sender, instance, **kwargs): + wallet = create_wallet() + instance.wallet_public_key = wallet.publicKey + instance.wallet_private_key = wallet.privateKey + + +@receiver(post_save, sender=User) +def process_user(sender, instance, created, **kwargs): + if created: + instance.set_password(instance.password) + instance.save() diff --git a/app/users/tasks.py b/app/users/tasks.py new file mode 100644 index 0000000..d7c3152 --- /dev/null +++ b/app/users/tasks.py @@ -0,0 +1,6 @@ +from celery import shared_task + + +@shared_task +def process_dir(path): + return path diff --git a/app/users/views.py b/app/users/views.py deleted file mode 100644 index 91ea44a..0000000 --- a/app/users/views.py +++ /dev/null @@ -1,3 +0,0 @@ -from django.shortcuts import render - -# Create your views here.