diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index 6c1c54ff4a..c1dbc63c76 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -140,12 +140,10 @@ class Field(object): def get_db_prep_lookup(self, lookup_type, value): "Returns field's value prepared for database lookup." - if lookup_type in ('exact', 'gt', 'gte', 'lt', 'lte', 'ne', 'month', 'day'): + if lookup_type in ('exact', 'gt', 'gte', 'lt', 'lte', 'ne', 'year', 'month', 'day'): return [value] elif lookup_type in ('range', 'in'): return value - elif lookup_type == 'year': - return ['%s-01-01' % value, '%s-12-31' % value] elif lookup_type in ('contains', 'icontains'): return ["%%%s%%" % prep_for_like_query(value)] elif lookup_type == 'iexact': diff --git a/django/db/models/query.py b/django/db/models/query.py index 408fe1ff47..e0a7bdc266 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -516,9 +516,9 @@ def get_where_clause(lookup_type, table_prefix, field_name, value): pass if lookup_type == 'in': return '%s%s IN (%s)' % (table_prefix, field_name, ','.join(['%s' for v in value])) - elif lookup_type in ('range', 'year'): + elif lookup_type == 'range': return '%s%s BETWEEN %%s AND %%s' % (table_prefix, field_name) - elif lookup_type in ('month', 'day'): + elif lookup_type in ('year', 'month', 'day'): return "%s = %%s" % backend.get_date_extract_sql(lookup_type, table_prefix + field_name) elif lookup_type == 'isnull': return "%s%s IS %sNULL" % (table_prefix, field_name, (not value and 'NOT ' or '')) diff --git a/django/views/generic/date_based.py b/django/views/generic/date_based.py index f2367d83cb..05d3e27ede 100644 --- a/django/views/generic/date_based.py +++ b/django/views/generic/date_based.py @@ -43,7 +43,8 @@ def archive_index(request, queryset, date_field, num_latest=15, return HttpResponse(t.render(c)) def archive_year(request, year, queryset, date_field, template_name=None, - template_loader=loader, extra_context={}, context_processors=None): + template_loader=loader, extra_context={}, allow_empty=False, + context_processors=None): """ Generic yearly archive view. @@ -61,7 +62,7 @@ def archive_year(request, year, queryset, date_field, template_name=None, if int(year) >= now.year: lookup_kwargs['%s__lte' % date_field] = now date_list = queryset.filter(**lookup_kwargs).dates(date_field, 'month') - if not date_list: + if not date_list and not allow_empty: raise Http404 if not template_name: template_name = "%s/%s_archive_year" % (model._meta.app_label, model._meta.object_name.lower()) @@ -79,7 +80,7 @@ def archive_year(request, year, queryset, date_field, template_name=None, def archive_month(request, year, month, queryset, date_field, month_format='%b', template_name=None, template_loader=loader, - extra_context={}, context_processors=None): + extra_context={}, allow_empty=False, context_processors=None): """ Generic monthly archive view. @@ -112,7 +113,7 @@ def archive_month(request, year, month, queryset, date_field, if last_day >= now.date(): lookup_kwargs['%s__lte' % date_field] = now object_list = queryset.filter(**lookup_kwargs) - if not object_list: + if not object_list and not allow_empty: raise Http404 if not template_name: template_name = "%s/%s_archive_month" % (model._meta.app_label, model._meta.object_name.lower()) diff --git a/docs/cache.txt b/docs/cache.txt index aaceb228d2..58705c23c0 100644 --- a/docs/cache.txt +++ b/docs/cache.txt @@ -5,7 +5,7 @@ Django's cache framework So, you got slashdotted_. Now what? Django's cache framework gives you three methods of caching dynamic pages in -memory or in a database. You can cache the output of entire pages, you can +memory or in a database. You can cache the output of specific views, you can cache only the pieces that are difficult to produce, or you can cache your entire site. @@ -122,7 +122,7 @@ See the `middleware documentation`_ for more on middleware. .. _`middleware documentation`: http://www.djangoproject.com/documentation/middleware/ -The per-page cache +The per-view cache ================== A more granular way to use the caching framework is by caching the output of @@ -152,8 +152,8 @@ The low-level cache API Sometimes, however, caching an entire rendered page doesn't gain you very much. For example, you may find it's only necessary to cache the result of an -intensive database. In cases like this, you can use the low-level cache API to -store objects in the cache with any level of granularity you like. +intensive database query. In cases like this, you can use the low-level cache +API to store objects in the cache with any level of granularity you like. The cache API is simple:: diff --git a/docs/generic_views.txt b/docs/generic_views.txt index d677a94bb3..90f40646ee 100644 --- a/docs/generic_views.txt +++ b/docs/generic_views.txt @@ -166,6 +166,8 @@ The date-based generic functions are: Yearly archive. Requires that the ``year`` argument be present in the URL pattern. + Takes an optional ``allow_empty`` parameter, as ``archive_index``. + Uses the template ``/_archive_year`` by default. Has the following template context: @@ -185,6 +187,8 @@ The date-based generic functions are: default, which is a three-letter month abbreviation. To change it to use numbers, use ``"%m"``. + Takes an optional ``allow_empty`` parameter, as ``archive_index``. + Uses the template ``/_archive_month`` by default. Has the following template context: diff --git a/docs/outputting_pdf.txt b/docs/outputting_pdf.txt index 3b7d6b0909..37e2779032 100644 --- a/docs/outputting_pdf.txt +++ b/docs/outputting_pdf.txt @@ -10,11 +10,12 @@ The advantage of generating PDF files dynamically is that you can create customized PDFs for different purposes -- say, for different users or different pieces of content. -For example, Django was used at kusports.com to generate customized, +For example, Django was used at kusports.com_ to generate customized, printer-friendly NCAA tournament brackets, as PDF files, for people participating in a March Madness contest. .. _ReportLab: http://www.reportlab.org/rl_toolkit.html +.. _kusports.com: http://www.kusports.com/ Install ReportLab ================= @@ -79,6 +80,15 @@ mention: whatever you want. It'll be used by browsers in the "Save as..." dialogue, etc. + * The ``Content-Disposition`` header starts with ``'attachment; '`` in this + example. This forces Web browsers to pop-up a dialog box + prompting/confirming how to handle the document even if a default is set + on the machine. If you leave off ``'attachment;'``, browsers will handle + the PDF using whatever program/plugin they've been configured to use for + PDFs. Here's what that code would look like:: + + response['Content-Disposition'] = 'filename=somefilename.pdf' + * Hooking into the ReportLab API is easy: Just pass ``response`` as the first argument to ``canvas.Canvas``. The ``Canvas`` class expects a file-like object, and ``HttpResponse`` objects fit the bill. @@ -88,3 +98,56 @@ mention: * Finally, it's important to call ``showPage()`` and ``save()`` on the PDF file. + +Complex PDFs +============ + +If you're creating a complex PDF document with ReportLab, consider using the +cStringIO_ library as a temporary holding place for your PDF file. The +cStringIO library provides a file-like object interface that is particularly +efficient. Here's the above "Hello World" example rewritten to use +``cStringIO``:: + + from cStringIO import StringIO + from reportlab.pdfgen import canvas + from django.utils.httpwrappers import HttpResponse + + def some_view(request): + # Create the HttpResponse object with the appropriate PDF headers. + response = HttpResponse(mimetype='application/pdf') + response['Content-Disposition'] = 'attachment; filename=somefilename.pdf' + + buffer = String() + + # Create the PDF object, using the StringIO object as its "file." + p = canvas.Canvas(buffer) + + # Draw things on the PDF. Here's where the PDF generation happens. + # See the ReportLab documentation for the full list of functionality. + p.drawString(100, 100, "Hello world.") + + # Close the PDF object cleanly. + p.showPage() + p.save() + + # Get the value of the StringIO buffer and write it to the response. + pdf = buffer.getvalue() + buffer.close() + response.write(pdf) + return response + +.. _cStringIO: http://www.python.org/doc/current/lib/module-cStringIO.html + +Further resources +================= + + * PDFlib_ is another PDF-generation library that has Python bindings. To + use it with Django, just use the same concepts explained in this article. + * HTMLdoc_ is a command-line script that can convert HTML to PDF. It + doesn't have a Python interface, but you can escape out to the shell + using ``system`` or ``popen`` and retrieve the output in Python. + * `forge_fdf in Python`_ is a library that fills in PDF forms. + +.. _PDFlib: http://www.pdflib.org/ +.. _HTMLdoc: http://www.htmldoc.org/ +.. _forge_fdf in Python: http://www.accesspdf.com/article.php/20050421092951834 diff --git a/tests/runtests.py b/tests/runtests.py index 4a5e5d8a3c..e88ffd933c 100755 --- a/tests/runtests.py +++ b/tests/runtests.py @@ -241,7 +241,7 @@ class TestRunner: if __name__ == "__main__": from optparse import OptionParser usage = "%prog [options] [model model model ...]" - parser = OptionParser() + parser = OptionParser(usage=usage) parser.add_option('-v', help='How verbose should the output be? Choices are 0, 1 and 2, where 2 is most verbose. Default is 0.', type='choice', choices=['0', '1', '2']) parser.add_option('--settings',