예제 #1
0
파일: pagination.py 프로젝트: Tapsa/Ferris
    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
예제 #2
0
    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
예제 #3
0
파일: handler.py 프로젝트: Tapsa/Ferris
    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))
예제 #4
0
    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))
예제 #5
0
파일: json.py 프로젝트: Tapsa/Ferris
    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)
예제 #6
0
    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)
예제 #7
0
    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()
예제 #8
0
    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()
예제 #9
0
파일: mixin.py 프로젝트: Tapsa/Ferris
    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()
예제 #10
0
    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)
예제 #11
0
    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)
예제 #12
0
파일: search.py 프로젝트: Tapsa/Ferris
    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
예제 #13
0
    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