From bafa51b748be882cc7e125fd36942fef2cc66b90 Mon Sep 17 00:00:00 2001 From: NigerianCoder Date: Thu, 9 Jun 2022 02:59:03 +0100 Subject: [PATCH] added user validation --- core/api_views.py | 8 +++++- core/input_serializer.py | 56 ++++++++++++++++++++++++++++++++++++++- core/models/abstration.py | 8 ++++-- 3 files changed, 68 insertions(+), 4 deletions(-) diff --git a/core/api_views.py b/core/api_views.py index fb22692..da225b3 100644 --- a/core/api_views.py +++ b/core/api_views.py @@ -71,7 +71,13 @@ def signup(self, request, *args, **kwargs): try: rcv_ser = SignupInputSerializer(data=self.request.data) if rcv_ser.is_valid(): - print("hello") + user = rcv_ser.create_user() + if not user.is_active: + code = "12345" + code_otp = "546387" + + else: + print("hello") else: return BadRequestResponse( "Unable to confirm", diff --git a/core/input_serializer.py b/core/input_serializer.py index 6264beb..1821781 100644 --- a/core/input_serializer.py +++ b/core/input_serializer.py @@ -1,9 +1,16 @@ from datetime import datetime, timedelta +from xml.dom import ValidationErr from django.contrib.auth import get_user_model from django.contrib.auth.models import AbstractBaseUser +from django.core.validators import validate_email as dj_validate_email from django.forms import CharField, EmailField from rest_framework.serializers import Serializer +from django.db.models import Q +from django.utils import timezone +from django.utils.translation import gettext as _ +from rest_framework.fields import * +from rest_framework.serializers import Serializer, ValidationError User = get_user_model() @@ -16,4 +23,51 @@ class SignupInputSerializer(Serializer): password = CharField() class Meta: - ref_name = None \ No newline at end of file + ref_name = None + + def validate_username(self, *args): + username = self.initial_data["username"] + u = User.objects.filter(username=username).first() + if u: + #and u.date_joined >= datetime(2020, 1, 1, tzinfo=pytz.UTC): + raise ValidationErr("This username is already used.") + return username + + def validate_email(self, *args): + email = self.initial_data["email"] + try: + dj_validate_email(email) + user = User.objects.filter(email=email).first() + if user: + raise ValidationErr("This email is already used.") + except ValidationErr as e: + raise e + + return email + + def create_user(self, *args): + username = self.validated_data["username"] + email = self.validated_data["email"] + phone = self.validated_data["phone"] + first_name = self.validated_data["first_name"] + last_name = self.validated_data["last_name"] + password = self.validated_data["password"] + + user = User.objects.filter(email=email).first() + + if not user: + user = User.objects.create_user( + username=username, + email=email, + phone_number=phone, + first_name=first_name, + last_name=last_name, + password=password + ) + user.is_active = False + user.save() + + else: + raise ValidationErr("User already exist") + + return user \ No newline at end of file diff --git a/core/models/abstration.py b/core/models/abstration.py index bf2a144..9eecc9e 100644 --- a/core/models/abstration.py +++ b/core/models/abstration.py @@ -61,7 +61,7 @@ def __str__(self): class Product(models.Model): category: models.ForeignKey = models.ForeignKey(Category, related_name="products", on_delete=models.CASCADE) - name: models.CharField(max_length=255) + name: models.CharField = models.CharField(max_length=255) slug: models.CharField = models.SlugField(unique=True) description: models.TextField = models.TextField(blank=True, null=True) price: models.DecimalField = models.DecimalField(max_digits=50, decimal_places=2) @@ -75,8 +75,12 @@ class Meta: def __str__(self): return "{} - {} - {} - {} - {}".format(self.category, - self.title, + self.name, + self.slug, + self.description, self.price, + self.stock, + self.available, self.created_at, self.updated_at)