Ticket #4481: ticket4481.patch

File ticket4481.patch, 5.2 KB (added by floguy@…, 8 years ago)

first implementation of pagination on object instances

  • 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
    1011
    1112EMPTY_VALUE = '(None)'
    1213
    1314class EasyModel(object):
    14     def __init__(self, site, model):
     15    def __init__(self, site, model, paginate_by=100, page=0):
    1516        self.site = site
    1617        self.model = model
    1718        self.model_list = site.registry.keys()
    1819        self.verbose_name = model._meta.verbose_name
    1920        self.verbose_name_plural = model._meta.verbose_name_plural
     21        self.paginate_by = paginate_by
     22        self.page = page
     23        self.pagination = None
    2024
    2125    def __repr__(self):
    2226        return '<EasyModel for %s>' % self.model._meta.object_name
     
    2933        return '%s%s/%s/' % (self.site.root_url, self.model._meta.app_label, self.model._meta.module_name)
    3034
    3135    def objects(self, **kwargs):
    32         for obj in self.model._default_manager.filter(**kwargs):
     36        op = ObjectPaginator(self.model._default_manager.filter(**kwargs), self.paginate_by)
     37        self.pagination = {
     38            'hits' : op.hits,
     39            'pages' : op.pages,
     40            'has_next_page' : op.has_next_page(self.page),
     41            'has_previous_page' : op.has_previous_page(self.page),
     42            'first_on_page' : op.first_on_page(self.page),
     43            'last_on_page' : op.last_on_page(self.page),
     44            'next_page_url' : "%s?page=%d" % (self.url(), self.page+1),
     45            'previous_page_url' : "%s?page=%d" % (self.url(), self.page-1),
     46        }
     47        for obj in op.get_page(self.page):
    3348            yield EasyInstance(self, obj)
    3449
    3550    def object_by_pk(self, pk):
     
    85100        return '<EasyInstance for %s (%s)>' % (self.model.model._meta.object_name, self.instance._get_pk_val())
    86101
    87102    def __str__(self):
    88         val = str(self.instance)
    89         if len(val) > 30:
    90             return val[:30] + '...'
    91         return val
     103        return str(self.instance)
    92104
    93105    def pk(self):
    94106        return self.instance._get_pk_val()
  • django/contrib/databrowse/templates/databrowse/model_detail.html

     
    1616{% endfor %}
    1717</ul>
    1818
     19<div class="pagination">
     20{% if model.pagination.has_previous_page %}
     21    <a href="{{ model.pagination.previous_page_url }}" class="prev">&larr; Previous Page</a>
     22{% endif %}
     23{% if model.pagination.has_next_page %}
     24    <a href="{{ model.pagination.next_page_url }}" class="next">Next Page &rarr;</a>
     25{% endif %}
     26</div>
    1927{% 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|truncatechar:"30" }}</a>,
    1516                {% endfor %}
    1617                        <a class="more" href="{{ model.url }}">More &rarr;</a>
    1718                </p>
  • django/contrib/databrowse/templates/databrowse/base.html

     
    4747#header a:hover { text-decoration:underline; }
    4848/* CONTENT */
    4949#content { background:#fff; border-bottom:1px solid #ddd; padding:0 20px; }
     50/* PAGINATION */
     51.pagination { width: 50%; margin-left: auto; margin-right: auto; font-size: 1.4em; }
     52.pagination a.prev { float: left; }
     53.pagination a.next { float: right; }
    5054</style>
    5155</head>
    5256<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        if "page" in request.GET:
     63            page = int(request.GET['page'])
     64        else:
     65            page = 0
     66        easy_model = EasyModel(self.site, self.model, page=page)
    6367        html_snippets = '\n'.join([p.model_index_html(request, self.model, self.site) for p in self.plugins.values()])
    6468        return render_to_response('databrowse/model_detail.html', {
    6569            'model': easy_model,
Back to Top