Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #MODELS.PY
- from django.db import models
- from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin
- from django.db.models.signals import post_save
- from rest_framework_simplejwt.tokens import RefreshToken
- class UserManage(BaseUserManager):
- def create_user(self, username, email, password=None):
- if username is None:
- raise TypeError('users should have an username')
- if email is None:
- raise TypeError('users should have an email')
- user = self.model(username=username, email=self.normalize_email(email))
- user.set_password(password)
- user.save()
- return user
- def create_superuser(self, username, email, password):
- if password is None:
- raise TypeError('Password should not to be none.')
- user = self.create_user(username, email, password)
- user.is_superuser = True
- user.is_staff = True
- user.save()
- return user
- class User(AbstractBaseUser, PermissionsMixin):
- username = models.CharField(max_length=50, unique=True, db_index=True)
- email = models.EmailField(max_length=50, unique=True, db_index=True)
- is_verified = models.BooleanField(default=False)
- is_active = models.BooleanField(default=True)
- is_staff = models.BooleanField(default=False)
- created_at = models.DateTimeField(auto_now_add=True)
- updated_at = models.DateTimeField(auto_now=True)
- USERNAME_FIELD = 'email'
- REQUIRED_FIELDS = ['username']
- objects = UserManage()
- class Meta:
- app_label = 'user_data'
- def __str__(self):
- return self.email
- def tokens(self):
- refresh = RefreshToken.for_user(self)
- return {
- 'refresh': str(refresh),
- 'access': str(refresh.access_token)
- }
- #ROUTER.PY
- class DemoRouter:
- """
- A router to control all database operations on models in the
- user application.
- """
- def db_for_read(self, model, **hints):
- """
- Attempts to read user models go to users_db.
- """
- if model._meta.app_label == 'user_data':
- return 'users_db'
- elif model._meta.app_label == 'post_data':
- return 'posts_db'
- return 'default'
- def db_for_write(self, model, **hints):
- """
- Attempts to write user models go to users_db.
- """
- if model._meta.app_label == 'user_data':
- return 'users_db'
- if model._meta.app_label == 'post_data':
- return 'posts_db'
- return 'default'
- def allow_relation(self, obj1, obj2, **hints):
- """
- Allow relations if a model in the user app is involved.
- """
- if obj1._meta.app_label == 'user_data' or \
- obj2._meta.app_label == 'user_data':
- return True
- if obj1._meta.app_label == 'post_data' or \
- obj2._meta.app_label == 'post_data':
- return True
- return None
- def allow_migrate(self, db, app_label, model_name=None, **hints):
- """
- Make sure the auth app only appears in the 'users_db'
- database.
- """
- if app_label == 'user_data':
- return db == 'users_db'
- if app_label == 'post_data':
- return db == 'posts_db'
- return 'default'
- #SETTINGS.PY
- DATABASES = {
- 'users_db': {
- 'ENGINE': 'django.db.backends.postgresql_psycopg2',
- 'NAME': '*',
- 'USER': 'djlbldcxpzonpy',
- 'PASSWORD': '*',
- 'HOST': '*',
- 'PORT': '5432',
- },
- 'posts_db': {
- 'ENGINE': 'django.db.backends.postgresql_psycopg2',
- 'NAME': '*',
- 'USER': '*',
- 'PASSWORD': '*',
- 'HOST': '*',
- 'PORT': '5432',
- }
- }
- DATABASE_ROUTERS = ['src.routers.DemoRouter']
Advertisement
Add Comment
Please, Sign In to add comment