From e4cbd1fcbd0f1f2bd410bc93cf98f02a58a864af Mon Sep 17 00:00:00 2001
From: Steven H Johnson <shjohnson.pi@gmail.com>
Date: Thu, 23 Jun 2016 22:11:17 -0400
Subject: [PATCH] Get latest tourn date year as default for import forms

---
 usta/admin.py        | 26 +++++++++++++++++++++++++-
 usta/admin_import.py |  7 ++++++-
 2 files changed, 31 insertions(+), 2 deletions(-)

diff --git a/usta/admin.py b/usta/admin.py
index 87f9df8..0c52462 100644
--- a/usta/admin.py
+++ b/usta/admin.py
@@ -6,6 +6,7 @@ from usta.models import (
     Section,
     TennisPlayer,
     TournamentData,
+    TournamentDate,
     PlayerMatch,
     SiteSettings,
     Match,
@@ -80,6 +81,18 @@ class TennisPlayerAdmin(ImportMixin, admin.ModelAdmin):
             },
         }
 
+    def get_initial_for_action(self, action):
+        if action.name in ['all', 'status']:
+            year = None
+            try:
+                latest = TournamentDate.objects.latest('start_date')
+                year = latest.start_date.year
+            except TournamentDate.DoesNotExist:
+                pass
+            return {'year': year}
+
+        return super(TennisPlayerAdmin, self).get_initial_for_action(action)
+
     def import_action_validate_context(self, action, form, context):
         """Use for both 'all' and 'status' actions"""
         if form.is_valid():
@@ -151,7 +164,7 @@ class MatchRoundListFilter(admin.SimpleListFilter):
 
 
 class TournamentDateAdmin(admin.ModelAdmin):
-    """Simple TournmantDate Admin"""
+    """Simple TournamentDate Admin"""
     list_display = ['start_date', 'end_date']
     form = TournamentDateAdminForm
 
@@ -180,6 +193,17 @@ class MatchAdmin(ImportMixin, admin.ModelAdmin):
             },
         }
 
+    def get_initial_for_action(self, action):
+        if action.name == 'matches':
+            year = None
+            try:
+                latest = TournamentDate.objects.latest('start_date')
+                year = latest.start_date.year
+            except TournamentDate.DoesNotExist:
+                pass
+            return {'year': year}
+
+        return super(MatchAdmin, self).get_initial_for_action(action)
 
     def show_info(self, request, queryset):
         rows_updated = queryset.update(show_info=True)
diff --git a/usta/admin_import.py b/usta/admin_import.py
index 6af4676..bf0e1dd 100644
--- a/usta/admin_import.py
+++ b/usta/admin_import.py
@@ -217,7 +217,8 @@ class ImportMixin:
         """Create the import form and return template view"""
         action = self._get_action_or_404(action_name)
 
-        form = action.import_form(request.POST or None, request.FILES or None)
+        form = action.import_form(request.POST or None, request.FILES or None,
+            initial=self.get_initial_for_action(action))
 
         context = {}
         context.update(self.admin_site.each_context(request))
@@ -398,3 +399,7 @@ class ImportMixin:
         the number of valid entries that will be imported.
         """
         return context
+
+    def get_initial_for_action(self, action):
+        """Override to provide initial values for the given action form"""
+        return {}
-- 
GitLab