Skip to content

Commit

Permalink
Merge pull request #1125 from kartoza/feature/1104-Hide-Username
Browse files Browse the repository at this point in the history
Update user form in admin
  • Loading branch information
tinashechiraya authored Oct 18, 2024
2 parents 663e2c6 + 13bf6db commit 2fc2dbd
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
28 changes: 27 additions & 1 deletion django_project/minisass_authentication/admin.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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):
Expand Down
26 changes: 25 additions & 1 deletion django_project/minisass_authentication/forms.py
Original file line number Diff line number Diff line change
@@ -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
"""
Expand Down Expand Up @@ -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):
Expand All @@ -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

0 comments on commit 2fc2dbd

Please sign in to comment.