Skip to content

Commit

Permalink
Update user model to include more information
Browse files Browse the repository at this point in the history
  • Loading branch information
tudoramariei committed Oct 21, 2024
1 parent c5ff2c7 commit cda9a2d
Show file tree
Hide file tree
Showing 5 changed files with 310 additions and 83 deletions.
127 changes: 127 additions & 0 deletions backend/accounts/admin.py
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): ...
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"),
),
]
15 changes: 12 additions & 3 deletions backend/accounts/models.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from django.conf import settings
from django.contrib.auth.models import AbstractUser
from django.contrib.auth.models import AbstractUser, Group
from django.db import models
from django.db.models.functions import Lower
from django.utils.decorators import method_decorator
from django.utils.translation import gettext as _
from model_utils.models import TimeStampedModel

from civil_society_vote.common.cache import cache_decorator

Expand All @@ -14,7 +15,7 @@
NGO_GROUP = "ONG"


class User(AbstractUser):
class User(AbstractUser, TimeStampedModel):
# We ignore the "username" field because we will use the email for the authentication
username = models.CharField(
verbose_name=_("username"),
Expand All @@ -25,7 +26,7 @@ class User(AbstractUser):
null=True,
)
email = models.EmailField(verbose_name=_("email address"), blank=False, null=False, unique=True)
is_ngohub_user = models.BooleanField(default=False)
is_ngohub_user = models.BooleanField(verbose_name=_("is ngo hub user"), default=False)

organization = models.ForeignKey(
"hub.Organization",
Expand Down Expand Up @@ -67,3 +68,11 @@ def in_commission_groups(self):
@method_decorator(cache_decorator(timeout=settings.TIMEOUT_CACHE_NORMAL, cache_key_prefix="staff_groups"))
def in_staff_groups(self):
return self.groups.filter(name__in=[STAFF_GROUP, SUPPORT_GROUP]).exists()


class GroupProxy(Group):
class Meta:
proxy = True

verbose_name = _("Group")
verbose_name_plural = _("Groups")
Loading

0 comments on commit cda9a2d

Please sign in to comment.