daily pastebin goal
91%
SHARE
TWEET

Untitled

a guest Jun 30th, 2018 129 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from django.contrib import admin
  2. from django.urls import path, include
  3. from rest_framework_jwt.views import refresh_jwt_token
  4.  
  5. urlpatterns = [
  6.     path('admin/', admin.site.urls),
  7.     path('', include('rest_auth.urls')),
  8.     path('registration/', include('rest_auth.registration.urls')),
  9.     path('refresh-token/', refresh_jwt_token),
  10. ]
  11.    
  12. from django.contrib.auth.models import (
  13.     BaseUserManager, AbstractBaseUser, PermissionsMixin
  14. )
  15. from django.db import models
  16. from django.utils import timezone
  17. from django.utils.translation import ugettext_lazy as _
  18.  
  19. class UserManager(BaseUserManager):
  20.     def create_user(self, email, username, password=None):
  21.         if not email:
  22.             raise ValueError(_('Users must have an email address'))
  23.  
  24.         user = self.model(
  25.             email=self.normalize_email(email),
  26.             username=username,
  27.         )
  28.  
  29.         user.set_password(password)
  30.         user.save(using=self._db)
  31.         return user
  32.  
  33.     def create_superuser(self, email, username, password):
  34.         user = self.create_user(
  35.             email=email,
  36.             password=password,
  37.             username=username,
  38.         )
  39.  
  40.         user.is_superuser = True
  41.         user.save(using=self._db)
  42.         return user
  43.  
  44.  
  45. class User(AbstractBaseUser, PermissionsMixin):
  46.     email = models.EmailField(
  47.         verbose_name=_('Email address'),
  48.         max_length=255,
  49.         unique=True,
  50.     )
  51.     username = models.CharField(
  52.         verbose_name=_('username'),
  53.         max_length=30,
  54.         unique=True
  55.     )
  56.     is_active = models.BooleanField(
  57.         verbose_name=_('Is active'),
  58.         default=True
  59.     )
  60.     date_joined = models.DateTimeField(
  61.         verbose_name=_('Date joined'),
  62.         default=timezone.now
  63.     )
  64.     salt = models.CharField(
  65.         verbose_name=_('Salt'),
  66.         max_length=10,
  67.         blank=True
  68.     )
  69.  
  70.     objects = UserManager()
  71.  
  72.     USERNAME_FIELD = 'email'
  73.     REQUIRED_FIELDS = ['username', ]
  74.  
  75.     class Meta:
  76.         verbose_name = _('user')
  77.         verbose_name_plural = _('users')
  78.         ordering = ('-date_joined',)
  79.  
  80.     def __str__(self):
  81.         return self.username
  82.  
  83.     def get_full_name(self):        
  84.         return self.username
  85.  
  86.     def get_short_name(self):
  87.         return self.username
  88.  
  89.     @property
  90.     def is_staff(self):
  91.         "Is the user a member of staff?"
  92.         # Simplest possible answer: All superusers are staff
  93.         return self.is_superuser
  94.  
  95.     get_full_name.short_description = _('Full name')
  96.    
  97. from allauth.account.adapter import DefaultAccountAdapter
  98. from allauth.account.utils import user_username, user_email, user_field
  99.  
  100. class UserAdapter(DefaultAccountAdapter):
  101.     def save_user(self, request, user, form, commit=True):
  102.         data = form.cleaned_data
  103.         #first_name = data.get('first_name')
  104.         #last_name = data.get('last_name')
  105.         email = data.get('email')
  106.         username = data.get('username')
  107.         user_email(user, email)
  108.         user_username(user, username)
  109.         # if first_name:
  110.         #     user_field(user, 'first_name', first_name)
  111.         # if last_name:
  112.         #     user_field(user, 'last_name', last_name)
  113.         if 'password1' in data:
  114.             user.set_password(data["password1"])
  115.         else:
  116.             user.set_unusable_password()
  117.         self.populate_username(request, user)
  118.         if commit:
  119.             user.save()
  120.         return user
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand