def paginate(self, query_or_var_name=None): """ Paginates a query and sets up the appropriate template variables. Uses ``handler.paginate_limit`` to determine how many items per page, or defaults to 10 if omitted. Sets the ``paging`` template variable to a dictionary like:: { "cursor": "abc...", "next_cursor": "nzb...", "limit": 10 } Returns the data, and if ``query_or_var_name`` is a string, sets that template variable. """ if not query_or_var_name: query_or_var_name = inflector.pluralize(self.handler.name) if isinstance(query_or_var_name, basestring): query = self.handler.get(query_or_var_name) else: query = query_or_var_name if not isinstance(query, (db.Query, ndb.Query)): logging.debug('Unable to paginate, query is not a Query or ndb Query object') return cursor = self.handler.request.params.get('cursor', None) default_limit = 10 if hasattr(self.handler, 'paginate_limit'): default_limit = self.handler.paginate_limit limit = int(self.handler.request.params.get('limit', default_limit)) if hasattr(self.handler, 'max_paginate_limit'): if limit > self.handler.max_paginate_limit: limit = self.handler.max_paginate_limit if isinstance(query, db.Query): data = self.paginate_db(query, cursor, limit) elif isinstance(query, ndb.Query): data = self.paginate_ndb(query, cursor, limit) if isinstance(query_or_var_name, basestring): self.handler.set(query_or_var_name, data) else: self.handler.set(inflector.pluralize(self.handler.name), data) return data
def list(self): """ Passes a list of all of the entities for the model to the template. If your handler is Cats, it does:: self.set(cats=Cat.query()) The list of entities is always at ``inflector.pluralize(self.name)``, so 'cats', 'posts', etc. """ self.set(inflector.pluralize(self.name), ndb_util.list(self.Model))
def __init__(self, handler): self.handler = handler self.render_as_json = False self.indent = None self.try_vars = [ 'data', inflector.pluralize(self.handler.name), inflector.singularize(self.handler.name), 'edited_item', 'added_item', 'item'] self.handler.events.before_dispatch += self.before_dispatch_callback.__get__(self) self.handler.events.after_dispatch += self.after_dispatch_callback.__get__(self)
def _scaffold_on_before_render(self, handler): self.template_vars['scaffolding'] = { 'name': self.name, 'proper_name': self.proper_name, 'title': inflector.titleize(self.proper_name), 'pluralized': inflector.pluralize(self.name), 'singularized': inflector.singularize(self.name), 'key_id_for': ndb_util.key_id_for, 'key_urlsafe_for': ndb_util.key_urlsafe_for, 'url_id_for': self.url_id_for, 'use_ids': self.scaffold.use_ids, 'form_action': self.scaffold.form_action, 'form_encoding': self.scaffold.form_encoding } self._determine_display_properties()
def _scaffold_on_before_render(self, handler): self.template_vars["scaffolding"] = { "name": self.name, "proper_name": self.proper_name, "title": inflector.titleize(self.proper_name), "pluralized": inflector.pluralize(self.name), "singularized": inflector.singularize(self.name), "key_id_for": ndb_util.key_id_for, "key_urlsafe_for": ndb_util.key_urlsafe_for, "url_id_for": self.url_id_for, "use_ids": self.scaffold.use_ids, "form_action": self.scaffold.form_action, "form_encoding": self.scaffold.form_encoding, } self._determine_display_properties()
def process_list(self): data = {} if self.paginate: paging = self.handler.get('paging') if not paging and 'pagination' in self.handler.components: self.handler.components.pagination.paginate() paging = self.handler.get('paging') if paging: data.update({ 'itemsPerPage': paging['limit'], 'cursor': paging['cursor'], 'nextCursor': paging['next_cursor'] if paging['next_cursor'] else None, 'nextLink': self.handler.uri(cursor=paging['next_cursor'], _pass_all=True, _full=True) if paging['next_cursor'] else None, }) items = self.handler.get(inflector.pluralize(self.handler.name), self.handler.get('data', None)) if isinstance(items, (db.Query, ndb.Query)): item_count = items.count() else: item_count = len(items) data.update({ 'itemCount': item_count, 'selfLink': self.handler.uri(_pass_all=True, _full=True), 'items': items }) self.handler.set(data=data)
def process_list(self): data = {} if self.paginate: paging = self.handler.get('paging') if not paging and 'pagination' in self.handler.components: self.handler.components.pagination.paginate() paging = self.handler.get('paging') if paging: data.update({ 'itemsPerPage': paging['limit'], 'cursor': paging['cursor'], 'nextCursor': paging['next_cursor'] if paging['next_cursor'] else None, 'nextLink': self.handler.uri( cursor=paging['next_cursor'], _pass_all=True, _full=True) if paging['next_cursor'] else None, }) items = self.handler.get( inflector.pluralize(self.handler.name), self.handler.get('data', None)) if isinstance(items, (db.Query, ndb.Query)): item_count = items.count() else: item_count = len(items) data.update({ 'itemCount': item_count, 'selfLink': self.handler.uri(_pass_all=True, _full=True), 'items': items }) self.handler.set(data=data)
def search(self, index=None): """ Searches using the provided index (or an automatically determine one). Expects the search query to be in the ``query`` request parameter. Also takes care of setting pagination information if the :class:`pagination component <ferris.components.pagination.Pagnation>` is present. """ Model = self.handler.Model limit = 50 if hasattr(self.handler, 'paginate_limit'): limit = self.handler.paginate_limit try: query_string = self.handler.request.params.get('query', '') cursor = self.handler.request.params.get('cursor', None) if cursor: cursor = search.Cursor(web_safe_string=cursor) else: cursor = search.Cursor() options = search.QueryOptions( limit=limit, ids_only=True, cursor=cursor) query = search.Query(query_string=query_string, options=options) if not index: if hasattr(Model, 'get_search_index'): index = Model.get_search_index() elif hasattr(Model, 'search_index_name'): index = Model.search_index_name else: index = 'auto_ix_%s' % Model._get_kind() index = search.Index(name=index) logging.debug("Searching %s with \"%s\" and cursor %s" % (index.name, query.query_string, cursor.web_safe_string)) index_results = index.search(query) if issubclass(Model, ndb.Model): results = ndb.get_multi([ndb.Key(urlsafe=x.doc_id) for x in index_results]) results = [x for x in results if x] else: results = Model.get([x.doc_id for x in index_results]) Model.prefetch_references(results) if index_results.cursor: self.handler.set(paging={ 'limit': limit, 'cursor': cursor.web_safe_string, 'next_cursor': str(index_results.cursor.web_safe_string)}) except (search.Error, search.query_parser.QueryException): results = [] self.handler.set(error=True) self.handler.set(query_string=query_string) self.handler.set(inflector.pluralize(self.handler.name), results) return results
def search(self, index=None): """ Searches using the provided index (or an automatically determine one). Expects the search query to be in the ``query`` request parameter. Also takes care of setting pagination information if the :class:`pagination component <ferris.components.pagination.Pagnation>` is present. """ Model = self.handler.Model limit = 50 if hasattr(self.handler, 'paginate_limit'): limit = self.handler.paginate_limit try: query_string = self.handler.request.params.get('query', '') cursor = self.handler.request.params.get('cursor', None) if cursor: cursor = search.Cursor(web_safe_string=cursor) else: cursor = search.Cursor() options = search.QueryOptions(limit=limit, ids_only=True, cursor=cursor) query = search.Query(query_string=query_string, options=options) if not index: if hasattr(Model, 'get_search_index'): index = Model.get_search_index() elif hasattr(Model, 'search_index_name'): index = Model.search_index_name else: index = 'auto_ix_%s' % Model._get_kind() index = search.Index(name=index) logging.debug( "Searching %s with \"%s\" and cursor %s" % (index.name, query.query_string, cursor.web_safe_string)) index_results = index.search(query) if issubclass(Model, ndb.Model): results = ndb.get_multi( [ndb.Key(urlsafe=x.doc_id) for x in index_results]) results = [x for x in results if x] else: results = Model.get([x.doc_id for x in index_results]) Model.prefetch_references(results) if index_results.cursor: self.handler.set( paging={ 'limit': limit, 'cursor': cursor.web_safe_string, 'next_cursor': str( index_results.cursor.web_safe_string) }) except (search.Error, search.query_parser.QueryException): results = [] self.handler.set(error=True) self.handler.set(query_string=query_string) self.handler.set(inflector.pluralize(self.handler.name), results) return results