|
1 #!/usr/bin/python2.5 |
|
2 # |
|
3 # Copyright 2008 the Melange authors. |
|
4 # |
|
5 # Licensed under the Apache License, Version 2.0 (the "License"); |
|
6 # you may not use this file except in compliance with the License. |
|
7 # You may obtain a copy of the License at |
|
8 # |
|
9 # http://www.apache.org/licenses/LICENSE-2.0 |
|
10 # |
|
11 # Unless required by applicable law or agreed to in writing, software |
|
12 # distributed under the License is distributed on an "AS IS" BASIS, |
|
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14 # See the License for the specific language governing permissions and |
|
15 # limitations under the License. |
|
16 |
|
17 """Helpers used to render lists. |
|
18 """ |
|
19 |
|
20 __authors__ = [ |
|
21 '"Pawel Solyga" <pawel.solyga@gmail.com>', |
|
22 ] |
|
23 |
|
24 |
|
25 DEF_LIMIT = 10 |
|
26 |
|
27 def getListParemeters(offset=None, limit=None): |
|
28 """Updates and validates offset and limit values of the list. |
|
29 |
|
30 Args: |
|
31 offset: offset in list which defines first item to return |
|
32 limit: max amount of items per page |
|
33 |
|
34 Returns: |
|
35 updated offset and limit values |
|
36 """ |
|
37 # update offset value |
|
38 if offset: |
|
39 try: |
|
40 offset = int(offset) |
|
41 except: |
|
42 offset = 0 |
|
43 else: |
|
44 offset = max(0, offset) |
|
45 else: |
|
46 offset = 0 |
|
47 |
|
48 # update limit value |
|
49 if limit: |
|
50 try: |
|
51 limit = int(limit) |
|
52 except: |
|
53 limit = DEF_LIMIT |
|
54 else: |
|
55 limit = max(1, min(limit, 100)) |
|
56 else: |
|
57 limit = DEF_LIMIT |
|
58 |
|
59 return offset, limit |
|
60 |
|
61 DEF_LIST_TEMPLATES = {'list_main': 'soc/list/list_main.html', |
|
62 'list_pagination': 'soc/list/list_pagination.html', |
|
63 'list_row': 'soc/list/list_row.html', |
|
64 'list_heading': 'soc/list/list_heading.html'} |
|
65 |
|
66 def setList(request, context, list_data, |
|
67 offset=0, limit=0, list_templates=DEF_LIST_TEMPLATES): |
|
68 """Updates template context dict with variables used for rendering lists. |
|
69 |
|
70 Args: |
|
71 request: the Django HTTP request object |
|
72 context: the template context dict to be updated in-place (pass in a copy |
|
73 if the original must not be modified), or None if a new one is to be |
|
74 created; any existing fields already present in the context dict passed |
|
75 in by the caller are left unaltered |
|
76 list_data: array of data to be displayed in the list |
|
77 offset: offset in list which defines first item to return |
|
78 limit: max amount of items per page |
|
79 list_templates: templates that are used when rendering list |
|
80 |
|
81 Returns: |
|
82 updated template context dict supplied by the caller or a new context |
|
83 dict if the caller supplied None |
|
84 |
|
85 { |
|
86 'list_data': list data to be displayed |
|
87 'list_main': url to list main template |
|
88 'list_pagination': url to list pagination template |
|
89 'list_row': url to list row template |
|
90 'list_heading': url to list heading template |
|
91 'limit': max amount of items per page, |
|
92 'newest': url to first page of the list |
|
93 'prev': url to previous page |
|
94 'next': url to next page |
|
95 'first': offset of the first item in the list |
|
96 'last': offest of the lst item in the list |
|
97 } |
|
98 """ |
|
99 if not list_data: |
|
100 list_data = [] |
|
101 |
|
102 more = bool(list_data[limit:]) |
|
103 if more: |
|
104 del list_data[limit:] |
|
105 if more: |
|
106 next = request.path + '?offset=%d&limit=%d' % (offset+limit, limit) |
|
107 else: |
|
108 next = '' |
|
109 if offset > 0: |
|
110 prev = request.path + '?offset=%d&limit=%d' % (max(0, offset-limit), limit) |
|
111 else: |
|
112 prev = '' |
|
113 newest = '' |
|
114 if offset > limit: |
|
115 newest = request.path + '?limit=%d' % limit |
|
116 |
|
117 if not context: |
|
118 context = {} |
|
119 |
|
120 context.update( |
|
121 {'list_data': list_data, |
|
122 'list_main': list_templates['list_main'], |
|
123 'list_pagination': list_templates['list_pagination'], |
|
124 'list_row': list_templates['list_row'], |
|
125 'list_heading': list_templates['list_heading'], |
|
126 'limit': limit, |
|
127 'newest': newest, |
|
128 'prev': prev, |
|
129 'next': next, |
|
130 'first': offset+1, |
|
131 'last': len(list_data) > 1 and offset+len(list_data) or None}) |
|
132 |
|
133 return context |