url.py

  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
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# Create your views here.
import math
from django.shortcuts import render_to_response
from django import template
from menu.models import Page
from text.models import Static
from django import template
from django.template import resolve_variable
from django.template import Library, Node
from django.db import connection
register = template.Library()
@register.inclusion_tag('page/menu.html')
def get_main_tree(pid = 0):
if pid == 0:
list = Page.objects.all().filter(pid__isnull=True, issub=0).order_by('main')
else:
list = Page.objects.all().filter(pid=pid, issub=0).order_by('main')
return {'list': list}
@register.inclusion_tag('page/submenu.html')
def get_sub_tree(pid, level=0):
issub = 1
if level == 0:
try:
p = Page.objects.get(id=pid)
except:
return
while p.issub == 1:
pid = p.pid.id
p = p.pid
list = Page.objects.all().filter(pid=pid, issub=1).order_by('main')
return {'list': list, 'level': level+1}
@register.inclusion_tag('page/path.html')
def get_path(p):
page = p
try:
page = page.pid
except:
return
return {'page': page}
import math
class Pages:
def __init__(self, view, page, pages, segment):
self.view = view
self.page = page + 1
self.pages = pages
self.left = []
self.middle = []
self.right = []
self.next = None
self.previous = None
if self.page > 1:
self.previous = self.page - 1
p = int(1)
print p
print segment
print self.pages
while p < segment and p <= self.pages:
self.left.append(p)
p = p + 1
if p < self.page - segment/2:
p = self.page - segment/2 + 1
if p > segment:
while p < self.page + segment/2 and p <= self.pages:
self.middle.append(int(p))
p = p + 1
else:
while p < self.page + segment/2 and p <= self.pages:
self.left.append(int(p))
p = p + 1
if p < self.pages - segment/2:
p = self.pages - segment/2
if p > self.pages - segment/2:
while p < self.pages:
self.middle.append(int(p))
p = p + 1
else:
while p <= self.pages:
self.right.append(int(p))
p = p + 1
if self.page < self.pages:
self.next = self.page + 1
class PaginationNode(Node):
def __init__(self, view, objects_var, page_var, step, segment, variable):
self.view = view
self.objects_var = objects_var
self.page_var = page_var
self.step = int(step)
self.segment = int(segment)
self.variable = variable
def render(self, context):
objects = template.resolve_variable(self.objects_var, context)
page = template.resolve_variable(self.page_var, context)
if not page:
page = 1
page = int(page) - 1
count = objects.count()
pages = math.ceil(float(count) / self.step)
try:
context[self.variable] = objects[page * self.step: (page + 1) * self.step]
context['pagination'] = Pages(self.view, page, pages, self.segment)
except:
pass
return ''
def paginate(parser, token):
tokens = token.contents.split()
if len(tokens) != 7:
raise template.TemplateSyntaxError, "pagination tag takes view, objects, page, step, segment, and variable as arguments"
return PaginationNode(tokens[1], tokens[2], tokens[3], tokens[4], tokens[5], tokens[6])
register.tag('paginate', paginate)