Skip to content

Commit

Permalink
Merge pull request #811 from hackerspace-ntnu/fix-possible-responsibles
Browse files Browse the repository at this point in the history
fix multiple users as responsible field bug after merge conflict
  • Loading branch information
michaelbrusegard authored Apr 11, 2024
2 parents e6f771f + 3feeb48 commit ac91eae
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
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

0 comments on commit ac91eae

Please sign in to comment.