added user endpoints, connected blockchain

This commit is contained in:
Alexander Karpov 2022-10-08 13:52:49 +03:00
parent c56f8e018f
commit 9214c386c7
11 changed files with 87 additions and 10 deletions

View File

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

View File

@ -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
# ------------------------------------------------------------------------------

View File

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

View File

View File

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

12
app/users/api/views.py Normal file
View File

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

View File

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

View File

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

19
app/users/signals.py Normal file
View File

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

6
app/users/tasks.py Normal file
View File

@ -0,0 +1,6 @@
from celery import shared_task
@shared_task
def process_dir(path):
return path

View File

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