76 limit = getPreferredListPagination() |
76 limit = getPreferredListPagination() |
77 |
77 |
78 return max(0, offset), max(1, min(limit, MAX_PAGINATION)) |
78 return max(0, offset), max(1, min(limit, MAX_PAGINATION)) |
79 |
79 |
80 |
80 |
81 DEF_LIST_TEMPLATES = {'list_main': 'soc/list/list_main.html', |
81 def getList(request, list_data, list_templates, description, offset=0, limit=0): |
82 'list_pagination': 'soc/list/list_pagination.html', |
82 """Returns a dict with fields used for rendering lists. |
83 'list_row': 'soc/list/list_row.html', |
|
84 'list_heading': 'soc/list/list_heading.html'} |
|
85 |
|
86 def setList(request, context, list_data, |
|
87 offset=0, limit=0, list_templates=DEF_LIST_TEMPLATES): |
|
88 """Updates template context dict with variables used for rendering lists. |
|
89 |
83 |
90 Args: |
84 Args: |
91 request: the Django HTTP request object |
85 request: the Django HTTP request object |
92 context: the template context dict to be updated in-place (pass in a copy |
|
93 if the original must not be modified), or None if a new one is to be |
|
94 created; any existing fields already present in the context dict passed |
|
95 in by the caller are left unaltered |
|
96 list_data: array of data to be displayed in the list |
86 list_data: array of data to be displayed in the list |
97 offset: offset in list which defines first item to return |
87 offset: offset in list which defines first item to return |
98 limit: max amount of items per page |
88 limit: max amount of items per page |
99 list_templates: templates that are used when rendering list |
89 list_templates: templates that are used when rendering list |
100 |
90 |
101 Returns: |
91 Returns: |
102 updated template context dict supplied by the caller or a new context |
92 A a dictionary with the following values set: |
103 dict if the caller supplied None. |
|
104 |
93 |
105 { |
94 { |
106 'list_data': list data to be displayed |
95 'data': list data to be displayed |
107 'list_main': url to list main template |
96 'main': url to list main template |
108 'list_pagination': url to list pagination template |
97 'pagination': url to list pagination template |
109 'list_row': url to list row template |
98 'row': url to list row template |
110 'list_heading': url to list heading template |
99 'heading': url to list heading template |
111 'limit': max amount of items per page, |
100 'limit': max amount of items per page, |
112 'newest': url to first page of the list |
101 'newest': url to first page of the list |
113 'prev': url to previous page |
102 'prev': url to previous page |
114 'next': url to next page |
103 'next': url to next page |
115 'first': offset of the first item in the list |
104 'first': offset of the first item in the list |
116 'last': offest of the lst item in the list |
105 'last': offest of the lst item in the list |
117 } |
106 } |
118 """ |
107 """ |
|
108 |
119 if not list_data: |
109 if not list_data: |
120 list_data = [] |
110 list_data = [] |
121 |
111 |
122 more = bool(list_data[limit:]) |
112 more = bool(list_data[limit:]) |
123 if more: |
113 if more: |
124 del list_data[limit:] |
114 del list_data[limit:] |
|
115 |
|
116 newest = '' |
|
117 next = '' |
|
118 prev = '' |
|
119 |
125 if more: |
120 if more: |
126 next = request.path + '?offset=%d&limit=%d' % (offset+limit, limit) |
121 next = request.path + '?offset=%d&limit=%d' % (offset+limit, limit) |
127 else: |
122 |
128 next = '' |
|
129 if offset > 0: |
123 if offset > 0: |
130 prev = request.path + '?offset=%d&limit=%d' % (max(0, offset-limit), limit) |
124 prev = request.path + '?offset=%d&limit=%d' % (max(0, offset-limit), limit) |
131 else: |
125 |
132 prev = '' |
|
133 newest = '' |
|
134 if offset > limit: |
126 if offset > limit: |
135 newest = request.path + '?limit=%d' % limit |
127 newest = request.path + '?limit=%d' % limit |
136 |
128 |
137 if not context: |
129 content = { |
138 context = {} |
130 'data': list_data, |
139 |
131 'description': description, |
140 context.update( |
132 'main': list_templates['list_main'], |
141 {'list_data': list_data, |
133 'pagination': list_templates['list_pagination'], |
142 'list_main': list_templates['list_main'], |
134 'row': list_templates['list_row'], |
143 'list_pagination': list_templates['list_pagination'], |
135 'heading': list_templates['list_heading'], |
144 'list_row': list_templates['list_row'], |
|
145 'list_heading': list_templates['list_heading'], |
|
146 'limit': limit, |
136 'limit': limit, |
147 'newest': newest, |
137 'newest': newest, |
148 'prev': prev, |
138 'prev': prev, |
149 'next': next, |
139 'next': next, |
150 'first': offset+1, |
140 'first': offset+1, |
151 'last': len(list_data) > 1 and offset+len(list_data) or None}) |
141 'last': len(list_data) > 1 and offset+len(list_data) or None |
152 |
142 } |
153 return context |
143 |
|
144 return content |
154 |
145 |
155 |
146 |
156 def makePaginationForm( |
147 def makePaginationForm( |
157 request, limit, arg_name='limit', choices=DEF_PAGINATION_CHOICES, |
148 request, limit, arg_name='limit', choices=DEF_PAGINATION_CHOICES, |
158 field_name_fmt=helper.forms.DEF_SELECT_QUERY_ARG_FIELD_NAME_FMT): |
149 field_name_fmt=helper.forms.DEF_SELECT_QUERY_ARG_FIELD_NAME_FMT): |