Created
July 19, 2015 15:26
-
-
Save gengue/aefd369616654bd26222 to your computer and use it in GitHub Desktop.
Create a custom user in Django1.8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
file: profiles/admin.py | |
""" | |
from django.contrib import admin | |
from django.contrib.auth.forms import UserChangeForm, UserCreationForm, ReadOnlyPasswordHashField | |
from .models import User | |
# Formulario de creacion de usuario | |
class UserCreationForm(UserCreationForm): | |
class Meta(UserCreationForm.Meta): | |
model = User | |
fields = ("email",) | |
# Formulario de edicion de usuario | |
class UserChangeForm(UserChangeForm): | |
password = ReadOnlyPasswordHashField(label= ("Password"), | |
help_text= ("Las contraseñas no se almacenan crudas. No hay manera de ver " | |
"la contraseña de este usuario, pero puedes cambiarla " | |
"usando <a href=\"password/\">este formulario</a>.")) | |
class Meta(UserChangeForm.Meta): | |
model = User | |
class UserChangeForm(UserChangeForm): | |
password = ReadOnlyPasswordHashField(label= ("Password"), | |
help_text= ("Las contraseñas no se almacenan crudas. No hay manera de ver " | |
"la contraseña de este usuario, pero puedes cambiarla " | |
"usando <a href=\"password/\">este formulario</a>.")) | |
class Meta(UserChangeForm.Meta): | |
model = User | |
# Admin del usuario | |
class UserAdmin(UserAdmin): | |
form = UserChangeForm | |
add_form = UserCreationForm | |
fieldsets = ( | |
(None, {'fields': ('username', 'first_name', 'last_name', 'email', 'phone', 'password',)}), | |
('Permisos', {'fields': ('is_active', 'is_superuser', 'hotel', 'groups',)}), | |
) | |
add_fieldsets = ( | |
(None, { | |
'classes': ('wide',), | |
'fields': ('username', 'first_name', 'last_name', 'email', 'password1', 'password2', 'phone',)} | |
), | |
) | |
list_display = ('username', 'fullname', 'email', 'phone', 'dni', 'is_active',) | |
list_filter = ('is_active', 'is_superuser',) | |
search_fields = ('username', 'first_name', 'last_name', 'email', 'dni', 'phone',) | |
ordering = ('first_name', 'email',) | |
def fullname(self, obj): | |
return obj.get_full_name() | |
fullname.short_description = 'Nombre completo' | |
fullname.admin_order_field = 'first_name' | |
admin.site.register(User, UserAdmin) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
file: profiles/models.py | |
""" | |
from django.db import models | |
from django.contrib.auth.models import AbstractUser | |
from hotels.models import Hotel | |
class User(AbstractUser): | |
""" | |
La clase extiende del modelo user de django y agrega | |
los campos adicionales que se requieren | |
""" | |
phone = models.CharField(max_length=255, blank=True, null=True) | |
dni = models.CharField(max_length=255, blank=True, null=True, unique=True) | |
def __unicode__(self): | |
return self.get_full_name() | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
AUTH_USER_MODEL = 'profiles.User' | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
file: profiles/user_manager.py | |
""" | |
from django.contrib.auth.models import BaseUserManager | |
from django.utils import timezone | |
class AuthUserManager(BaseUserManager): | |
def create_user(self, email, password=None): | |
if not email: | |
raise ValueError('Users must have an email address') | |
user = self.model(email=self.normalize_email(email),) | |
user.is_active = True | |
user.set_password(password) | |
user.save(using=self._db) | |
return user | |
def create_superuser(self, email, password): | |
user = self.create_user(email=email, password=password) | |
user.is_staff = True | |
user.is_superuser = True | |
user.save(using=self._db) | |
return user |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment