Skip to content

Instantly share code, notes, and snippets.

@AliRn76
Last active December 24, 2020 10:27
Show Gist options
  • Save AliRn76/96b51a85724451c4957345eed3f5412e to your computer and use it in GitHub Desktop.
Save AliRn76/96b51a85724451c4957345eed3f5412e to your computer and use it in GitHub Desktop.
import re
from django.contrib import auth
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
from django.core import validators
from django.core.validators import RegexValidator
from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.utils.deconstruct import deconstructible
from rest_framework.authtoken.models import Token
from django.utils.translation import gettext_lazy as _
class AdminManager(BaseUserManager):
def create_user(self, username, password=None):
if not username:
raise ValueError("Username Can't be empty")
if not password:
raise ValueError("Admin Most Have a Password")
admin = self.model(username=username)
admin.set_password(password)
admin.save(using=self._db)
return admin
def create_superuser(self, username, password=None):
admin = self.create_user(
username,
password=password,
)
admin.super_admin = True
admin.save(using=self._db)
return admin
class Admin(AbstractBaseUser):
id = models.AutoField(db_column='ID', primary_key=True)
username = models.CharField(db_column='Username', max_length=63, unique=True)
password = models.CharField(max_length=127)
super_admin = models.BooleanField(db_column='SuperAdmin', default=False)
can_read = models.BooleanField(db_column='CanRead', default=False)
can_write = models.BooleanField(db_column='CanWrite', default=False)
can_update = models.BooleanField(db_column='CanUpdate', default=False)
can_delete = models.BooleanField(db_column='CanDelete', default=False)
objects = AdminManager()
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = []
def __str__(self):
return "username: " + self.username
def has_perm(self, perm, obj=None):
"Does the User have a specific permission?"
# Simplest possible answer: Yes, always
return True
def has_module_perms(self, app_label):
"Does the User have permissions to view the app `app_label`?"
# Simplest possible answer: Yes, always
return True
@property
def is_staff(self):
"Is the User a member of staff?"
# Simplest possible answer: All admins are staff
return self.super_admin
class Meta:
db_table = 'Admin'
def check_phone_number(value):
pattern = re.compile('09(\d{9})$')
if pattern.match(value):
return value
class User(models.Model):
phone_regex = RegexValidator(regex=r'09(\d{9})$',
message="Enter a valid phone_number. This value may contain only numbers.")
id = models.AutoField(db_column='ID', primary_key=True)
phone_number = models.CharField(db_column='PhoneNumber', max_length=11, unique=True, validators=[phone_regex])
full_name = models.CharField(db_column='FullName', max_length=255)
is_safir = models.BooleanField(db_column='IsSafir', default=False)
is_seller = models.BooleanField(db_column='IsSeller', default=False)
profile_picture = models.ImageField(db_column='ProfilePicture', upload_to='profile_pictures/', max_length=255, blank=True, null=True)
wallet_amount = models.IntegerField(db_column='WalletAmount', default=0)
date_joined = models.DateTimeField(db_column='DateJoined', auto_now_add=True)
last_login = models.DateTimeField(db_column='LastLogin', auto_now=True)
is_active = models.BooleanField(db_column='IsActive', default=True)
is_authenticated = models.BooleanField(db_column='IsAuthenticated', default=True)
otp = models.CharField(db_column='Otp', max_length=15, blank=True, null=True)
class Meta:
db_table = 'User'
@receiver(post_save, sender=User)
def create_auth_token(sender, instance=None, created=False, **kwargs):
if created:
Token.objects.create(user=instance)
class Token(Token):
key = models.CharField(_("Key"), max_length=40, primary_key=True)
user = models.ForeignKey(User, related_name='auth_tokens', on_delete=models.CASCADE, verbose_name=_("User"))
created = models.DateTimeField(_("Created"), auto_now_add=True)
class Wallet(models.Model):
id = models.AutoField(db_column='ID', primary_key=True)
amount = models.IntegerField(db_column='Amount', default=0)
can_withdraw = models.BooleanField(db_column='CanWithdraw', default=True)
deposit_code = models.CharField(db_column='DepositCode', max_length=127)
date = models.DateTimeField(db_column='Date', auto_now_add=True)
user_id = models.ForeignKey(User, models.DO_NOTHING, db_column='UserID')
class Meta:
db_table = 'Wallet'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment