Skip to content

Commit

Permalink
added user validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Nigeriancoder committed Jun 9, 2022
1 parent ff99b0e commit bafa51b
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
8 changes: 7 additions & 1 deletion core/api_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
56 changes: 55 additions & 1 deletion core/input_serializer.py
Original file line number Diff line number Diff line change
@@ -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()

Expand All @@ -16,4 +23,51 @@ class SignupInputSerializer(Serializer):
password = CharField()

class Meta:
ref_name = None
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
8 changes: 6 additions & 2 deletions core/models/abstration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

Expand Down

0 comments on commit bafa51b

Please sign in to comment.