mirror of
https://github.com/django/django.git
synced 2025-07-05 10:19:20 +00:00
magic-removal: Fixed #328 -- Added archive_week generic view
git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2743 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
923c1d304b
commit
0f89743539
@ -57,7 +57,9 @@ def archive_year(request, year, queryset, date_field, template_name=None,
|
|||||||
"""
|
"""
|
||||||
model = queryset.model
|
model = queryset.model
|
||||||
now = datetime.datetime.now()
|
now = datetime.datetime.now()
|
||||||
|
|
||||||
lookup_kwargs = {'%s__year' % date_field: year}
|
lookup_kwargs = {'%s__year' % date_field: year}
|
||||||
|
|
||||||
# Only bother to check current date if the year isn't in the past.
|
# Only bother to check current date if the year isn't in the past.
|
||||||
if int(year) >= now.year:
|
if int(year) >= now.year:
|
||||||
lookup_kwargs['%s__lte' % date_field] = now
|
lookup_kwargs['%s__lte' % date_field] = now
|
||||||
@ -103,6 +105,7 @@ def archive_month(request, year, month, queryset, date_field,
|
|||||||
|
|
||||||
model = queryset.model
|
model = queryset.model
|
||||||
now = datetime.datetime.now()
|
now = datetime.datetime.now()
|
||||||
|
|
||||||
# Calculate first and last day of month, for use in a date-range lookup.
|
# Calculate first and last day of month, for use in a date-range lookup.
|
||||||
first_day = date.replace(day=1)
|
first_day = date.replace(day=1)
|
||||||
if first_day.month == 12:
|
if first_day.month == 12:
|
||||||
@ -110,6 +113,7 @@ def archive_month(request, year, month, queryset, date_field,
|
|||||||
else:
|
else:
|
||||||
last_day = first_day.replace(month=first_day.month + 1)
|
last_day = first_day.replace(month=first_day.month + 1)
|
||||||
lookup_kwargs = {'%s__range' % date_field: (first_day, last_day)}
|
lookup_kwargs = {'%s__range' % date_field: (first_day, last_day)}
|
||||||
|
|
||||||
# Only bother to check current date if the month isn't in the past.
|
# Only bother to check current date if the month isn't in the past.
|
||||||
if last_day >= now.date():
|
if last_day >= now.date():
|
||||||
lookup_kwargs['%s__lte' % date_field] = now
|
lookup_kwargs['%s__lte' % date_field] = now
|
||||||
@ -132,6 +136,53 @@ def archive_month(request, year, month, queryset, date_field,
|
|||||||
c[key] = value
|
c[key] = value
|
||||||
return HttpResponse(t.render(c))
|
return HttpResponse(t.render(c))
|
||||||
|
|
||||||
|
def archive_week(request, year, week, queryset, date_field,
|
||||||
|
template_name=None, template_loader=loader,
|
||||||
|
extra_context={}, allow_empty=True, context_processors=None,
|
||||||
|
template_object_name='object'):
|
||||||
|
"""
|
||||||
|
Generic weekly archive view.
|
||||||
|
|
||||||
|
Templates: ``<app_label>/<model_name>_archive_week.html``
|
||||||
|
Context:
|
||||||
|
week:
|
||||||
|
(date) this week
|
||||||
|
object_list:
|
||||||
|
list of objects published in the given week
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
date = datetime.date(*time.strptime(year+'-0-'+week, '%Y-%w-%U')[:3])
|
||||||
|
except ValueError:
|
||||||
|
raise Http404
|
||||||
|
|
||||||
|
model = queryset.model
|
||||||
|
now = datetime.datetime.now()
|
||||||
|
|
||||||
|
# Calculate first and last day of week, for use in a date-range lookup.
|
||||||
|
first_day = date
|
||||||
|
last_day = date + datetime.timedelta(days=7)
|
||||||
|
lookup_kwargs = {'%s__range' % date_field: (first_day, last_day)}
|
||||||
|
|
||||||
|
# Only bother to check current date if the week isn't in the past.
|
||||||
|
if last_day >= now.date():
|
||||||
|
lookup_kwargs['%s__lte' % date_field] = now
|
||||||
|
object_list = queryset.filter(**lookup_kwargs)
|
||||||
|
if not object_list and not allow_empty:
|
||||||
|
raise Http404
|
||||||
|
if not template_name:
|
||||||
|
template_name = "%s/%s_archive_week.html" % (model._meta.app_label, model._meta.object_name.lower())
|
||||||
|
t = template_loader.get_template(template_name)
|
||||||
|
c = RequestContext(request, {
|
||||||
|
'%s_list' % template_object_name: object_list,
|
||||||
|
'week': date,
|
||||||
|
})
|
||||||
|
for key, value in extra_context.items():
|
||||||
|
if callable(value):
|
||||||
|
c[key] = value()
|
||||||
|
else:
|
||||||
|
c[key] = value
|
||||||
|
return HttpResponse(t.render(c))
|
||||||
|
|
||||||
def archive_day(request, year, month, day, queryset, date_field,
|
def archive_day(request, year, month, day, queryset, date_field,
|
||||||
month_format='%b', day_format='%d', template_name=None,
|
month_format='%b', day_format='%d', template_name=None,
|
||||||
template_loader=loader, extra_context={}, allow_empty=False,
|
template_loader=loader, extra_context={}, allow_empty=False,
|
||||||
@ -157,9 +208,11 @@ def archive_day(request, year, month, day, queryset, date_field,
|
|||||||
|
|
||||||
model = queryset.model
|
model = queryset.model
|
||||||
now = datetime.datetime.now()
|
now = datetime.datetime.now()
|
||||||
|
|
||||||
lookup_kwargs = {
|
lookup_kwargs = {
|
||||||
'%s__range' % date_field: (datetime.datetime.combine(date, datetime.time.min), datetime.datetime.combine(date, datetime.time.max)),
|
'%s__range' % date_field: (datetime.datetime.combine(date, datetime.time.min), datetime.datetime.combine(date, datetime.time.max)),
|
||||||
}
|
}
|
||||||
|
|
||||||
# Only bother to check current date if the date isn't in the past.
|
# Only bother to check current date if the date isn't in the past.
|
||||||
if date >= now.date():
|
if date >= now.date():
|
||||||
lookup_kwargs['%s__lte' % date_field] = now
|
lookup_kwargs['%s__lte' % date_field] = now
|
||||||
@ -214,9 +267,11 @@ def object_detail(request, year, month, day, queryset, date_field,
|
|||||||
|
|
||||||
model = queryset.model
|
model = queryset.model
|
||||||
now = datetime.datetime.now()
|
now = datetime.datetime.now()
|
||||||
|
|
||||||
lookup_kwargs = {
|
lookup_kwargs = {
|
||||||
'%s__range' % date_field: (datetime.datetime.combine(date, datetime.time.min), datetime.datetime.combine(date, datetime.time.max)),
|
'%s__range' % date_field: (datetime.datetime.combine(date, datetime.time.min), datetime.datetime.combine(date, datetime.time.max)),
|
||||||
}
|
}
|
||||||
|
|
||||||
# Only bother to check current date if the date isn't in the past.
|
# Only bother to check current date if the date isn't in the past.
|
||||||
if date >= now.date():
|
if date >= now.date():
|
||||||
lookup_kwargs['%s__lte' % date_field] = now
|
lookup_kwargs['%s__lte' % date_field] = now
|
||||||
|
@ -213,6 +213,27 @@ The date-based generic functions are:
|
|||||||
parameter. (See above.) For example, if ``template_object_name`` is
|
parameter. (See above.) For example, if ``template_object_name`` is
|
||||||
``foo``, the variable will be ``foo_list``.
|
``foo``, the variable will be ``foo_list``.
|
||||||
|
|
||||||
|
``archive_week``
|
||||||
|
Weekly archive. Requires that ``year`` and ``week`` arguments be given. The
|
||||||
|
``week`` argument should be an integer (as a string) representing the week
|
||||||
|
number, where weeks start with Sunday.
|
||||||
|
|
||||||
|
Takes an optional ``template_object_name`` parameter, which designates the
|
||||||
|
name of the template variable to use. Default is ``'object'``.
|
||||||
|
|
||||||
|
Uses the template ``<app_label>/<model_name>_archive_week.html`` by default.
|
||||||
|
|
||||||
|
Has the following template context:
|
||||||
|
|
||||||
|
``object_list``
|
||||||
|
List of objects published on the given day.
|
||||||
|
|
||||||
|
You can change this variable name from ``object_list`` by using the
|
||||||
|
``template_object_name`` parameter. (See above.) For example, if
|
||||||
|
``template_object_name`` is ``foo``, the variable will be ``foo_list``.
|
||||||
|
``week``
|
||||||
|
The first day of the given week (a datetime.datetime object)
|
||||||
|
|
||||||
``archive_day``
|
``archive_day``
|
||||||
Daily archive. Requires that ``year``, ``month``, and ``day`` arguments be
|
Daily archive. Requires that ``year``, ``month``, and ``day`` arguments be
|
||||||
given.
|
given.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user