magic-removal: Merged to [2338]

git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2339 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2006-02-18 20:24:24 +00:00
parent 01732d4f72
commit 4cb8cca158
7 changed files with 81 additions and 15 deletions

View File

@ -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':

View File

@ -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 ''))

View File

@ -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())

View File

@ -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::

View File

@ -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 ``<app_label>/<model_name>_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 ``<app_label>/<model_name>_archive_month`` by default.
Has the following template context:

View File

@ -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

View File

@ -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',