Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #4125 -- Added some small fixes to contrib.databrowse: fixed ca…

…lendar views, added a summary of the number of objects in each list, and added a template entry point for the customisation enthusiasts. Nice work, SmileyChris.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@5947 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 8d97f15b966d1679849e8b1de7806b0476362f17 1 parent 0a49719
Malcolm Tredinnick authored August 19, 2007
23  django/contrib/databrowse/datastructures.py
@@ -8,6 +8,7 @@
8 8
 from django.utils.text import capfirst
9 9
 from django.utils.translation import get_date_formats
10 10
 from django.utils.encoding import smart_unicode, smart_str, iri_to_uri
  11
+from django.db.models.query import QuerySet
11 12
 
12 13
 EMPTY_VALUE = '(None)'
13 14
 
@@ -30,8 +31,12 @@ def url(self):
30 31
         return '%s%s/%s/' % (self.site.root_url, self.model._meta.app_label, self.model._meta.module_name)
31 32
 
32 33
     def objects(self, **kwargs):
33  
-        for obj in self.model._default_manager.filter(**kwargs):
34  
-            yield EasyInstance(self, obj)
  34
+        return self.get_query_set().filter(**kwargs)
  35
+
  36
+    def get_query_set(self):
  37
+        easy_qs = self.model._default_manager.get_query_set()._clone(klass=EasyQuerySet)
  38
+        easy_qs._easymodel = self
  39
+        return easy_qs
35 40
 
36 41
     def object_by_pk(self, pk):
37 42
         return EasyInstance(self, self.model._default_manager.get(pk=pk))
@@ -194,3 +199,17 @@ def urls(self):
194 199
         else:
195 200
             lst = [(self.values()[0], None)]
196 201
         return lst
  202
+
  203
+class EasyQuerySet(QuerySet):
  204
+    """
  205
+    When creating (or cloning to) an `EasyQuerySet`, make sure to set the
  206
+    `_easymodel` variable to the related `EasyModel`.
  207
+    """
  208
+    def iterator(self, *args, **kwargs):
  209
+        for obj in super(EasyQuerySet, self).iterator(*args, **kwargs):
  210
+            yield EasyInstance(self._easymodel, obj)
  211
+
  212
+    def _clone(self, *args, **kwargs):
  213
+        c = super(EasyQuerySet, self)._clone(*args, **kwargs)
  214
+        c._easymodel = self._easymodel
  215
+        return c
10  django/contrib/databrowse/plugins/calendars.py
@@ -64,22 +64,22 @@ def homepage_view(self, request):
64 64
 
65 65
     def calendar_view(self, request, field, year=None, month=None, day=None):
66 66
         easy_model = EasyModel(self.site, self.model)
  67
+        queryset = easy_model.get_query_set()
67 68
         extra_context = {'root_url': self.site.root_url, 'model': easy_model, 'field': field}
68 69
         if day is not None:
