Ticket #4481: databrowse_pagination_calendars.diff

File databrowse_pagination_calendars.diff, 25.0 KB (added by floguy@…, 17 years ago)

Heavily updated patch

  • django/contrib/databrowse/plugins/calendars.py

     
    55from django.shortcuts import render_to_response
    66from django.utils.text import capfirst
    77from django.utils.translation import get_date_formats
     8from django.template import RequestContext
    89from django.views.generic import date_based
    910from django.utils.encoding import force_unicode
    1011import datetime
     
    6364        return render_to_response('databrowse/calendar_homepage.html', {'root_url': self.site.root_url, 'model': easy_model, 'field_list': field_list})
    6465
    6566    def calendar_view(self, request, field, year=None, month=None, day=None):
    66         easy_model = EasyModel(self.site, self.model)
    67         extra_context = {'root_url': self.site.root_url, 'model': easy_model, 'field': field}
    68         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,
    71                 template_name='databrowse/calendar_day.html', allow_empty=False, allow_future=True,
    72                 extra_context=extra_context)
    73         elif month is not None:
    74             return date_based.archive_month(request, year, month, self.model.objects.all(), field.name,
    75                 template_name='databrowse/calendar_month.html', allow_empty=False, allow_future=True,
    76                 extra_context=extra_context)
    77         elif year is not None:
    78             return date_based.archive_year(request, year, self.model.objects.all(), field.name,
    79                 template_name='databrowse/calendar_year.html', allow_empty=False, allow_future=True,
    80                 extra_context=extra_context)
     67        page = 0
     68        if request.has_key('page'):
     69            page = request.GET['page']
     70        easy_model = EasyModel(self.site, self.model, paginate_by = 100, page = page)
     71        context = {'model': easy_model, 'field': field, 'root_url' : self.site.root_url}
     72        if day is not None and day != "objects":
     73            context['year'] = year
     74            context['month'] = month
     75            context['day'] = day
     76            query = {
     77                "%s__day" % field.name : int(day),
     78                "%s__month" % field.name : int(time.strftime('%m', time.strptime(month, '%B'))),
     79                "%s__year" % field.name : int(year),
     80            }
     81            context['object_list'] = easy_model.objects(**query)
     82            template_name = "databrowse/calendar_fordate.html"
     83        elif month is not None and month != "objects":
     84            context['year'] = year
     85            context['month'] = month
     86            query = {
     87                "%s__month" % field.name : int(time.strftime('%m', time.strptime(month, '%B'))),
     88                "%s__year" % field.name : int(year),
     89            }
     90            if day == "objects":
     91                context['object_list'] = easy_model.objects(**query)
     92                template_name = "databrowse/calendar_fordate.html"
     93            else:
     94                context['date_list'] = self.model.objects.filter(**query).dates(field.name, 'day')
     95                template_name = "databrowse/calendar_daterange.html"
     96        elif year is not None and year != "objects":
     97            context['year'] = year
     98            query = {"%s__year" % field.name : int(year)}
     99            if month == "objects":
     100                context['object_list'] = easy_model.objects(**query)
     101                template_name = "databrowse/calendar_fordate.html"
     102            else:
     103                context['date_list'] = self.model.objects.filter(**query).dates(field.name, 'month')
     104                template_name = "databrowse/calendar_daterange.html"
    81105        else:
    82             return date_based.archive_index(request, self.model.objects.all(), field.name,
    83                 template_name='databrowse/calendar_main.html', allow_empty=True, allow_future=True,
    84                 extra_context=extra_context)
     106            context = { 'model' : easy_model, 'field' : field }
     107            if year == "objects":
     108                context['object_list'] = easy_model.objects()
     109                template_name = "databrowse/calendar_fordate.html"
     110            else:
     111                context['date_list'] = self.model.objects.all().dates(field.name, 'year')
     112                template_name = "databrowse/calendar_daterange.html"
     113        return render_to_response(template_name, context, context_instance=RequestContext(request))
    85114        assert False, ('%s, %s, %s, %s' % (field, year, month, day))
  • django/contrib/databrowse/plugins/fieldchoices.py

     
    6565        return render_to_response('databrowse/fieldchoice_homepage.html', {'root_url': self.site.root_url, 'model': easy_model, 'field_list': field_list})
    6666
    6767    def field_view(self, request, field, value=None):
    68         easy_model = EasyModel(self.site, self.model)
     68        page = 0
     69        if request.has_key('page'):
     70            page = int(request.GET['page'])
     71        easy_model = EasyModel(self.site, self.model, paginate_by = 100, page = page)
    6972        easy_field = easy_model.field(field.name)
    7073        if value is not None:
    7174            obj_list = easy_model.objects(**{field.name: value})
    7275            return render_to_response('databrowse/fieldchoice_detail.html', {'root_url': self.site.root_url, 'model': easy_model, 'field': easy_field, 'value': value, 'object_list': obj_list})
    73         obj_list = [v[field.name] for v in self.model._default_manager.distinct().order_by(field.name).values(field.name)]
    74         return render_to_response('databrowse/fieldchoice_list.html', {'root_url': self.site.root_url, 'model': easy_model, 'field': easy_field, 'object_list': obj_list})
     76        op = ObjectPaginator(self.model._default_manager.distinct().order_by(field.name), 100)
     77        results = op.get_page(page)
     78        pagination_dict = {
     79            "current_page" : page,
     80            "pages" : op.pages,
     81        }
     82        obj_list = [getattr(v, field.name) for v in results]
     83        return render_to_response('databrowse/fieldchoice_list.html', {'root_url': self.site.root_url, 'model': easy_model, 'field': easy_field, 'object_list': obj_list, "pagination" : pagination_dict})
  • django/contrib/databrowse/datastructures.py

     
    77from django.utils import dateformat
    88from django.utils.text import capfirst
    99from django.utils.translation import get_date_formats
     10from django.core.paginator import ObjectPaginator, InvalidPage
    1011from django.utils.encoding import smart_unicode, smart_str, iri_to_uri
    1112
    1213EMPTY_VALUE = '(None)'
    1314
    1415class EasyModel(object):
    15     def __init__(self, site, model):
     16    def __init__(self, site, model, paginate_by = None, page = 0):
    1617        self.site = site
    1718        self.model = model
    1819        self.model_list = site.registry.keys()
    1920        self.verbose_name = model._meta.verbose_name
    2021        self.verbose_name_plural = model._meta.verbose_name_plural
     22        self.paginate_by, self.page = paginate_by, page
     23        self.pagination = None
    2124
    2225    def __repr__(self):
    2326        return '<EasyModel for %s>' % smart_str(self.model._meta.object_name)
     
    2932    def url(self):
    3033        return '%s%s/%s/' % (self.site.root_url, self.model._meta.app_label, self.model._meta.module_name)
    3134
    32     def objects(self, **kwargs):
    33         for obj in self.model._default_manager.filter(**kwargs):
     35    def objects_from_queryset(self, qs):
     36        if self.paginate_by is not None:
     37            op = ObjectPaginator(qs, self.paginate_by)
     38            results = op.get_page(self.page)
     39            self.pagination = {
     40                u"pages" : op.pages,
     41                u"current_page" : int(self.page),
     42            }
     43        else:
     44            results = qs
     45        for obj in results:
    3446            yield EasyInstance(self, obj)
    3547
     48    def objects(self, **kwargs):
     49        return self.objects_from_queryset(self.model._default_manager.filter(**kwargs))
     50
    3651    def object_by_pk(self, pk):
    3752        return EasyInstance(self, self.model._default_manager.get(pk=pk))
    3853
  • django/contrib/databrowse/templatetags/databrowse.py

     
     1from django import template
     2from django.template import resolve_variable
     3
     4register = template.Library()
     5
     6def letterlimit(value, arg):
     7    try:
     8        cutoff = int(arg)
     9        return "%s%s" % (str(value)[:cutoff], '...')
     10    except Exception, e:
     11        return value
     12
     13def do_pagination(parser, token):
     14    try:
     15        tag_name, pagination_dict_instance, width = token.split_contents()
     16    except ValueError:
     17        raise template.TemplateSyntaxError, "%r tag requires a double argument" % token.contents.split()[0]
     18    try:
     19        width_int = int(width)
     20    except:
     21        raise template.TemplateSyntaxError, "%r tag's second argument must be an integer" % token.contents.split()[0]
     22    return PaginationNode(pagination_dict_instance, width_int)
     23
     24class PaginationNode(template.Node):
     25    def __init__(self, context_key, width):
     26        self.context_key = context_key
     27        self.width = width
     28    def render(self, context):
     29        to_return = "<div id=\"pagination\">"
     30        link_str = "<a href=\"./?page=%d\">[%d]</a>"
     31        try:
     32            pagination_dict = resolve_variable(self.context_key, context)
     33            if pagination_dict['pages'] < 2:
     34                to_return = ''
     35            else:
     36                for page in xrange(pagination_dict['pages']):
     37                    if page == pagination_dict['current_page']:
     38                        to_return = to_return + ("<a href=\"./?page=%d\" class=\"bold\">[%d]</a>" % (page, page))
     39                    elif page < (self.width + 1) or page > (pagination_dict['pages'] - self.width - 2):
     40                        to_return = to_return + (link_str % (page, page))
     41                    elif page == pagination_dict['current_page'] - self.width - 1:
     42                        to_return = to_return + "..."
     43                    elif page == pagination_dict['current_page'] + self.width + 1:
     44                        to_return = to_return + "..."
     45                    elif (page >= pagination_dict['current_page'] - self.width) and (page <= pagination_dict['current_page'] + self.width):
     46                        to_return = to_return + (link_str % (page, page))
     47                to_return = to_return + "</div>"
     48        except template.VariableDoesNotExist:
     49            to_return = ''
     50        return to_return
     51
     52register.tag('paginate', do_pagination)
     53register.filter('letterlimit', letterlimit)
     54 No newline at end of file
  • django/contrib/databrowse/templates/databrowse/calendar_fordate.html

    Cannot display: file marked as a binary type.
    svn:mime-type = application/octet-stream
    
    Property changes on: django/contrib/databrowse/templatetags/databrowse.pyc
    ___________________________________________________________________
    Name: svn:mime-type
       + application/octet-stream
    
    Cannot display: file marked as a binary type.
    svn:mime-type = application/octet-stream
    
    Property changes on: django/contrib/databrowse/templatetags/__init__.pyc
    ___________________________________________________________________
    Name: svn:mime-type
       + application/octet-stream
    
     
     1{% extends "databrowse/base.html" %}
     2{% load databrowse %}
     3
     4{% block title %}{{ model.verbose_name_plural|capfirst }} with {{ field.verbose_name }} in {% if year %}{{ year }}{% else %}{% if month %}{{ month }}{% else %}{% if day %}{{ day }}{% endif %}{% endif %}{% endif %}{% endblock %}
     5
     6{% block content %}
     7
     8<div id="breadcrumbs">
     9{% if day %}
     10    <a href="../../../../../../../../">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="../../"> {{ year }}</a> / <a href="../">{{ month|capfirst }}</a> / {{ day }}
     11{% else %}
     12    {% if month %}
     13        <a href="../../../../../../../">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="../../"> {{ year }}</a> / <a href="../">{{ month|capfirst }}</a> / objects
     14    {% else %}
     15        {% if year %}
     16            <a href="../../../../../../">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="../">{{ year }}</a> / objects
     17        {% else %}
     18            <a href="../../../../../">Home</a> / <a href="{{ model.url }}">{{ model.verbose_name_plural|capfirst }}</a> / <a href="../../">Calendars</a> / <a href="../">By {{ field.verbose_name }}</a> / objects
     19        {% endif %}
     20    {% endif %}
     21{% endif %}
     22</div>
     23
     24<h1>
     25{{ model.verbose_name_plural|capfirst }}
     26{% if day %}
     27    with {{ field.verbose_name }} on {{ month|capfirst }} {{ day }}, {{ year }}
     28{% else %}
     29    {% if month %}
     30        with {{ field.verbose_name }} in {{ month|capfirst }} of {{ year }}
     31    {% else %}
     32        {% if year %}
     33            with {{ field.verbose_name }} in {{ year }}
     34        {% else %}
     35            by {{ field.verbose_name }}
     36        {% endif %}
     37    {% endif %}
     38{% endif %}
     39</h1>
     40
     41<ul class="objectlist">
     42{% for object in object_list %}
     43    <li class="{% cycle odd,even %}"><a href="{{ object.url }}">{{ object }}</a></li>
     44{% endfor %}
     45</ul>
     46
     47{% if model.pagination %}
     48    {% paginate model.pagination 2 %}
     49{% endif %}
     50
     51{% endblock %}
  • django/contrib/databrowse/templates/databrowse/calendar_month.html

     
    1 {% extends "databrowse/base.html" %}
    2 
    3 {% block title %}{{ model.verbose_name_plural|capfirst }} with {{ field.verbose_name }} in {{ month|date:"F Y" }}{% endblock %}
    4 
    5 {% block content %}
    6 
    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 
    9 <h1>{{ model.verbose_name_plural|capfirst }} with {{ field.verbose_name }} in {{ month|date:"F Y" }}</h1>
    10 
    11 <ul class="objectlist">
    12 {% for object in object_list %}
    13 <li class="{% cycle odd,even %}"><a href="{{ object.url }}">{{ object }}</a></li>
    14 {% endfor %}
    15 </ul>
    16 
    17 {% endblock %}
  • django/contrib/databrowse/templates/databrowse/fieldchoice_detail.html

     
    11{% extends "databrowse/base.html" %}
     2{% load databrowse %}
    23
    34{% block title %}{{ model.verbose_name_plural|capfirst|escape }} with {{ field.field.verbose_name|escape }} {{ value|escape }}{% endblock %}
    45
     
    1415{% endfor %}
    1516</ul>
    1617
     18{% paginate model.pagination 2 %}
     19
    1720{% endblock %}
  • django/contrib/databrowse/templates/databrowse/model_detail.html

     
    11{% extends "databrowse/base.html" %}
     2{% load databrowse %}
    23
    34{% block title %}{{ model.verbose_name_plural|capfirst }}{% endblock %}
    45
     
    1617{% endfor %}
    1718</ul>
    1819
     20{% paginate model.pagination 2 %}
     21
    1922{% endblock %}
  • django/contrib/databrowse/templates/databrowse/calendar_daterange.html

     
     1{% extends "databrowse/base.html" %}
     2{% load databrowse %}
     3
     4{% block title %}{{ model.verbose_name_plural|capfirst }} with {{ field.verbose_name }} in {% if year %}{{ year }}{% else %}{% if month %}{{ month }}{% else %}{% if day %}{{ day }}{% endif %}{% endif %}{% endif %}{% endblock %}
     5
     6{% block content %}
     7
     8<div id="breadcrumbs">
     9{% if month %}
     10    <a href="../../../../../../">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="../"> {{ year }}</a> / {{ month|capfirst }}
     11{% else %}
     12    {% if year %}
     13        <a href="../../../../../">Home</a> / <a href="{{ model.url }}">{{ model.verbose_name_plural|capfirst }}</a> / <a href="../../">Calendars</a> / <a href="../">By {{ field.verbose_name }}</a> / {{ year }}
     14    {% else %}
     15        <a href="../../../../">Home</a> / <a href="{{ model.url }}">{{ model.verbose_name_plural|capfirst }}</a> / <a href="../">Calendars</a> / By {{ field.verbose_name }}
     16    {% endif %}
     17{% endif %}
     18</div>
     19
     20<h1>
     21{{ model.verbose_name_plural|capfirst }}
     22{% if month %}
     23    with {{ field.verbose_name }} in {{ month|capfirst }} of {{ year }}
     24{% else %}
     25    {% if year %}
     26        with {{ field.verbose_name }} in {{ year }}
     27    {% else %}
     28        by {{ field.verbose_name }}
     29    {% endif %}
     30{% endif %}
     31</h1>
     32
     33<ul class="objectlist">
     34{% for date in date_list %}
     35    {% if month %}
     36        <li class="{% cycle odd,even %}"><a href="{{ date|date:"d"|lower }}">{{ date|date:"D d" }}</a></li>
     37    {% else %}
     38        {% if year %}
     39            <li class="{% cycle odd,even %}">
     40                <a href="{{ date|date:"F"|lower }}">{{ date|date:"F" }}</a><a href="{{ date|date:"F"|lower }}/objects" class="more">See Objects &rarr;</a>
     41            </li>
     42        {% else %}
     43            <li class="{% cycle odd,even %}">
     44                <a href="{{ date|date:"Y"|lower }}">{{ date|date:"Y" }}</a><a href="{{ date|date:"Y"|lower }}/objects" class="more">See Objects &rarr;</a>
     45            </li>
     46        {% endif %}
     47    {% endif %}
     48{% endfor %}
     49</ul>
     50
     51{% if model.pagination %}
     52    {% paginate model.pagination 2 %}
     53{% endif %}
     54
     55{% endblock %}
  • django/contrib/databrowse/templates/databrowse/homepage.html

     
    11{% extends "databrowse/base.html" %}
     2{% load databrowse %}
    23
    34{% block title %}Databrowse{% endblock %}
    45
     
    1112          <h2><a href="{{ model.url }}">{{ model.verbose_name_plural|capfirst }}</a></h2>
    1213                <p>
    1314                {% for object in model.sample_objects %}
    14                         <a href="{{ object.url }}">{{ object }}</a>,
     15                        <a href="{{ object.url }}">{{ object|letterlimit:"30" }}</a>,
    1516                {% endfor %}
    1617                        <a class="more" href="{{ model.url }}">More &rarr;</a>
    1718                </p>
  • django/contrib/databrowse/templates/databrowse/fieldchoice_list.html

     
    11{% extends "databrowse/base.html" %}
     2{% load databrowse %}
    23
    34{% block title %}{{ model.verbose_name_plural|capfirst|escape }} by {{ field.field.verbose_name|escape }}{% endblock %}
    45
     
    1415{% endfor %}
    1516</ul>
    1617
     18{% paginate pagination 2 %}
     19
    1720{% endblock %}
  • django/contrib/databrowse/templates/databrowse/calendar_year.html

     
    1 {% extends "databrowse/base.html" %}
    2 
    3 {% block title %}{{ model.verbose_name_plural|capfirst }} with {{ field.verbose_name }} in {{ year }}{% endblock %}
    4 
    5 {% block content %}
    6 
    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> / {{ year }}</div>
    8 
    9 <h1>{{ model.verbose_name_plural|capfirst }} with {{ field.verbose_name }} in {{ year }}</h1>
    10 
    11 <ul class="objectlist">
    12 {% for month in date_list %}
    13 <li class="{% cycle odd,even %}"><a href="{{ month|date:"M"|lower }}/">{{ month|date:"F" }}</a></li>
    14 {% endfor %}
    15 </ul>
    16 
    17 {% endblock %}
  • django/contrib/databrowse/templates/databrowse/calendar_day.html

     
    1 {% extends "databrowse/base.html" %}
    2 
    3 {% block title %}{{ model.verbose_name_plural|capfirst }} with {{ field.verbose_name }} {{ day|date:"F j, Y" }}{% endblock %}
    4 
    5 {% block content %}
    6 
    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 
    9 <h1>{{ model.verbose_name_plural|capfirst }} with {{ field.verbose_name }} on {{ day|date:"F j, Y" }}</h1>
    10 
    11 <ul class="objectlist">
    12 {% for object in object_list %}
    13 <li class="{% cycle odd,even %}"><a href="{{ object.url }}">{{ object }}</a></li>
    14 {% endfor %}
    15 </ul>
    16 
    17 {% endblock %}
  • django/contrib/databrowse/templates/databrowse/base.html

     
    1818.filter strong { color:#666; }
    1919/* OBJECT LISTS */
    2020.objectlist { clear:both; margin:0 -20px; color:#666; }
    21 .objectlist li a { display:block; padding:1em 20px; }
    22 .objectlist li a:hover { background:#5b80b2; color:#3B5572; color:#fff; text-decoration:none; }
     21.objectlist li { display:block; padding:1em 20px; color: #5b80b2; }
     22.objectlist li:hover { background:#5b80b2; color:#fff; }
     23.objectlist li a { color: inherit; }
     24.objectlist li a:hover { text-decoration: none; }
     25.objectlist li a.more { margin-left: 1.2em; color:#999 }
    2326.related h2 { font-size: 1em; margin-bottom: 0.6em; }
    2427.related .objectlist li a { padding: 0.6em 20px; }
    2528.related .objectlist li.odd { background:#eee; }
     
    4750#header a:hover { text-decoration:underline; }
    4851/* CONTENT */
    4952#content { background:#fff; border-bottom:1px solid #ddd; padding:0 20px; }
     53/* PAGINATION */
     54#pagination { margin: auto auto auto auto; text-align: center; font-size: 1.2em; }
     55#pagination .bold { font-weight: bold; }
    5056</style>
    5157</head>
    5258<body id="{% block bodyid %}page{% endblock %}">
  • django/contrib/databrowse/sites.py

     
    5959        return plugin.model_view(request, self, rest_of_url)
    6060
    6161    def main_view(self, request):
    62         easy_model = EasyModel(self.site, self.model)
     62        page = 0
     63        if request.has_key('page'):
     64            page = int(request.GET['page'])
     65        easy_model = EasyModel(self.site, self.model, paginate_by = 100, page = page)
    6366        html_snippets = u'\n'.join([p.model_index_html(request, self.model, self.site) for p in self.plugins.values()])
    6467        return render_to_response('databrowse/model_detail.html', {
    6568            'model': easy_model,
Back to Top