From 030566d9b52235826edea9c735e75b06d6e4c6ff Mon Sep 17 00:00:00 2001 From: Maxime Bloch Date: Tue, 21 Jul 2020 22:29:33 +0200 Subject: [PATCH 1/4] Add events as default path --- KeRS/urls.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/KeRS/urls.py b/KeRS/urls.py index 0d67372..8902ed7 100644 --- a/KeRS/urls.py +++ b/KeRS/urls.py @@ -17,6 +17,6 @@ from django.contrib import admin from django.urls import path, include urlpatterns = [ - path('events/', include('events.urls')), + path('', include('events.urls')), path('admin/', admin.site.urls), ] From e6068af1b7880fdb8b5836c575fad6833ec46289 Mon Sep 17 00:00:00 2001 From: Maxime Bloch Date: Wed, 22 Jul 2020 00:51:40 +0200 Subject: [PATCH 2/4] Add custom users --- KeRS/settings.py | 42 ++++++++++----------- events/migrations/0001_initial.py | 7 ++-- events/models.py | 4 +- requirements.txt | 1 + users/managers.py | 33 ++++++++++++++++ users/migrations/0001_initial.py | 20 ++++++++-- users/migrations/0002_auto_20200721_2249.py | 18 +++++++++ users/models.py | 24 ++++++++++-- 8 files changed, 113 insertions(+), 36 deletions(-) create mode 100644 users/managers.py create mode 100644 users/migrations/0002_auto_20200721_2249.py diff --git a/KeRS/settings.py b/KeRS/settings.py index dd4bf9f..33a4a3a 100644 --- a/KeRS/settings.py +++ b/KeRS/settings.py @@ -15,7 +15,6 @@ import os # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) - # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/ @@ -27,7 +26,6 @@ DEBUG = True ALLOWED_HOSTS = [] - # Application definition OWN_APPS = [ @@ -36,15 +34,13 @@ OWN_APPS = [ ] INSTALLED_APPS = [ - 'django.contrib.admin', - 'django.contrib.auth', - 'django.contrib.contenttypes', - 'django.contrib.sessions', - 'django.contrib.messages', - 'django.contrib.staticfiles', -] + OWN_APPS - - + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + ] + OWN_APPS MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', @@ -60,11 +56,11 @@ ROOT_URLCONF = 'KeRS.urls' TEMPLATES = [ { - 'BACKEND': 'django.template.backends.django.DjangoTemplates', - 'DIRS': [os.path.join(BASE_DIR, 'templates')] + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [os.path.join(BASE_DIR, 'templates')] , 'APP_DIRS': True, - 'OPTIONS': { + 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', @@ -77,22 +73,20 @@ TEMPLATES = [ WSGI_APPLICATION = 'KeRS.wsgi.application' - # Database # https://docs.djangoproject.com/en/3.0/ref/settings/#databases DATABASES = { 'default': { - 'ENGINE': 'django.db.backends.mysql', - 'NAME': 'kers', - 'USER': 'kers', + 'ENGINE': 'django.db.backends.mysql', + 'NAME': 'kers', + 'USER': 'kers', 'PASSWORD': 'kers', - 'HOST': '127.0.0.1', - 'PORT': '3306', + 'HOST': '127.0.0.1', + 'PORT': '3306', } } - # Password validation # https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators @@ -111,7 +105,6 @@ AUTH_PASSWORD_VALIDATORS = [ }, ] - # Internationalization # https://docs.djangoproject.com/en/3.0/topics/i18n/ @@ -125,8 +118,11 @@ USE_L10N = True USE_TZ = True - # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/3.0/howto/static-files/ STATIC_URL = '/static/' + +# Custom stuff + +AUTH_USER_MODEL = 'users.CustomUser' diff --git a/events/migrations/0001_initial.py b/events/migrations/0001_initial.py index caeecc0..f50a5f5 100644 --- a/events/migrations/0001_initial.py +++ b/events/migrations/0001_initial.py @@ -1,5 +1,6 @@ -# Generated by Django 3.0.8 on 2020-07-21 20:00 +# Generated by Django 3.0.8 on 2020-07-21 21:36 +from django.conf import settings from django.db import migrations, models import django.db.models.deletion @@ -9,7 +10,7 @@ class Migration(migrations.Migration): initial = True dependencies = [ - ('users', '__first__'), + migrations.swappable_dependency(settings.AUTH_USER_MODEL), ] operations = [ @@ -27,7 +28,7 @@ class Migration(migrations.Migration): ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('shirt_size', models.CharField(choices=[('I', 'Interested'), ('A', 'Admitted'), ('D', 'Denied')], max_length=1)), ('event_id', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='events.Event')), - ('user_id', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='users.User')), + ('user_id', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), ], ), ] diff --git a/events/models.py b/events/models.py index 6023ca4..04076f1 100644 --- a/events/models.py +++ b/events/models.py @@ -1,6 +1,6 @@ from django.db import models -from users.models import User +from users.models import CustomUser class Event(models.Model): @@ -15,5 +15,5 @@ class EventRegistration(models.Model): ('D', 'Denied'), ) event_id = models.ForeignKey(Event, on_delete=models.CASCADE) - user_id = models.ForeignKey(User, on_delete=models.CASCADE) + user_id = models.ForeignKey(CustomUser, on_delete=models.CASCADE) shirt_size = models.CharField(max_length=1, choices=REGISTRATION_STATE) diff --git a/requirements.txt b/requirements.txt index 1150930..6f5b4bd 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ asgiref==3.2.10 Django==3.0.8 +mysqlclient==2.0.1 pytz==2020.1 sqlparse==0.3.1 diff --git a/users/managers.py b/users/managers.py new file mode 100644 index 0000000..bdf2ec7 --- /dev/null +++ b/users/managers.py @@ -0,0 +1,33 @@ +from django.contrib.auth.base_user import BaseUserManager +from django.utils.translation import ugettext_lazy as _ + + +class CustomUserManager(BaseUserManager): + """ + Custom user model manager where email is the unique identifiers + for authentication instead of usernames. + """ + + def create_user(self, zeus_id, username, password=None, **extra_fields): + """ + Create and save a User with the given email and password. + """ + if zeus_id is None or username is None: + raise ValueError(_('The zeus_id and username must be set')) + user = self.model(zeus_id=zeus_id, username=username, password=password, **extra_fields) + user.set_password(password) + user.save() + return user + + def create_superuser(self, zeus_id, username, password, **extra_fields): + """ + Create and save a SuperUser with the given email and password. + """ + extra_fields.setdefault('is_staff', True) + extra_fields.setdefault('is_superuser', True) + + if extra_fields.get('is_staff') is not True: + raise ValueError(_('Superuser must have is_staff=True.')) + if extra_fields.get('is_superuser') is not True: + raise ValueError(_('Superuser must have is_superuser=True.')) + return self.create_user(zeus_id, username, password, **extra_fields) diff --git a/users/migrations/0001_initial.py b/users/migrations/0001_initial.py index 463055b..b68ad10 100644 --- a/users/migrations/0001_initial.py +++ b/users/migrations/0001_initial.py @@ -1,6 +1,7 @@ -# Generated by Django 3.0.8 on 2020-07-21 20:03 +# Generated by Django 3.0.8 on 2020-07-21 22:48 from django.db import migrations, models +import django.utils.timezone class Migration(migrations.Migration): @@ -8,17 +9,28 @@ class Migration(migrations.Migration): initial = True dependencies = [ + ('auth', '0011_update_proxy_permissions'), ] operations = [ migrations.CreateModel( - name='User', + name='CustomUser', 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')), + ('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')), ('zeus_id', models.IntegerField()), - ('username', models.CharField(max_length=255)), + ('is_staff', models.BooleanField(default=False)), + ('username', models.CharField(max_length=50, unique=True)), ('student_number', models.CharField(max_length=255)), - ('real_name', models.CharField(max_length=255)), + ('real_name', models.CharField(max_length=100)), + ('date_joined', models.DateTimeField(default=django.utils.timezone.now)), + ('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/users/migrations/0002_auto_20200721_2249.py b/users/migrations/0002_auto_20200721_2249.py new file mode 100644 index 0000000..2d53721 --- /dev/null +++ b/users/migrations/0002_auto_20200721_2249.py @@ -0,0 +1,18 @@ +# Generated by Django 3.0.8 on 2020-07-21 22:49 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('users', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='customuser', + name='zeus_id', + field=models.IntegerField(null=True, unique=True), + ), + ] diff --git a/users/models.py b/users/models.py index ea16066..4277ec7 100644 --- a/users/models.py +++ b/users/models.py @@ -1,8 +1,24 @@ +from django.contrib.auth.base_user import AbstractBaseUser from django.db import models +from django.contrib.auth.models import User, PermissionsMixin +from django.utils import timezone + +from users.managers import CustomUserManager -class User(models.Model): - zeus_id = models.IntegerField() - username = models.CharField(max_length=255) # zeus username +class CustomUser(AbstractBaseUser, PermissionsMixin): + zeus_id = models.IntegerField(unique=True, null=True) + is_staff = models.BooleanField(default=False) + username = models.CharField(max_length=50, unique=True) # zeus username student_number = models.CharField(max_length=255) - real_name = models.CharField(max_length=255) + real_name = models.CharField(max_length=100) + + date_joined = models.DateTimeField(default=timezone.now) + + USERNAME_FIELD = 'username' + REQUIRED_FIELDS = ['zeus_id'] + + objects = CustomUserManager() + + def __str__(self): + return self.username From 33af7f13d183912c18f951f61d8a287f9668ce5b Mon Sep 17 00:00:00 2001 From: Maxime Bloch Date: Wed, 22 Jul 2020 00:52:02 +0200 Subject: [PATCH 3/4] Add admin stuff for custom users --- users/admin.py | 28 ++++++++++++++++++++++++++++ users/forms.py | 15 +++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 users/admin.py create mode 100644 users/forms.py diff --git a/users/admin.py b/users/admin.py new file mode 100644 index 0000000..2980848 --- /dev/null +++ b/users/admin.py @@ -0,0 +1,28 @@ +from django.contrib import admin +from django.contrib.auth.admin import UserAdmin + +from .forms import CustomUserCreationForm, CustomUserChangeForm +from .models import CustomUser + + +class CustomUserAdmin(UserAdmin): + add_form = CustomUserCreationForm + form = CustomUserChangeForm + model = CustomUser + list_display = ('username', 'is_staff', 'is_superuser') + list_filter = ('username', 'is_staff') + fieldsets = ( + (None, {'fields': ('username', 'password')}), + ('Permissions', {'fields': ('is_staff', 'is_superuser')}), + ) + add_fieldsets = ( + (None, { + 'classes': ('wide',), + 'fields': ('username', 'password1', 'password2', 'is_staff')} + ), + ) + search_fields = ('username',) + ordering = ('username',) + + +admin.site.register(CustomUser, CustomUserAdmin) diff --git a/users/forms.py b/users/forms.py new file mode 100644 index 0000000..d08ecbd --- /dev/null +++ b/users/forms.py @@ -0,0 +1,15 @@ +from django.contrib.auth.forms import UserCreationForm, UserChangeForm + +from .models import CustomUser + + +class CustomUserCreationForm(UserCreationForm): + class Meta(UserCreationForm): + model = CustomUser + fields = ('username',) + + +class CustomUserChangeForm(UserChangeForm): + class Meta: + model = CustomUser + fields = ('username',) From 6240f53ef54c15e3bc09d1ddec9a76ad0f2054f9 Mon Sep 17 00:00:00 2001 From: Maxime Bloch Date: Wed, 22 Jul 2020 01:44:14 +0200 Subject: [PATCH 4/4] Fix something :) --- events/models.py | 2 +- requirements.txt | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/events/models.py b/events/models.py index 04076f1..b030d7f 100644 --- a/events/models.py +++ b/events/models.py @@ -16,4 +16,4 @@ class EventRegistration(models.Model): ) event_id = models.ForeignKey(Event, on_delete=models.CASCADE) user_id = models.ForeignKey(CustomUser, on_delete=models.CASCADE) - shirt_size = models.CharField(max_length=1, choices=REGISTRATION_STATE) + state = models.CharField(max_length=1, choices=REGISTRATION_STATE) diff --git a/requirements.txt b/requirements.txt index 6f5b4bd..1fc8adc 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,4 +2,5 @@ asgiref==3.2.10 Django==3.0.8 mysqlclient==2.0.1 pytz==2020.1 +PyYAML==5.3.1 sqlparse==0.3.1