-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update user model to include more information
- Loading branch information
1 parent
c5ff2c7
commit cda9a2d
Showing
5 changed files
with
310 additions
and
83 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
from django.contrib import admin | ||
from django.contrib.admin import ModelAdmin | ||
from django.contrib.admin.sites import NotRegistered | ||
from django.contrib.auth.admin import GroupAdmin as BaseGroupAdmin | ||
from django.contrib.auth.models import Group as BaseGroup | ||
from django.urls import reverse_lazy | ||
from django.utils.safestring import mark_safe | ||
from django.utils.translation import gettext_lazy as _ | ||
|
||
from .models import User, GroupProxy | ||
|
||
# Remove the default admins for User and Group | ||
try: | ||
admin.site.unregister(User) | ||
except NotRegistered: | ||
pass | ||
|
||
try: | ||
admin.site.unregister(BaseGroup) | ||
except NotRegistered: | ||
pass | ||
|
||
|
||
@admin.register(User) | ||
class UserAdmin(ModelAdmin): | ||
list_display = ( | ||
"email", | ||
"get_organization", | ||
"get_groups", | ||
"is_active", | ||
"is_ngohub_user", | ||
"created", | ||
) | ||
list_display_links = ( | ||
"email", | ||
"is_active", | ||
"is_ngohub_user", | ||
"created", | ||
) | ||
list_filter = ("is_ngohub_user", "is_active", "is_superuser", "is_staff", "groups") | ||
|
||
search_fields = ("email", "first_name", "last_name") | ||
|
||
readonly_fields = ( | ||
"email", | ||
"password", | ||
"organization", | ||
"is_ngohub_user", | ||
"date_joined", | ||
"last_login", | ||
"first_name", | ||
"last_name", | ||
"groups", | ||
"user_permissions", | ||
) | ||
|
||
fieldsets = ( | ||
( | ||
_("Identification"), | ||
{ | ||
"fields": ( | ||
"email", | ||
"is_ngohub_user", | ||
"organization", | ||
"first_name", | ||
"last_name", | ||
) | ||
}, | ||
), | ||
( | ||
_("Flags"), | ||
{ | ||
"fields": ( | ||
"is_active", | ||
"is_staff", | ||
"is_superuser", | ||
) | ||
}, | ||
), | ||
( | ||
_("Permissions"), | ||
{ | ||
"fields": ( | ||
"groups", | ||
"user_permissions", | ||
) | ||
}, | ||
), | ||
( | ||
_("Important dates"), | ||
{ | ||
"fields": ( | ||
"date_joined", | ||
"last_login", | ||
) | ||
}, | ||
), | ||
) | ||
|
||
def get_organization(self, obj: User): | ||
if not obj: | ||
return "-" | ||
|
||
if not obj.organization: | ||
return "-" | ||
|
||
organization_url = reverse_lazy("admin:hub_organization_change", args=[obj.organization.pk]) | ||
return mark_safe(f'<a href="{organization_url}">{obj.organization.name}</a>') | ||
|
||
get_organization.short_description = _("Organization") | ||
|
||
def get_groups(self, obj: User): | ||
user_groups = obj.groups.all() | ||
|
||
group_url = reverse_lazy("admin:accounts_user_changelist") | ||
groups_display = [ | ||
f'<a href="{group_url}?groups__id__exact={group.pk}">{group.name}</a>' for group in user_groups | ||
] | ||
|
||
# noinspection DjangoSafeString | ||
return mark_safe(", ".join(groups_display)) | ||
|
||
get_groups.short_description = _("Groups") | ||
|
||
|
||
@admin.register(GroupProxy) | ||
class GroupAdmin(BaseGroupAdmin, ModelAdmin): ... |
51 changes: 51 additions & 0 deletions
51
backend/accounts/migrations/0011_groupproxy_user_created_user_modified_and_more.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Generated by Django 4.2.16 on 2024-10-21 12:48 | ||
|
||
import django.contrib.auth.models | ||
from django.db import migrations, models | ||
import django.utils.timezone | ||
import model_utils.fields | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("auth", "0012_alter_user_first_name_max_length"), | ||
("accounts", "0010_user_organization"), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="GroupProxy", | ||
fields=[], | ||
options={ | ||
"verbose_name": "Grup", | ||
"verbose_name_plural": "Grupuri", | ||
"proxy": True, | ||
"indexes": [], | ||
"constraints": [], | ||
}, | ||
bases=("auth.group",), | ||
managers=[ | ||
("objects", django.contrib.auth.models.GroupManager()), | ||
], | ||
), | ||
migrations.AddField( | ||
model_name="user", | ||
name="created", | ||
field=model_utils.fields.AutoCreatedField( | ||
default=django.utils.timezone.now, editable=False, verbose_name="created" | ||
), | ||
), | ||
migrations.AddField( | ||
model_name="user", | ||
name="modified", | ||
field=model_utils.fields.AutoLastModifiedField( | ||
default=django.utils.timezone.now, editable=False, verbose_name="modified" | ||
), | ||
), | ||
migrations.AlterField( | ||
model_name="user", | ||
name="is_ngohub_user", | ||
field=models.BooleanField(default=False, verbose_name="este utilizator NGO Hub"), | ||
), | ||
] |
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
Oops, something went wrong.