From 0cc6e9b0719dee592eda9f0172c7d9a844a2b763 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carl=20G=C3=BCtzkow?= <70779496+CJGutz@users.noreply.github.com> Date: Wed, 10 Apr 2024 21:53:18 +0200 Subject: [PATCH] fix multiple users as responsible field bug after merge conflict --- news/forms.py | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/news/forms.py b/news/forms.py index 6b59f11a..d1aa4609 100644 --- a/news/forms.py +++ b/news/forms.py @@ -1,5 +1,6 @@ from django import forms from django.contrib.auth.models import User +from django.db.models import Count from django.db.utils import OperationalError, ProgrammingError from django.forms import inlineformset_factory from django.forms.widgets import ClearableFileInput @@ -111,16 +112,6 @@ def __init__(self, *args, **kwargs): ) -class UserFullnameChoiceField(forms.ModelMultipleChoiceField): - """ - Denne klassen overrider ModelChoiceField for å vise vanlige - fulle navn istedenfor brukernavn - """ - - def label_from_instance(self, obj): - return obj.get_full_name() - - class MaterialFileWidget(ClearableFileInput): template_name = "files/_file_widget.html" @@ -175,16 +166,21 @@ class EventForm(UpdatePubDateOnDraftPublishMixin, forms.ModelForm): registration_end = SplitDateTimeFieldCustom(label="Påmeldingsfrist") deregistration_end = SplitDateTimeFieldCustom(label="Avmeldingsfrist") - responsibles = UserFullnameChoiceField( + responsibles = forms.Field( label=_("Arrangementansvarlig"), - queryset=User.objects.all() - .filter(groups__name__in=get_committees()) - .order_by("first_name"), ) def clean_responsibles(self): - data = self.cleaned_data["responsibles"] - return User.objects.filter(pk__in=data) + """Get users from responsibles data, filter and check if they exist.""" + data = self.data.getlist("responsibles") + users = User.objects.annotate(group_count=Count("groups")).filter( + pk__in=data, group_count__gt=0 + ) + if type(data) is list and users.count() != len(data): + raise forms.ValidationError( + _("Bruker er ikke i en registert gruppe."), code="invalid" + ) + return users class Meta: model = Event