diff options
| author | Mitch Riedstra <Mitch@riedstra.us> | 2017-11-02 16:03:14 -0400 |
|---|---|---|
| committer | Mitch Riedstra <Mitch@riedstra.us> | 2017-11-02 16:03:14 -0400 |
| commit | e16fa08b915562c6ab77ce7bb79a9d766b5a4036 (patch) | |
| tree | 2f7d518131600f2db7bc3ac0545f11f2571c2aee /app/dispatchAuth | |
| parent | e4d865b1a61f6a72551e70abad78c6c35b9345e7 (diff) | |
| download | dispatch-tracker-e16fa08b915562c6ab77ce7bb79a9d766b5a4036.tar.gz dispatch-tracker-e16fa08b915562c6ab77ce7bb79a9d766b5a4036.tar.xz | |
Initial setup to use a custom User model, I still need to figure out how to use the built in Django permissions though
Diffstat (limited to 'app/dispatchAuth')
| -rw-r--r-- | app/dispatchAuth/__init__.py | 0 | ||||
| -rw-r--r-- | app/dispatchAuth/admin.py | 90 | ||||
| -rw-r--r-- | app/dispatchAuth/apps.py | 5 | ||||
| -rw-r--r-- | app/dispatchAuth/migrations/0001_initial.py | 35 | ||||
| -rw-r--r-- | app/dispatchAuth/migrations/__init__.py | 0 | ||||
| -rw-r--r-- | app/dispatchAuth/models.py | 69 | ||||
| -rw-r--r-- | app/dispatchAuth/tests.py | 3 | ||||
| -rw-r--r-- | app/dispatchAuth/views.py | 3 |
8 files changed, 205 insertions, 0 deletions
diff --git a/app/dispatchAuth/__init__.py b/app/dispatchAuth/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/app/dispatchAuth/__init__.py diff --git a/app/dispatchAuth/admin.py b/app/dispatchAuth/admin.py new file mode 100644 index 0000000..67b1170 --- /dev/null +++ b/app/dispatchAuth/admin.py @@ -0,0 +1,90 @@ +from django import forms +from django.contrib import admin +from django.contrib.auth.models import Group +from django.contrib.auth.admin import UserAdmin as BaseUserAdmin +from django.contrib.auth.forms import ReadOnlyPasswordHashField + +# Register your models here. + + +from .models import User + +class UserCreationForm(forms.ModelForm): + """A form for creating new users. Includes all the required + fields, plus a repeated password.""" + password1 = forms.CharField(label='Password', widget=forms.PasswordInput) + password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput) + + class Meta: + model = User + fields = ('email', 'first_name', 'last_name') + + def clean_password2(self): + # Check that the two password entries match + password1 = self.cleaned_data.get("password1") + password2 = self.cleaned_data.get("password2") + if password1 and password2 and password1 != password2: + raise forms.ValidationError("Passwords don't match") + return password2 + + def save(self, commit=True): + # Save the provided password in hashed format + user = super(UserCreationForm, self).save(commit=False) + user.set_password(self.cleaned_data["password1"]) + if commit: + user.save() + return user + + +class UserChangeForm(forms.ModelForm): + """A form for updating users. Includes all the fields on + the user, but replaces the password field with admin's + password hash display field. + """ + password = ReadOnlyPasswordHashField() + + class Meta: + model = User + fields = ('email', 'password', 'first_name', 'last_name', 'is_active', + 'is_superuser') + + def clean_password(self): + # Regardless of what the user provides, return the initial value. + # This is done here, rather than on the field, because the + # field does not have access to the initial value + return self.initial["password"] + + +class UserAdmin(BaseUserAdmin): + # The forms to add and change user instances + form = UserChangeForm + add_form = UserCreationForm + + # The fields to be used in displaying the User model. + # These override the definitions on the base UserAdmin + # that reference specific fields on auth.User. + list_display = ('email', 'is_superuser') + list_filter = ('is_superuser',) + fieldsets = ( + (None, {'fields': ('email', 'password')}), + ('Personal info', {'fields': ('first_name','last_name')}), + ('Permissions', {'fields': ('is_superuser',)}), + ) + # add_fieldsets is not a standard ModelAdmin attribute. UserAdmin + # overrides get_fieldsets to use this attribute when creating a user. + add_fieldsets = ( + (None, { + 'classes': ('wide',), + 'fields': ('email', 'first_name', 'last_name', 'password1', 'password2')} + ), + ) + search_fields = ('email',) + ordering = ('email',) + filter_horizontal = () + +# Now register the new UserAdmin... +admin.site.register(User, UserAdmin) + +# ... and, since we're not using Django's built-in permissions, +# unregister the Group model from admin. +# admin.site.unregister(Group) diff --git a/app/dispatchAuth/apps.py b/app/dispatchAuth/apps.py new file mode 100644 index 0000000..ba984ef --- /dev/null +++ b/app/dispatchAuth/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class DispatchauthConfig(AppConfig): + name = 'dispatchAuth' diff --git a/app/dispatchAuth/migrations/0001_initial.py b/app/dispatchAuth/migrations/0001_initial.py new file mode 100644 index 0000000..4a0f37c --- /dev/null +++ b/app/dispatchAuth/migrations/0001_initial.py @@ -0,0 +1,35 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.5 on 2017-11-02 19:51 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ('auth', '0008_alter_user_username_max_length'), + ] + + operations = [ + migrations.CreateModel( + name='User', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('password', models.CharField(max_length=128, verbose_name='password')), + ('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')), + ('email', models.EmailField(max_length=256, unique=True)), + ('first_name', models.CharField(max_length=256)), + ('last_name', models.CharField(max_length=256)), + ('is_active', models.BooleanField(default=True)), + ('is_superuser', models.BooleanField(default=False)), + ('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.Group', verbose_name='groups')), + ('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.Permission', verbose_name='user permissions')), + ], + options={ + 'abstract': False, + }, + ), + ] diff --git a/app/dispatchAuth/migrations/__init__.py b/app/dispatchAuth/migrations/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/app/dispatchAuth/migrations/__init__.py diff --git a/app/dispatchAuth/models.py b/app/dispatchAuth/models.py new file mode 100644 index 0000000..6c90dd0 --- /dev/null +++ b/app/dispatchAuth/models.py @@ -0,0 +1,69 @@ +from django.contrib.auth.models import AbstractUser, AbstractBaseUser, PermissionsMixin, BaseUserManager +from django.db import models + +# Create your models here. + + +class UserManager(BaseUserManager): + def create_user(self, email, first_name, last_name, password=None): + """ + Creates and saves a User with the given email, first name, + last name and password. + """ + if not email: + raise ValueError('Users must have an email address') + + user = self.model( + email=self.normalize_email(email), + first_name=first_name, + last_name=last_name, + ) + + user.set_password(password) + user.save(using=self._db) + return user + + def create_superuser(self, email, first_name, last_name, password): + """ + Creates and saves a superuser with the given email, first name, + last name and password. + """ + user = self.create_user( + email=email, + first_name=first_name, + last_name=last_name, + password=password, + ) + user.is_superuser = True + user.save(using=self._db) + return user + + +class User(PermissionsMixin, AbstractBaseUser): + email = models.EmailField(max_length=256, unique=True) + first_name = models.CharField(max_length=256) + last_name = models.CharField(max_length=256) + is_active = models.BooleanField(default=True) + is_superuser = models.BooleanField(default=False) + + USERNAME_FIELD = 'email' + EMAIL_FIELD = 'email' + REQUIRED_FIELDS = ['first_name', 'last_name'] + + objects = UserManager() + + def get_full_name(self): + return "{} {}".format(self.first_name, self.last_name) + + def get_short_name(self): + return self.first_name + + def get_absolute_url(self): + return reverse('driver_details', kwargs={'pk': self.pk}) + + def __str__(self): + return self.email + + @property + def is_staff(self): + return self.is_superuser diff --git a/app/dispatchAuth/tests.py b/app/dispatchAuth/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/app/dispatchAuth/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/app/dispatchAuth/views.py b/app/dispatchAuth/views.py new file mode 100644 index 0000000..91ea44a --- /dev/null +++ b/app/dispatchAuth/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here. |
