From 4e0a9929ddecd441fae4f579436378c9185572ec Mon Sep 17 00:00:00 2001
From: Jorran de Wit <jorrandewit@outlook.com>
Date: Sun, 1 Oct 2017 14:53:46 +0200
Subject: [PATCH] Auto open time sheet even if not explicity filtered

---
 finance/views.py    |  4 ++--
 production/forms.py | 26 +++++++++++++++-----------
 2 files changed, 17 insertions(+), 13 deletions(-)

diff --git a/finance/views.py b/finance/views.py
index ddfeaadf5..1f46a444d 100644
--- a/finance/views.py
+++ b/finance/views.py
@@ -14,7 +14,7 @@ def timesheets(request):
         'form': form,
     }
 
-    if form.is_valid():
-        context['totals'] = form.get_totals()
+    # if form.is_valid():
+    context['totals'] = form.get_totals()
 
     return render(request, 'finance/timesheets.html', context)
diff --git a/production/forms.py b/production/forms.py
index bba5be4e7..1fda9312f 100644
--- a/production/forms.py
+++ b/production/forms.py
@@ -53,11 +53,23 @@ class ProductionUserMonthlyActiveFilter(forms.Form):
     year = forms.ChoiceField(choices=[(y, y) for y in reversed(range(today.year-6, today.year+1))])
 
     def __init__(self, *args, **kwargs):
+        if not kwargs.get('data', False) and not args[0]:
+            args = list(args)
+            args[0] = {
+                'month': today.month,
+                'year': today.year
+            }
+            args = tuple(args)
+        kwargs['initial'] = {
+            'month': today.month,
+            'year': today.year
+        }
         super().__init__(*args, **kwargs)
-        self.fields['month'].initial = today.month
-        self.fields['year'].initial = today.year
 
     def get_totals(self):
+        # Make accessible without need to explicitly check validity of form.
+        self.is_valid()
+
         users = ProductionUser.objects.filter(events__duration__isnull=False,
                                               events__noted_on__month=self.cleaned_data['month'],
                                               events__noted_on__year=self.cleaned_data['year']
@@ -72,13 +84,5 @@ class ProductionUserMonthlyActiveFilter(forms.Form):
                 'duration': events.aggregate(total=Sum('duration')),
                 'user': user
             })
-            
-        return output
 
-    def get_events(self, officer=None):
-        qs = ProductionEvent.objects.filter(duration__isnull=False,
-                                            noted_on__month=self.cleaned_data['month'],
-                                            noted_on__year=self.cleaned_data['year'])
-        if officer:
-            qs.filter(noted_by=officer)
-        return qs.order_by('noted_by')
+        return output
-- 
GitLab