empezado models authentication

This commit is contained in:
2023-02-16 17:03:02 +01:00
parent 9510c9bed2
commit 3c405bff6d
3 changed files with 40 additions and 2 deletions

View File

@@ -1,3 +1,26 @@
from django.db import models
from django.contrib.auth.models import AbstractUser
from django.contrib.auth.base_user import BaseUserManager
# Create your models here.
class CustomUserMnager(BaseUserManager):
def create_user(self, email, password, **extra_fields):
if not email:
raise ValueError(_("El email es necesario"))
email = self.normalize_email(email)
new_user = self.model(email=email)
new_user.set_password(password)
new_user.save()
return new_user
def create_superuser(self, email, password, **extra_fields):
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
extra_fields.setdefault('is_active', True)
if extra_fields.get('is_staff') is not True:
raise ValueError(_('Debe pertenecer al staff'))
if extra_fields.get('is_superuser') is not True:
raise ValueError(_('Debe ser un superusuario'))
if extra_fields.get('is_active') is not True:
raise ValueError(_('Debe ser un usuario activo'))
return self.create_user(email, password, **extra_fields)