comparison roundup/rest.py @ 5591:a25d79e874cb REST-rebased

Added filtering and pagination Adjust unittest to use empty cgi formfield instead of empty dict committer: Ralf Schlatterbeck <rsc@runtux.com>
author Chau Nguyen <dangchau1991@yahoo.com>
date Wed, 30 Jan 2019 10:26:35 +0100
parents 4d8746c73fdb
children 344b6a87dac6
comparison
equal deleted inserted replaced
5590:4d8746c73fdb 5591:a25d79e874cb
176 ): 176 ):
177 raise Unauthorised('Permission to view %s denied' % class_name) 177 raise Unauthorised('Permission to view %s denied' % class_name)
178 178
179 class_obj = self.db.getclass(class_name) 179 class_obj = self.db.getclass(class_name)
180 class_path = self.base_path + class_name 180 class_path = self.base_path + class_name
181
182 # Handle filtering and pagination
183 filter_props = {}
184 page = {
185 'size': None,
186 'index': None
187 }
188 for form_field in input.value:
189 key = form_field.name
190 value = form_field.value
191 if key.startswith("where_"): # serve the filter purpose
192 key = key[6:]
193 filter_props[key] = [
194 getattr(self.db, key).lookup(p)
195 for p in value.split(",")
196 ]
197 elif key.startswith("page_"): # serve the paging purpose
198 key = key[5:]
199 value = int(value)
200 page[key] = value
201
202 if not filter_props:
203 obj_list = class_obj.list()
204 else:
205 obj_list = class_obj.filter(None, filter_props)
206
207 # extract result from data
181 result = [ 208 result = [
182 {'id': item_id, 'link': class_path + item_id} 209 {'id': item_id, 'link': class_path + item_id}
183 for item_id in class_obj.list() 210 for item_id in obj_list
184 if self.db.security.hasPermission( 211 if self.db.security.hasPermission(
185 'View', self.db.getuid(), class_name, itemid=item_id 212 'View', self.db.getuid(), class_name, itemid=item_id
186 ) 213 )
187 ] 214 ]
215
216 # pagination
217 if page['size'] is not None and page['index'] is not None:
218 page_start = max(page['index'] * page['size'], 0)
219 page_end = min(page_start + page['size'], len(result))
220 result = result[page_start:page_end]
221
188 self.client.setHeader("X-Count-Total", str(len(result))) 222 self.client.setHeader("X-Count-Total", str(len(result)))
189 return 200, result 223 return 200, result
190 224
191 @_data_decorator 225 @_data_decorator
192 def get_element(self, class_name, item_id, input): 226 def get_element(self, class_name, item_id, input):
805 output = getattr( 839 output = getattr(
806 self, "%s_attribute" % method.lower() 840 self, "%s_attribute" % method.lower()
807 )(class_name, item_id, uri_split[2], input) 841 )(class_name, item_id, uri_split[2], input)
808 842
809 # Format the content type 843 # Format the content type
810 if format_output.lower() == "json": 844 if data_type.lower() == "json":
811 self.client.setHeader("Content-Type", "application/json") 845 self.client.setHeader("Content-Type", "application/json")
812 if pretty_output: 846 if pretty_output:
813 indent = 4 847 indent = 4
814 else: 848 else:
815 indent = None 849 indent = None

Roundup Issue Tracker: http://roundup-tracker.org/