Login

Get sorted items for any model in any order

Author:
vikingosegundo
Posted:
October 8, 2009
Language:
Python
Version:
1.1
Score:
1 (after 1 ratings)

This get_sorted_items tag takes app.model, a number n, a a field name to sort ,and a variable-name as arguments and will deliver the n first objects of the model.

it checks if a Manager publicmgr exists and calls this if the user isn't authenticated.

Additionally it write the count of the model to the context

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# -*- coding: UTF-8 -*-
from django.template import Library, Node ,TemplateSyntaxError
from django.db.models import get_model
import re
from datetime import datetime

register = Library()

class GetItemsNode(Node):
    def __init__(self, model, num, by, varname):
        self.num, self.varname = num, varname
        self.model = get_model(*model.split('.'))
        self.by = by

    def render(self, context):
        if hasattr(self.model, 'publicmgr') and not context['user'].is_authenticated():
            context[self.varname] = self.model.publicmgr.all().order_by(self.by)[:self.num]
            context[self.model.__name__.lower()+'_count']= self.model.publicmgr.count()
        
        else:
            context[self.varname] = self.model.objects.all().order_by(self.by)[:self.num]
            context[self.model.__name__.lower()+'_count'] = self.model.objects.count()   

        return  ''
        
        
def get_sorted_items(parser, token):
    bits = token.contents.split()
    if len(bits) != 7:
        raise TemplateSyntaxError, "get_sorted_items tag takes exactly 6 arguments"
    if bits[3] != 'by':
        raise TemplateSyntaxError, "third argument to get_sorted_items tag must be 'by'"
    if bits[5] != 'as':
        raise TemplateSyntaxError, "fivth argument to get_sorted_items tag must be 'as'"
    return GetItemsNode(bits[1], bits[2],bits[4], bits[6])
    
get_sorted_items = register.tag(get_sorted_items)


       <ul id="right_column">
        
        <li id="news_portlet" class="portlet">
        <h2>News</h2>
            <div class="portlet_body">
            {% get_sorted_items cms.news 5 by -created_on as items %}
            {% include 'snippets/dl.html' %}
            {% if news_count > items|length %}more...{% endif %}
            </div>
        </li>
        <li id="event_portlet" class="portlet">
        <h2>events</h2>
            <div class="portlet_body">
            {% get_sorted_items cms.event 5 by date as items %}
            {% include 'snippets/dl.html' %}
            {% if event_count > items|length %}more...{% endif %}
            </div>
        </li>
    </ul>

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

Please login first before commenting.