From 7efa626b8b834d6ac9693eaf8fd7a44515fed699 Mon Sep 17 00:00:00 2001 From: moanos Date: Wed, 20 Nov 2024 19:55:16 +0100 Subject: [PATCH] feat: Migrate to new Integer choice field to allow nicer handling --- ...eason_for_signup_alter_user_trust_level.py | 23 +++++++++++++ src/fellchensammlung/models.py | 34 +++++++++---------- src/fellchensammlung/views.py | 12 +++---- 3 files changed, 46 insertions(+), 23 deletions(-) create mode 100644 src/fellchensammlung/migrations/0022_alter_user_reason_for_signup_alter_user_trust_level.py diff --git a/src/fellchensammlung/migrations/0022_alter_user_reason_for_signup_alter_user_trust_level.py b/src/fellchensammlung/migrations/0022_alter_user_reason_for_signup_alter_user_trust_level.py new file mode 100644 index 0000000..0272889 --- /dev/null +++ b/src/fellchensammlung/migrations/0022_alter_user_reason_for_signup_alter_user_trust_level.py @@ -0,0 +1,23 @@ +# Generated by Django 5.1.1 on 2024-11-20 18:50 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('fellchensammlung', '0021_user_reason_for_signup'), + ] + + operations = [ + migrations.AlterField( + model_name='user', + name='reason_for_signup', + field=models.TextField(help_text="Wir würden gerne wissen warum du dich registriertst, ob du dich z.B. Tiere eines bestimmten Tierheim einstellen willst 'nur mal gucken' willst. Beides ist toll! Wenn du für ein Tierheim/eine Pflegestelle arbeitest kontaktieren wir dich ggf. um dir erweiterte Rechte zu geben.", verbose_name='Grund für die Registrierung'), + ), + migrations.AlterField( + model_name='user', + name='trust_level', + field=models.IntegerField(choices=[(1, 'Member'), (2, 'Coordinator'), (3, 'Moderator'), (4, 'Admin')], default=1), + ), + ] diff --git a/src/fellchensammlung/models.py b/src/fellchensammlung/models.py index 142b2a6..d59c922 100644 --- a/src/fellchensammlung/models.py +++ b/src/fellchensammlung/models.py @@ -121,6 +121,17 @@ class RescueOrganization(models.Model): return AdoptionNotice.objects.filter(organization=self) +# Admins can perform all actions and have the highest trust associated with them +# Moderators can make moderation decisions regarding the deletion of content +# Coordinators can create adoption notices without them being checked +# Members can create adoption notices that must be activated +class TrustLevel(models.IntegerChoices): + MEMBER = 1, 'Member' + COORDINATOR = 2, 'Coordinator' + MODERATOR = 3, 'Moderator' + ADMIN = 4, 'Admin' + + class User(AbstractUser): """ Model that holds a user's profile, including the django user model @@ -128,28 +139,17 @@ class User(AbstractUser): The trust levels act as permission system and can be displayed as a badge for the user """ - # Admins can perform all actions and have the highest trust associated with them - # Moderators can make moderation decisions regarding the deletion of content - # Coordinators can create adoption notices without them being checked - # Members can create adoption notices that must be activated - ADMIN = "admin" - MODERATOR = "Moderator" - COORDINATOR = "Koordinator*in" - MEMBER = "Mitglied" - TRUST_LEVEL = { - ADMIN: 4, - MODERATOR: 3, - COORDINATOR: 2, - MEMBER: 1, - } - + trust_level = models.IntegerField( + choices=TrustLevel.choices, + default=TrustLevel.MEMBER, # Default to the lowest trust level + ) preferred_language = models.ForeignKey(Language, on_delete=models.PROTECT, null=True, blank=True, verbose_name=_('Bevorzugte Sprache')) - trust_level = models.IntegerField(choices=TRUST_LEVEL, default=TRUST_LEVEL[MEMBER]) updated_at = models.DateTimeField(auto_now=True) organization_affiliation = models.ForeignKey(RescueOrganization, on_delete=models.PROTECT, null=True, blank=True, verbose_name=_('Organisation')) - reason_for_signup = models.TextField(verbose_name=_("Grund für die Registrierung"), help_text=_("Wir würden gerne wissen warum du dich registriertst, ob du dich z.B. Tiere eines bestimmten Tierheim einstellen willst 'nur mal gucken' willst. Beides ist toll! Wenn du für ein Tierheim/eine Pflegestelle arbeitest kontaktieren wir dich ggf. um dir erweiterte Rechte zu geben.")) + reason_for_signup = models.TextField(verbose_name=_("Grund für die Registrierung"), help_text=_( + "Wir würden gerne wissen warum du dich registriertst, ob du dich z.B. Tiere eines bestimmten Tierheim einstellen willst 'nur mal gucken' willst. Beides ist toll! Wenn du für ein Tierheim/eine Pflegestelle arbeitest kontaktieren wir dich ggf. um dir erweiterte Rechte zu geben.")) REQUIRED_FIELDS = ["reason_for_signup", "email"] class Meta: diff --git a/src/fellchensammlung/views.py b/src/fellchensammlung/views.py index 88d5309..bcb5893 100644 --- a/src/fellchensammlung/views.py +++ b/src/fellchensammlung/views.py @@ -16,7 +16,7 @@ from notfellchen import settings from fellchensammlung import logger from .models import AdoptionNotice, Text, Animal, Rule, Image, Report, ModerationAction, \ User, Location, AdoptionNoticeStatus, Subscriptions, CommentNotification, BaseNotification, RescueOrganization, \ - Species, Log, Timestamp + Species, Log, Timestamp, TrustLevel from .forms import AdoptionNoticeForm, AdoptionNoticeFormWithDateWidget, ImageForm, ReportAdoptionNoticeForm, \ CommentForm, ReportCommentForm, AnimalForm, \ AdoptionNoticeSearchForm, AnimalFormWithDateWidget, AdoptionNoticeFormWithDateWidgetAutoAnimal @@ -28,16 +28,16 @@ from .tasks import add_adoption_notice_location from rest_framework.authtoken.models import Token -def user_is_trust_level_or_above(user, trust_level=User.MODERATOR): - return user.is_authenticated and user.trust_level >= User.TRUST_LEVEL[trust_level] +def user_is_trust_level_or_above(user, trust_level=TrustLevel.MODERATOR): + return user.is_authenticated and user.trust_level >= trust_level -def user_is_owner_or_trust_level(user, django_object, trust_level=User.MODERATOR): +def user_is_owner_or_trust_level(user, django_object, trust_level=TrustLevel.MODERATOR): return user.is_authenticated and ( - user.trust_level == User.TRUST_LEVEL[trust_level] or django_object.owner == user) + user.trust_level == trust_level or django_object.owner == user) -def fail_if_user_not_owner_or_trust_level(user, django_object, trust_level=User.MODERATOR): +def fail_if_user_not_owner_or_trust_level(user, django_object, trust_level=TrustLevel.MODERATOR): if not user_is_owner_or_trust_level(user, django_object, trust_level): raise PermissionDenied