Пример #1
0
 def prev(self):
     if self.current_page is None:
         raise DbapiError('Can not go back more, at first page')
     if self.page_index == 0:
         # At the beginning of the current page, move to next...
         self.current_page = self.page_iterator.prev()
         self.page_index = len(self.current_page)
         if self.page_index == 0:
             raise DbapiError('No more items')
     self.page_index -= 1
     self.count -= 1
     return self.current_page[self.page_index]
Пример #2
0
 def prev(self):
     if self.prev_cursor == 0:
         raise DbapiError('Can not page back more, at first page')
     data, self.next_cursor, self.prev_cursor = self.method(
         cursor=self.prev_cursor, *self.args, **self.kargs)
     self.count -= 1
     return data
Пример #3
0
 def __init__(self, method, *args, **kargs):
     if hasattr(method, 'pagination_mode'):
         if method.pagination_mode == 'cursor':
             self.iterator = CursorIterator(method, args, kargs)
         else:
             self.iterator = PageIterator(method, args, kargs)
     else:
         raise DbapiError('This method does not perform pagination')
Пример #4
0
        def build_parameters(self, args, kargs):
            self.parameters = {}
            for idx, arg in enumerate(args):
                if arg is None:
                    continue

                try:
                    self.parameters[self.allowed_param[idx]] = convert_to_utf8_str(arg)
                except IndexError:
                    raise DbapiError('Too many parameters supplied!')

            for k, arg in kargs.items():
                if arg is None:
                    continue
                if k in self.parameters:
                    raise DbapiError('Multiple values for parameter %s supplied!' % k)

                self.parameters[k] = convert_to_utf8_str(arg)
Пример #5
0
    def execute(self, method):
        """generate mongodb requests and returns model objects

        This function is a combination of Dbapi's http-request and parse().
        """
        method.api.clear()
        try:
            if method.payload_type is None: return
            model = getattr(self.model_factory, method.payload_type)
        except AttributeError:
            raise DbapiError('No model for this payload type: %s' %
                             method.payload_type)

        #@todo critical!: how to avoid dumps+loads while still remain
        #invoking parse()?
        #@todo-fixed: seems no need to fix now!
        payload = self.json_lib.dumps(
            model.handler(method.path, method.api, method.parameters))

        return self.parse(method, payload)
Пример #6
0
    def parse(self, method, payload):
        try:
            if method.payload_type is None: return
            model = getattr(self.model_factory, method.payload_type)
        except AttributeError:
            raise DbapiError('No model for this payload type: %s' %
                             method.payload_type)

        json = JSONParser.parse(self, method, payload)
        if isinstance(json, tuple):
            json, cursors = json
        else:
            cursors = None

        if method.payload_list:
            result = model.parse_list(method.api, json)
        else:
            result = model.parse(method.api, json)

        if cursors:
            return result, cursors
        else:
            return result
Пример #7
0
 def parse(self, method, payload):
     try:
         json = self.json_lib.loads(payload)
     except Exception, e:
         raise DbapiError('Failed to parse JSON payload: %s' % e)
Пример #8
0
 def prev(self):
     if (self.current_page == 1):
         raise DbapiError('Can not page back more, at first page')
     self.current_page -= 1
     return self.method(page=self.current_page, *self.args, **self.kargs)