69  
-            # TODO: The objects in this template should be EasyInstances
70  
-            return date_based.archive_day(request, year, month, day, self.model.objects.all(), field.name,
  70
+            return date_based.archive_day(request, year, month, day, queryset, field.name,
71 71
                 template_name='databrowse/calendar_day.html', allow_empty=False, allow_future=True,
72 72
                 extra_context=extra_context)
73 73
         elif month is not None:
74  
-            return date_based.archive_month(request, year, month, self.model.objects.all(), field.name,
  74
+            return date_based.archive_month(request, year, month, queryset, field.name,
75 75
                 template_name='databrowse/calendar_month.html', allow_empty=False, allow_future=True,
76 76
                 extra_context=extra_context)
77 77
         elif year is not None:
78  
-            return date_based.archive_year(request, year, self.model.objects.all(), field.name,
  78
+            return date_based.archive_year(request, year, queryset, field.name,
79 79
                 template_name='databrowse/calendar_year.html', allow_empty=False, allow_future=True,
80 80
                 extra_context=extra_context)
81 81
         else:
82  
-            return date_based.archive_index(request, self.model.objects.all(), field.name,
  82
+            return date_based.archive_index(request, queryset, field.name,
83 83
                 template_name='databrowse/calendar_main.html', allow_empty=True, allow_future=True,
84 84
                 extra_context=extra_context)
85 85
         assert False, ('%s, %s, %s, %s' % (field, year, month, day))
5  django/contrib/databrowse/templates/databrowse/base.html
@@ -2,6 +2,7 @@
2 2
 <html xmlns="http://www.w3.org/1999/xhtml" lang="{{ LANGUAGE_CODE }}" xml:lang="{{ LANGUAGE_CODE }}" {% if LANGUAGE_BIDI %}dir="rtl"{% endif %}>
3 3
 <head>
4 4
 <title>{% block title %}{% endblock %}</title>
  5
+{% block style %}
5 6
 <style type="text/css">
6 7
 * { margin:0; padding:0; }
7 8
 body { background:#eee; color:#333; font:76%/1.6 "Lucida Grande","Bitstream Vera Sans",Verdana,sans-serif; }
@@ -48,9 +49,11 @@
48 49
 /* CONTENT */
49 50
 #content { background:#fff; border-bottom:1px solid #ddd; padding:0 20px; }
50 51
 </style>
  52
+{% endblock %}
  53
+{% block extrahead %}{% endblock %}
51 54
 </head>
52 55
 <body id="{% block bodyid %}page{% endblock %}">
53  
-<div id="header"><a href="{{ root_url }}">Databrowse</a></div>
  56
+<div id="header"><a href="{{ root_url }}">{% block databrowse_title %}Databrowse{% endblock %}</a></div>
54 57
 <div id="content">
55 58
 {% block content %}{% endblock %}
56 59
 </div>
1  django/contrib/databrowse/templates/databrowse/base_site.html
... ...
@@ -0,0 +1 @@
  1
+{% extends "databrowse/base.html" %}
4  django/contrib/databrowse/templates/databrowse/calendar_day.html
... ...
@@ -1,4 +1,4 @@
1  
-{% extends "databrowse/base.html" %}
  1
+{% extends "databrowse/base_site.html" %}
2 2
 
3 3
 {% block title %}{{ model.verbose_name_plural|capfirst }} with {{ field.verbose_name }} {{ day|date:"F j, Y" }}{% endblock %}
4 4
 
@@ -6,7 +6,7 @@
6 6
 
7 7
 <div id="breadcrumbs"><a href="{{ root_url }}">Home</a> / <a href="{{ model.url }}">{{ model.verbose_name_plural|capfirst }}</a> / <a href="../../../../">Calendars</a> / <a href="../../../">By {{ field.verbose_name }}</a> / <a href="../../">{{ day.year }}</a> / <a href="../">{{ day|date:"F" }}</a> / {{ day.day }}</div>
8 8
 
9  
-<h1>{{ model.verbose_name_plural|capfirst }} with {{ field.verbose_name }} on {{ day|date:"F j, Y" }}</h1>
  9
+<h1>{{ object_list.count }} {% if object_list.count|pluralize %}{{ model.verbose_name_plural|escape }}{% else %}{{ model.verbose_name|escape }}{% endif %} with {{ field.verbose_name }} on {{ day|date:"F j, Y" }}</h1>
10 10
 
11 11
 <ul class="objectlist">
12 12
 {% for object in object_list %}
2  django/contrib/databrowse/templates/databrowse/calendar_homepage.html
... ...
@@ -1,4 +1,4 @@
1  
-{% extends "databrowse/base.html" %}
  1
+{% extends "databrowse/base_site.html" %}
2 2
 
3 3
 {% block title %}Calendars{% endblock %}
4 4
 
2  django/contrib/databrowse/templates/databrowse/calendar_main.html
... ...
@@ -1,4 +1,4 @@
1  
-{% extends "databrowse/base.html" %}
  1
+{% extends "databrowse/base_site.html" %}
2 2
 
3 3
 {% block title %}{{ field.verbose_name|capfirst }} calendar{% endblock %}
4 4
 
4  django/contrib/databrowse/templates/databrowse/calendar_month.html
... ...
@@ -1,4 +1,4 @@
1  
-{% extends "databrowse/base.html" %}
  1
+{% extends "databrowse/base_site.html" %}
2 2
 
3 3
 {% block title %}{{ model.verbose_name_plural|capfirst }} with {{ field.verbose_name }} in {{ month|date:"F Y" }}{% endblock %}
4 4
 
@@ -6,7 +6,7 @@
6 6
 
7 7
 <div id="breadcrumbs"><a href="{{ root_url }}">Home</a> / <a href="{{ model.url }}">{{ model.verbose_name_plural|capfirst }}</a> / <a href="../../../">Calendars</a> / <a href="../../">By {{ field.verbose_name }}</a> / <a href="../">{{ month.year }}</a> / {{ month|date:"F" }}</div>
8 8
 
9  
-<h1>{{ model.verbose_name_plural|capfirst }} with {{ field.verbose_name }} in {{ month|date:"F Y" }}</h1>
  9
+<h1>{{ object_list.count }} {% if object_list.count|pluralize %}{{ model.verbose_name_plural|escape }}{% else %}{{ model.verbose_name|escape }}{% endif %} with {{ field.verbose_name }} on {{ day|date:"F Y" }}</h1>
10 10
 
11 11
 <ul class="objectlist">
12 12
 {% for object in object_list %}
2  django/contrib/databrowse/templates/databrowse/calendar_year.html
... ...
@@ -1,4 +1,4 @@
1  
-{% extends "databrowse/base.html" %}
  1
+{% extends "databrowse/base_site.html" %}
2 2
 
3 3
 {% block title %}{{ model.verbose_name_plural|capfirst }} with {{ field.verbose_name }} in {{ year }}{% endblock %}
4 4
 
2  django/contrib/databrowse/templates/databrowse/choice_detail.html
... ...
@@ -1,4 +1,4 @@
1  
-{% extends "databrowse/base.html" %}
  1
+{% extends "databrowse/base_site.html" %}
2 2
 
3 3
 {% block title %}{{ model.verbose_name_plural|capfirst }} by {{ field.field.verbose_name }}: {{ value|escape }}{% endblock %}
4 4
 
2  django/contrib/databrowse/templates/databrowse/choice_list.html
... ...
@@ -1,4 +1,4 @@
1  
-{% extends "databrowse/base.html" %}
  1
+{% extends "databrowse/base_site.html" %}
2 2
 
3 3
 {% block title %}{{ model.verbose_name_plural|capfirst }} by {{ field.field.verbose_name }}{% endblock %}
4 4
 
4  django/contrib/databrowse/templates/databrowse/fieldchoice_detail.html
... ...
@@ -1,4 +1,4 @@
1  
-{% extends "databrowse/base.html" %}
  1
+{% extends "databrowse/base_site.html" %}
2 2
 
3 3
 {% block title %}{{ model.verbose_name_plural|capfirst|escape }} with {{ field.field.verbose_name|escape }} {{ value|escape }}{% endblock %}
4 4
 
@@ -6,7 +6,7 @@
6 6
 
7 7
 <div id="breadcrumbs"><a href="{{ root_url }}">Home</a> / <a href="{{ model.url }}">{{ model.verbose_name_plural|capfirst }}</a> / <a href="../../">Fields</a> / <a href="../">By {{ field.field.verbose_name|escape }}</a> / {{ value|escape }}</div>
8 8
 
9  
-<h1>{{ model.verbose_name_plural|capfirst|escape }} with {{ field.field.verbose_name|escape }} {{ value|escape }}</h1>
  9
+<h1>{{ object_list.count }} {% if object_list.count|pluralize %}{{ model.verbose_name_plural|escape }}{% else %}{{ model.verbose_name|escape }}{% endif %} with {{ field.field.verbose_name|escape }} {{ value|escape }}</h1>
10 10
 
11 11
 <ul class="objectlist">
12 12
 {% for object in object_list %}
2  django/contrib/databrowse/templates/databrowse/fieldchoice_homepage.html
... ...
@@ -1,4 +1,4 @@
1  
-{% extends "databrowse/base.html" %}
  1
+{% extends "databrowse/base_site.html" %}
2 2
 
3 3
 {% block title %}Browsable fields in {{ model.verbose_name_plural|escape }}{% endblock %}
4 4
 
2  django/contrib/databrowse/templates/databrowse/fieldchoice_list.html
... ...
@@ -1,4 +1,4 @@
1  
-{% extends "databrowse/base.html" %}
  1
+{% extends "databrowse/base_site.html" %}
2 2
 
3 3
 {% block title %}{{ model.verbose_name_plural|capfirst|escape }} by {{ field.field.verbose_name|escape }}{% endblock %}
4 4
 
2  django/contrib/databrowse/templates/databrowse/homepage.html
... ...
@@ -1,4 +1,4 @@
1  
-{% extends "databrowse/base.html" %}
  1
+{% extends "databrowse/base_site.html" %}
2 2
 
3 3
 {% block title %}Databrowse{% endblock %}
4 4
 
4  django/contrib/databrowse/templates/databrowse/model_detail.html
... ...
@@ -1,4 +1,4 @@
1  
-{% extends "databrowse/base.html" %}
  1
+{% extends "databrowse/base_site.html" %}
2 2
 
3 3
 {% block title %}{{ model.verbose_name_plural|capfirst }}{% endblock %}
4 4
 
@@ -6,7 +6,7 @@
6 6
 
7 7
 <div id="breadcrumbs"><a href="{{ root_url }}">Home</a> / {{ model.verbose_name_plural|capfirst }}</div>
8 8
 
9  
-<h1>{{ model.verbose_name_plural|capfirst }}</h1>
  9
+<h1>{{ model.objects.count }} {% if model.objects.count|pluralize %}{{ model.verbose_name_plural }}{% else %}{{ model.verbose_name }}{% endif %}</h1>
10 10
 
11 11
 {{ plugin_html }}
12 12
 
2  django/contrib/databrowse/templates/databrowse/object_detail.html
... ...
@@ -1,4 +1,4 @@
1  
-{% extends "databrowse/base.html" %}
  1
+{% extends "databrowse/base_site.html" %}
2 2
 
3 3
 {% block title %}{{ object.model.verbose_name|capfirst }}: {{ object }}{% endblock %}
4 4
 

0 notes on commit 8d97f15

Please sign in to comment.
Something went wrong with that request. Please try again.