diff --git a/django_project/minisass_authentication/admin.py b/django_project/minisass_authentication/admin.py index dfafe3b48..cd230ccc7 100644 --- a/django_project/minisass_authentication/admin.py +++ b/django_project/minisass_authentication/admin.py @@ -1,7 +1,11 @@ from django.contrib import admin -from minisass_authentication.models import Lookup, UserProfile, PasswordHistory from django.contrib.auth.admin import UserAdmin as BaseUserAdmin from django.contrib.auth.models import User +from django.contrib.auth.forms import UserCreationForm +from minisass_authentication.models import Lookup, UserProfile, PasswordHistory +from minisass_authentication.forms import CustomUserAdminForm +from django import forms + @admin.register(Lookup) class LookupAdmin(admin.ModelAdmin): @@ -43,11 +47,33 @@ def correct_country(modeladmin, request, queryset): class UserAdmin(BaseUserAdmin): inlines = (UserProfileInline, ) + list_display = ( + "email", "first_name", "last_name", "is_staff" + ) list_filter = ( 'userprofile__expert_approval_status', 'userprofile__is_expert', 'is_staff', 'is_superuser', 'is_active' ) actions = [correct_country] + exclude = ('username',) + fieldsets = ( + ('Personal info', {'fields': ('first_name', 'last_name', 'email', 'password')}), + ('Important dates', {'fields': ('last_login', 'date_joined')}), + ('Permissions', {'fields': ('is_active', 'is_staff', 'is_superuser', 'groups', 'user_permissions')}), + ) + # Optionally customize the fields displayed in the user detail form (second step) + add_fieldsets = ( + (None, { + 'classes': ('wide',), + 'fields': ('email', 'password1', 'password2'), + }), + ) + + # Make sure email is used instead of username + def save_model(self, request, obj, form, change): + if not change: + obj.username = obj.email + super().save_model(request, obj, form, change) class PasswordHistoryAdmin(admin.ModelAdmin): diff --git a/django_project/minisass_authentication/forms.py b/django_project/minisass_authentication/forms.py index d7119de66..19a68b031 100644 --- a/django_project/minisass_authentication/forms.py +++ b/django_project/minisass_authentication/forms.py @@ -1,28 +1,36 @@ from django import forms +from django.contrib.auth.admin import UserAdmin +from django.contrib.auth.forms import UserCreationForm +from django.contrib.auth.models import User from django.utils.translation import gettext_lazy as _ from minisass_authentication.models import Lookup + def get_organisation_types(): result = [('','-- Select a Type --')] qs = Lookup.objects.filter(container__description='Organisation Type', active=True).order_by('rank', 'description') result.extend([(itm.id, itm.description) for itm in qs]) return result + def get_organisation_names(): return [] + def get_countries(): result = [('','-- Select a Country --')] qs = Lookup.objects.raw("SELECT * FROM minisass_registration_lookup WHERE container_id='8' AND active ='t' ORDER BY rank = 0, rank, description" ) result.extend([(itm.id, itm.description) for itm in qs]) return result + def get_countries_old(): result = [('','-- Select a Country --')] qs = Lookup.objects.filter(container__description='Country', active=True).order_by('rank', 'description') result.extend([(itm.id, itm.description) for itm in qs]) return result + class MiniSASSRegistrationForm(forms.Form): """ Add fields for firstname, lastname, and organisation """ @@ -52,7 +60,8 @@ class MiniSASSRegistrationForm(forms.Form): label=_("Country"), required=False, help_text=_("Please select a country"), - choices=get_countries() + # choices=get_countries() + choices=[] ) def clean(self): @@ -63,3 +72,18 @@ def clean(self): # You can add custom validation here if needed return cleaned_data + + +class CustomUserAdminForm(UserCreationForm): + email = forms.EmailField(required=True) + + class Meta: + model = User + fields = ('email',) + + def save(self, commit=True): + user = super().save(commit=False) + user.username = self.cleaned_data['email'] # Use email as the username + if commit: + user.save() + return user \ No newline at end of file