SHARE
TWEET

Untitled

a guest Aug 13th, 2019 164 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from django.db import models
  2.      
  3. class UserManager(BaseUserManager):
  4. def create_user(self, email, password=None, is_active=True, is_staff=False, is_admin=False):
  5.     if not email:
  6.         raise ValueError("Usuário deve ter um email")
  7.  
  8.     user = self.model(
  9.         email=self.normalize_email(email)
  10.     )
  11.     user.staff = is_staff
  12.     user.admin = is_admin
  13.     user.active = is_active
  14.     user.set_password(password)
  15.     user.save(using=self._db)
  16.     return user
  17.  
  18. def create_staffuser(self, email, password=None):
  19.     user = self.create_user(
  20.         email,
  21.         password=password,
  22.         is_staff=True
  23.     )
  24.     return user
  25.  
  26. def create_superuser(self, email, password=None):
  27.     user = self.create_user(
  28.         email,
  29.         password=password,
  30.         is_staff=True,
  31.         is_admin=True
  32.     )
  33.     return user
  34.  
  35.  
  36. class User(AbstractBaseUser):
  37. email = models.EmailField(max_length=255, unique=True)
  38. username = models.CharField(max_length=255, blank=True, null=True, verbose_name='Usuário')
  39. first_name = models.CharField(max_length=255, blank=True, null=True, verbose_name='Nome')
  40. last_name = models.CharField(max_length=255, blank=True, null=True, verbose_name='Sobrenome')
  41. active = models.BooleanField(default=True)
  42. staff = models.BooleanField(default=False)
  43. admin = models.BooleanField(default=False)
  44. timestamp = models.DateTimeField(auto_now_add=True)
  45.  
  46. objects = UserManager()
  47.  
  48. USERNAME_FIELD = 'email'
  49. REQUIRED_FIELDS = []
  50.  
  51. def __str__(self):
  52.     return self.email
  53.  
  54. def get_full_name(self):
  55.     return self.email
  56.  
  57. def get_short_name(self):
  58.     pass
  59.  
  60. def has_perm(self, perm, obj=None):
  61.     return True
  62.  
  63. def has_module_perms(self, app_label):
  64.     return True
  65.  
  66. @property
  67. def is_staff(self):
  68.     return self.staff
  69.  
  70. @property
  71. def is_admin(self):
  72.     return self.admin
  73.  
  74. @property
  75. def is_active(self):
  76.     return self.active
  77.  
  78. class Meta:
  79.     verbose_name = 'Usuário'
  80.     verbose_name_plural = 'Usuários'
  81.      
  82. def register(request):
  83.     if request.user.is_authenticated:
  84.         return HttpResponseRedirect(reverse('core:home'))
  85.     template_name = 'contas/register.html'
  86.     if request.method == 'POST':
  87.         form = RegisterForm(request.POST)
  88.         if form.is_valid():
  89.             user = form.save()
  90.             user = authenticate(username=user.email, password=form.cleaned_data['password1'])
  91.             auth_login(request, user)
  92.             return redirect('core:home')
  93.     else:
  94.         form = RegisterForm()
  95.     context = {
  96.         'form': form
  97.     }
  98.     return render(request, template_name, context)
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
 
Top