Changeset 2743
- Timestamp:
- 04/23/06 19:18:57 (3 years ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/magic-removal/django/views/generic/date_based.py
r2701 r2743 58 58 model = queryset.model 59 59 now = datetime.datetime.now() 60 60 61 lookup_kwargs = {'%s__year' % date_field: year} 62 61 63 # Only bother to check current date if the year isn't in the past. 62 64 if int(year) >= now.year: … … 104 106 model = queryset.model 105 107 now = datetime.datetime.now() 108 106 109 # Calculate first and last day of month, for use in a date-range lookup. 107 110 first_day = date.replace(day=1) … … 111 114 last_day = first_day.replace(month=first_day.month + 1) 112 115 lookup_kwargs = {'%s__range' % date_field: (first_day, last_day)} 116 113 117 # Only bother to check current date if the month isn't in the past. 114 118 if last_day >= now.date(): … … 126 130 'previous_month': first_day - datetime.timedelta(days=1), 127 131 }, context_processors) 132 for key, value in extra_context.items(): 133 if callable(value): 134 c[key] = value() 135 else: 136 c[key] = value 137 return HttpResponse(t.render(c)) 138 139 def archive_week(request, year, week, queryset, date_field, 140 template_name=None, template_loader=loader, 141 extra_context={}, allow_empty=True, context_processors=None, 142 template_object_name='object'): 143 """ 144 Generic weekly archive view. 145 146 Templates: ``<app_label>/<model_name>_archive_week.html`` 147 Context: 148 week: 149 (date) this week 150 object_list: 151 list of objects published in the given week 152 """ 153 try: 154 date = datetime.date(*time.strptime(year+'-0-'+week, '%Y-%w-%U')[:3]) 155 except ValueError: 156 raise Http404 157 158 model = queryset.model 159 now = datetime.datetime.now() 160 161 # Calculate first and last day of week, for use in a date-range lookup. 162 first_day = date 163 last_day = date + datetime.timedelta(days=7) 164 lookup_kwargs = {'%s__range' % date_field: (first_day, last_day)} 165 166 # Only bother to check current date if the week isn't in the past. 167 if last_day >= now.date(): 168 lookup_kwargs['%s__lte' % date_field] = now 169 object_list = queryset.filter(**lookup_kwargs) 170 if not object_list and not allow_empty: 171 raise Http404 172 if not template_name: 173 template_name = "%s/%s_archive_week.html" % (model._meta.app_label, model._meta.object_name.lower()) 174 t = template_loader.get_template(template_name) 175 c = RequestContext(request, { 176 '%s_list' % template_object_name: object_list, 177 'week': date, 178 }) 128 179 for key, value in extra_context.items(): 129 180 if callable(value): … … 158 209 model = queryset.model 159 210 now = datetime.datetime.now() 211 160 212 lookup_kwargs = { 161 213 '%s__range' % date_field: (datetime.datetime.combine(date, datetime.time.min), datetime.datetime.combine(date, datetime.time.max)), 162 214 } 215 163 216 # Only bother to check current date if the date isn't in the past. 164 217 if date >= now.date(): … … 215 268 model = queryset.model 216 269 now = datetime.datetime.now() 270 217 271 lookup_kwargs = { 218 272 '%s__range' % date_field: (datetime.datetime.combine(date, datetime.time.min), datetime.datetime.combine(date, datetime.time.max)), 219 273 } 274 220 275 # Only bother to check current date if the date isn't in the past. 221 276 if date >= now.date(): django/branches/magic-removal/docs/generic_views.txt
r2701 r2743 213 213 parameter. (See above.) For example, if ``template_object_name`` is 214 214 ``foo``, the variable will be ``foo_list``. 215 216 ``archive_week`` 217 Weekly archive. Requires that ``year`` and ``week`` arguments be given. The 218 ``week`` argument should be an integer (as a string) representing the week 219 number, where weeks start with Sunday. 220 221 Takes an optional ``template_object_name`` parameter, which designates the 222 name of the template variable to use. Default is ``'object'``. 223 224 Uses the template ``<app_label>/<model_name>_archive_week.html`` by default. 225 226 Has the following template context: 227 228 ``object_list`` 229 List of objects published on the given day. 230 231 You can change this variable name from ``object_list`` by using the 232 ``template_object_name`` parameter. (See above.) For example, if 233 ``template_object_name`` is ``foo``, the variable will be ``foo_list``. 234 ``week`` 235 The first day of the given week (a datetime.datetime object) 215 236 216 237 ``archive_day``
