Changeset 2339
- Timestamp:
- 02/18/06 14:24:24 (3 years ago)
- Files:
-
- django/branches/magic-removal/django/db/models/fields/__init__.py (modified) (1 diff)
- django/branches/magic-removal/django/db/models/query.py (modified) (1 diff)
- django/branches/magic-removal/django/views/generic/date_based.py (modified) (4 diffs)
- django/branches/magic-removal/docs/cache.txt (modified) (3 diffs)
- django/branches/magic-removal/docs/generic_views.txt (modified) (2 diffs)
- django/branches/magic-removal/docs/outputting_pdf.txt (modified) (3 diffs)
- django/branches/magic-removal/tests/runtests.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/magic-removal/django/db/models/fields/__init__.py
r2335 r2339 141 141 def get_db_prep_lookup(self, lookup_type, value): 142 142 "Returns field's value prepared for database lookup." 143 if lookup_type in ('exact', 'gt', 'gte', 'lt', 'lte', 'ne', ' month', 'day'):143 if lookup_type in ('exact', 'gt', 'gte', 'lt', 'lte', 'ne', 'year', 'month', 'day'): 144 144 return [value] 145 145 elif lookup_type in ('range', 'in'): 146 146 return value 147 elif lookup_type == 'year':148 return ['%s-01-01' % value, '%s-12-31' % value]149 147 elif lookup_type in ('contains', 'icontains'): 150 148 return ["%%%s%%" % prep_for_like_query(value)] django/branches/magic-removal/django/db/models/query.py
r2314 r2339 517 517 if lookup_type == 'in': 518 518 return '%s%s IN (%s)' % (table_prefix, field_name, ','.join(['%s' for v in value])) 519 elif lookup_type in ('range', 'year'):519 elif lookup_type == 'range': 520 520 return '%s%s BETWEEN %%s AND %%s' % (table_prefix, field_name) 521 elif lookup_type in (' month', 'day'):521 elif lookup_type in ('year', 'month', 'day'): 522 522 return "%s = %%s" % backend.get_date_extract_sql(lookup_type, table_prefix + field_name) 523 523 elif lookup_type == 'isnull': django/branches/magic-removal/django/views/generic/date_based.py
r2324 r2339 44 44 45 45 def archive_year(request, year, queryset, date_field, template_name=None, 46 template_loader=loader, extra_context={}, context_processors=None): 46 template_loader=loader, extra_context={}, allow_empty=False, 47 context_processors=None): 47 48 """ 48 49 Generic yearly archive view. … … 62 63 lookup_kwargs['%s__lte' % date_field] = now 63 64 date_list = queryset.filter(**lookup_kwargs).dates(date_field, 'month') 64 if not date_list :65 if not date_list and not allow_empty: 65 66 raise Http404 66 67 if not template_name: … … 80 81 def archive_month(request, year, month, queryset, date_field, 81 82 month_format='%b', template_name=None, template_loader=loader, 82 extra_context={}, context_processors=None):83 extra_context={}, allow_empty=False, context_processors=None): 83 84 """ 84 85 Generic monthly archive view. … … 113 114 lookup_kwargs['%s__lte' % date_field] = now 114 115 object_list = queryset.filter(**lookup_kwargs) 115 if not object_list :116 if not object_list and not allow_empty: 116 117 raise Http404 117 118 if not template_name: django/branches/magic-removal/docs/cache.txt
r1904 r2339 6 6 7 7 Django's cache framework gives you three methods of caching dynamic pages in 8 memory or in a database. You can cache the output of entire pages, you can8 memory or in a database. You can cache the output of specific views, you can 9 9 cache only the pieces that are difficult to produce, or you can cache your 10 10 entire site. … … 123 123 .. _`middleware documentation`: http://www.djangoproject.com/documentation/middleware/ 124 124 125 The per- pagecache125 The per-view cache 126 126 ================== 127 127 … … 153 153 Sometimes, however, caching an entire rendered page doesn't gain you very much. 154 154 For example, you may find it's only necessary to cache the result of an 155 intensive database . In cases like this, you can use the low-level cache API to156 store objects in the cache with any level of granularity you like.155 intensive database query. In cases like this, you can use the low-level cache 156 API to store objects in the cache with any level of granularity you like. 157 157 158 158 The cache API is simple:: django/branches/magic-removal/docs/generic_views.txt
r2324 r2339 167 167 pattern. 168 168 169 Takes an optional ``allow_empty`` parameter, as ``archive_index``. 170 169 171 Uses the template ``<app_label>/<model_name>_archive_year`` by default. 170 172 … … 185 187 default, which is a three-letter month abbreviation. To change it to use 186 188 numbers, use ``"%m"``. 189 190 Takes an optional ``allow_empty`` parameter, as ``archive_index``. 187 191 188 192 Uses the template ``<app_label>/<model_name>_archive_month`` by default. django/branches/magic-removal/docs/outputting_pdf.txt
r1916 r2339 11 11 pieces of content. 12 12 13 For example, Django was used at kusports.com to generate customized,13 For example, Django was used at kusports.com_ to generate customized, 14 14 printer-friendly NCAA tournament brackets, as PDF files, for people 15 15 participating in a March Madness contest. 16 16 17 17 .. _ReportLab: http://www.reportlab.org/rl_toolkit.html 18 .. _kusports.com: http://www.kusports.com/ 18 19 19 20 Install ReportLab … … 80 81 dialogue, etc. 81 82 83 * The ``Content-Disposition`` header starts with ``'attachment; '`` in this 84 example. This forces Web browsers to pop-up a dialog box 85 prompting/confirming how to handle the document even if a default is set 86 on the machine. If you leave off ``'attachment;'``, browsers will handle 87 the PDF using whatever program/plugin they've been configured to use for 88 PDFs. Here's what that code would look like:: 89 90 response['Content-Disposition'] = 'filename=somefilename.pdf' 91 82 92 * Hooking into the ReportLab API is easy: Just pass ``response`` as the 83 93 first argument to ``canvas.Canvas``. The ``Canvas`` class expects a … … 89 99 * Finally, it's important to call ``showPage()`` and ``save()`` on the PDF 90 100 file. 101 102 Complex PDFs 103 ============ 104 105 If you're creating a complex PDF document with ReportLab, consider using the 106 cStringIO_ library as a temporary holding place for your PDF file. The 107 cStringIO library provides a file-like object interface that is particularly 108 efficient. Here's the above "Hello World" example rewritten to use 109 ``cStringIO``:: 110 111 from cStringIO import StringIO 112 from reportlab.pdfgen import canvas 113 from django.utils.httpwrappers import HttpResponse 114 115 def some_view(request): 116 # Create the HttpResponse object with the appropriate PDF headers. 117 response = HttpResponse(mimetype='application/pdf') 118 response['Content-Disposition'] = 'attachment; filename=somefilename.pdf' 119 120 buffer = String() 121 122 # Create the PDF object, using the StringIO object as its "file." 123 p = canvas.Canvas(buffer) 124 125 # Draw things on the PDF. Here's where the PDF generation happens. 126 # See the ReportLab documentation for the full list of functionality. 127 p.drawString(100, 100, "Hello world.") 128 129 # Close the PDF object cleanly. 130 p.showPage() 131 p.save() 132 133 # Get the value of the StringIO buffer and write it to the response. 134 pdf = buffer.getvalue() 135 buffer.close() 136 response.write(pdf) 137 return response 138 139 .. _cStringIO: http://www.python.org/doc/current/lib/module-cStringIO.html 140 141 Further resources 142 ================= 143 144 * PDFlib_ is another PDF-generation library that has Python bindings. To 145 use it with Django, just use the same concepts explained in this article. 146 * HTMLdoc_ is a command-line script that can convert HTML to PDF. It 147 doesn't have a Python interface, but you can escape out to the shell 148 using ``system`` or ``popen`` and retrieve the output in Python. 149 * `forge_fdf in Python`_ is a library that fills in PDF forms. 150 151 .. _PDFlib: http://www.pdflib.org/ 152 .. _HTMLdoc: http://www.htmldoc.org/ 153 .. _forge_fdf in Python: http://www.accesspdf.com/article.php/20050421092951834 django/branches/magic-removal/tests/runtests.py
r2313 r2339 242 242 from optparse import OptionParser 243 243 usage = "%prog [options] [model model model ...]" 244 parser = OptionParser( )244 parser = OptionParser(usage=usage) 245 245 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.', 246 246 type='choice', choices=['0', '1', '2'])
