From a45c9ade057c541f963f1fc797223bb8ad2d5522 Mon Sep 17 00:00:00 2001
From: Jorran de Wit <jorrandewit@outlook.com>
Date: Fri, 25 Jan 2019 08:26:13 +0100
Subject: [PATCH] Improve form field formatting

---
 finances/forms.py   | 7 +++++--
 production/forms.py | 7 ++-----
 scipost/fields.py   | 5 +++++
 3 files changed, 12 insertions(+), 7 deletions(-)

diff --git a/finances/forms.py b/finances/forms.py
index 8e170cfd1..75a320fd0 100644
--- a/finances/forms.py
+++ b/finances/forms.py
@@ -10,6 +10,8 @@ from django.utils import timezone
 
 from ajax_select.fields import AutoCompleteSelectField
 
+from scipost.fields import UserModelChoiceField
+
 from .models import Subsidy, WorkLog
 
 today = timezone.now().date()
@@ -50,7 +52,7 @@ class LogsActiveFilter(forms.Form):
     Filter work logs given the requested date range and users.
     """
 
-    employee = forms.ModelChoiceField(
+    employee = UserModelChoiceField(
         queryset=get_user_model().objects.filter(work_logs__isnull=False), required=False)
     month = forms.ChoiceField(
         choices=[(None, 9 * '-')] + [(k, v) for k, v in MONTHS.items()], required=False)
@@ -74,7 +76,8 @@ class LogsActiveFilter(forms.Form):
         """Filter work logs and return in output-convenient format."""
         output = []
         if self.is_valid():
-            user_qs = get_user_model().objects.filter(work_logs__isnull=False)
+            user_qs = get_user_model().objects.filter(
+                work_logs__isnull=False, work_logs__work_date__year=self.cleaned_data['year'])
             if self.cleaned_data['employee']:
                 # Get as a queryset instead of single instead.
                 user_qs = user_qs.filter(id=self.cleaned_data['employee'].id)
diff --git a/production/forms.py b/production/forms.py
index c84b25a10..afb832597 100644
--- a/production/forms.py
+++ b/production/forms.py
@@ -7,6 +7,8 @@ import datetime
 from django import forms
 from django.contrib.auth import get_user_model
 
+from scipost.fields import UserModelChoiceField
+
 from . import constants
 from .models import ProductionUser, ProductionStream, ProductionEvent, Proofs,\
     ProductionEventAttachment
@@ -127,11 +129,6 @@ class StreamStatusForm(forms.ModelForm):
         return stream
 
 
-class UserModelChoiceField(forms.ModelChoiceField):
-    def label_from_instance(self, obj):
-         return '{}, {} ({})'.format(obj.last_name, obj.first_name, obj.email)
-
-
 class UserToOfficerForm(forms.ModelForm):
     user = UserModelChoiceField(queryset=get_user_model().objects.filter(
         production_user__isnull=True).order_by('last_name'))
diff --git a/scipost/fields.py b/scipost/fields.py
index abdfa395a..e002c69de 100644
--- a/scipost/fields.py
+++ b/scipost/fields.py
@@ -87,3 +87,8 @@ class ReCaptchaField(forms.CharField):
                 self.error_messages['captcha_invalid']
             )
         return values[0]
+
+
+class UserModelChoiceField(forms.ModelChoiceField):
+    def label_from_instance(self, obj):
+         return '{}, {} ({})'.format(obj.last_name, obj.first_name, obj.email)
-- 
GitLab