diff --git a/usta/admin.py b/usta/admin.py index 87f9df868b4ff381455f84ebe1205a085874e473..0c5246295ec02bdd68e60cb9b3ef2395142d0d7b 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 6af4676de577566eb92d5398e6993b29ca8e2530..bf0e1dd9252127d9479a3344b1650612033cd344 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 {}