Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix multiple users as responsible field bug after merge conflict #811

Merged
merged 3 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 12 additions & 16 deletions news/forms.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions news/templates/news/edit_event.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<link rel="stylesheet" type="text/css" href="{% static 'news/css/news_style.css' %}">
<link href="{% static 'website/css/select2.min.css' %}" rel="stylesheet" />
<style>
.select-wrapper {
#responsible-input-wrapper .select-wrapper {
display: none;
}
.select2 {
Expand Down Expand Up @@ -91,7 +91,7 @@ <h5>Generelt</h5>
</p>
<span class="helper-text hs-red-text">{{ form.internal.errors }}</span>
</div>
<div class="col s12 input-field">
<div class="col s12 input-field" id="responsible-input-wrapper">
<i class="material-icons prefix">person</i>
<label for="responsible-list">{{ form.responsibles.label }}</label>
<div style="margin-top: 40px; margin-bottom: -20px;" class="helper-text hs-red-text">{{ form.responsibles.errors }}</div>
Expand Down