diff --git a/djadmin2/themes/djadmin2theme_default/templates/djadmin2theme_default/model_list.html b/djadmin2/themes/djadmin2theme_default/templates/djadmin2theme_default/model_list.html index b84a3ba..865f88a 100644 --- a/djadmin2/themes/djadmin2theme_default/templates/djadmin2theme_default/model_list.html +++ b/djadmin2/themes/djadmin2theme_default/templates/djadmin2theme_default/model_list.html @@ -36,13 +36,20 @@ {% endif %} -
{% csrf_token %}
+ {% if dates %} + + {% endif %} + {% if view.model_admin.actions_on_top %} {% include 'djadmin2theme_default/includes/list_actions.html' with position='top' %} {% endif %} diff --git a/djadmin2/views.py b/djadmin2/views.py index fe273cb..278c39f 100644 --- a/djadmin2/views.py +++ b/djadmin2/views.py @@ -2,6 +2,7 @@ from __future__ import division, absolute_import, unicode_literals import operator +from collections import OrderedDict from django.contrib.auth import get_user_model from django.contrib.auth.forms import (PasswordChangeForm, @@ -219,15 +220,52 @@ class ModelListView(AdminModel2Mixin, generic.ListView): context['sort_term'] = self.request.GET.get('sort', '') if self.model_admin.date_hierarchy: - date_field = self.model_admin.date_hierarchy - context['years'] = ( - context['object_list'].dates(date_field, "year") - ) - context["days"] = ( - context['object_list'].dates(date_field, "day") - ) + year = self.request.GET.get("year", False) + month = self.request.GET.get("month", False) + day = self.request.GET.get("day", False) + + if year and month and day: + context["dates"] = self._format_days(context) + elif year and month: + context["dates"] = self._format_days(context) + elif year: + context["dates"] = self._format_months(context) + else: + context["dates"] = self._format_years(context) + return context + def _format_years(self, context): + return [ + (("?year=%s" % year.strftime("%Y")), year.strftime("%Y")) + for year in + context['object_list'].dates('published_date', 'year') + ] + + def _format_months(self, context): + return [ + ( + "?year=%s&month=%s" % ( + date.strftime("%Y"), date.strftime("%m") + ), + date.strftime("%B %Y") + ) for date in + context["object_list"].dates('published_date', 'day') + ] + + def _format_days(self, context): + return [ + ( + "?year=%s&month=%s&day=%s" % ( + date.strftime("%Y"), + date.strftime("%m"), + date.strftime("%d"), + ), + date.strftime("%B %d") + ) for date in + context["object_list"].dates('published_date', 'day') + ] + def get_success_url(self): view_name = 'admin2:{}_{}_index'.format( self.app_label, self.model_name) diff --git a/example/blog/models.py b/example/blog/models.py index 348af5b..01a0582 100644 --- a/example/blog/models.py +++ b/example/blog/models.py @@ -11,7 +11,7 @@ class Post(models.Model): title = models.CharField(max_length=255, verbose_name=_('title')) body = models.TextField(verbose_name=_('body')) published = models.BooleanField(default=False, verbose_name=_('published')) - published_date = models.DateField(auto_now=True) + published_date = models.DateField() def __unicode__(self): return self.title