Guest User

Untitled

a guest
Oct 20th, 2020
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.83 KB | None | 0 0
  1.  
  2. #MODELS.PY
  3. from django.db import models
  4.  
  5. from django.contrib.auth.models import AbstractBaseUser,  BaseUserManager, PermissionsMixin
  6. from django.db.models.signals import post_save
  7. from rest_framework_simplejwt.tokens import RefreshToken
  8.  
  9.  
  10.  
  11. class UserManage(BaseUserManager):
  12.     def create_user(self, username, email, password=None):
  13.  
  14.         if username is None:
  15.             raise TypeError('users should have an username')
  16.         if email is None:
  17.             raise TypeError('users should have an email')
  18.  
  19.         user = self.model(username=username, email=self.normalize_email(email))
  20.         user.set_password(password)
  21.         user.save()
  22.         return user
  23.  
  24.     def create_superuser(self, username, email, password):
  25.         if password is None:
  26.             raise TypeError('Password should not to be none.')
  27.  
  28.         user = self.create_user(username, email, password)
  29.         user.is_superuser = True
  30.         user.is_staff = True
  31.         user.save()
  32.         return user
  33.  
  34. class User(AbstractBaseUser, PermissionsMixin):
  35.     username = models.CharField(max_length=50, unique=True, db_index=True)
  36.     email = models.EmailField(max_length=50, unique=True, db_index=True)
  37.     is_verified = models.BooleanField(default=False)
  38.     is_active = models.BooleanField(default=True)
  39.     is_staff = models.BooleanField(default=False)
  40.     created_at = models.DateTimeField(auto_now_add=True)
  41.     updated_at = models.DateTimeField(auto_now=True)
  42.  
  43.     USERNAME_FIELD = 'email'
  44.     REQUIRED_FIELDS = ['username']
  45.  
  46.     objects = UserManage()
  47.  
  48.     class Meta:
  49.         app_label = 'user_data'
  50.  
  51.     def __str__(self):
  52.         return self.email
  53.  
  54.     def tokens(self):
  55.         refresh = RefreshToken.for_user(self)
  56.         return {
  57.  
  58.             'refresh': str(refresh),
  59.             'access': str(refresh.access_token)
  60.         }
  61. #ROUTER.PY
  62.  
  63.  
  64.  
  65. class DemoRouter:
  66.     """
  67.    A router to control all database operations on models in the
  68.    user application.
  69.    """
  70.     def db_for_read(self, model, **hints):
  71.         """
  72.        Attempts to read user models go to users_db.
  73.        """
  74.         if model._meta.app_label == 'user_data':
  75.             return 'users_db'
  76.         elif model._meta.app_label == 'post_data':
  77.             return 'posts_db'
  78.         return 'default'
  79.  
  80.     def db_for_write(self, model, **hints):
  81.         """
  82.        Attempts to write user models go to users_db.
  83.        """
  84.         if model._meta.app_label == 'user_data':
  85.             return 'users_db'
  86.         if model._meta.app_label == 'post_data':
  87.             return 'posts_db'
  88.         return 'default'
  89.  
  90.     def allow_relation(self, obj1, obj2, **hints):
  91.         """
  92.        Allow relations if a model in the user app is involved.
  93.        """
  94.         if obj1._meta.app_label == 'user_data' or \
  95.            obj2._meta.app_label == 'user_data':
  96.            return True
  97.         if obj1._meta.app_label == 'post_data' or \
  98.            obj2._meta.app_label == 'post_data':
  99.            return True
  100.         return None
  101.  
  102.     def allow_migrate(self, db, app_label, model_name=None, **hints):
  103.         """
  104.        Make sure the auth app only appears in the 'users_db'
  105.        database.
  106.        """
  107.         if app_label == 'user_data':
  108.             return db == 'users_db'
  109.         if app_label == 'post_data':
  110.             return db == 'posts_db'
  111.         return 'default'
  112.  
  113. #SETTINGS.PY
  114. DATABASES = {
  115.     'users_db': {
  116.         'ENGINE': 'django.db.backends.postgresql_psycopg2',
  117.         'NAME': '*',
  118.         'USER': 'djlbldcxpzonpy',
  119.         'PASSWORD': '*',
  120.         'HOST': '*',
  121.         'PORT': '5432',
  122.     },
  123.     'posts_db': {
  124.         'ENGINE': 'django.db.backends.postgresql_psycopg2',
  125.         'NAME': '*',
  126.         'USER': '*',
  127.         'PASSWORD': '*',
  128.         'HOST': '*',
  129.         'PORT': '5432',
  130.     }
  131. }
  132. DATABASE_ROUTERS = ['src.routers.DemoRouter']
  133.  
Advertisement
Add Comment
Please, Sign In to add comment