Ejemplo n.º 1
0
 def __iter__(self):
     """
     override __iter__ to pull results from dogpile
     if particular attributes have been configured.
     """
     if hasattr(self, '_cache_region'):
         return self.get_value(createfunc=lambda: list(BaseQuery.__iter__(self)))
     else:
         return BaseQuery.__iter__(self)
Ejemplo n.º 2
0
 def __iter__(self):
     """
     override __iter__ to pull results from dogpile
     if particular attributes have been configured.
     """
     if hasattr(self, '_cache_region'):
         return self.get_value(
             createfunc=lambda: list(BaseQuery.__iter__(self)))
     else:
         return BaseQuery.__iter__(self)
Ejemplo n.º 3
0
def get_user_comments_in_news(id):
    page = request.args.get('page', 1, type=int)
    per_page = min(request.args.get('per_page', current_app.config['PROFILE_COMMENTS_PER_PAGE'],
                                    type=int), current_app.config['PROFILE_COMMENTS_PER_PAGE'])
    user = User.query.get_or_404(id)
    user_comments_in_news = (
        db.session.query(News.id,
                         News.title,
                         func.count(Comment.id),
                         func.max(Comment.modify_datetime).label('last_modified'))
        .join(Comment, News.id == Comment.entity_id)
        .filter(Comment.author == user)
        .group_by(News.id)
        .order_by(desc('last_modified'))
    )
    p = BaseQuery.paginate(user_comments_in_news, page, per_page)

    return {
        'paginator': {
            'page': page,
            'pages': p.pages,
        },
        'objects': [
            dict(zip(['news_id', 'news_title', 'comments_amount', 'last_modified_date'], map(str, x)))
            for x in p.items
        ],
    }
Ejemplo n.º 4
0
def get_user_comments_in_news(id):
    page = request.args.get('page', 1, type=int)
    per_page = min(
        request.args.get('per_page',
                         current_app.config['PROFILE_COMMENTS_PER_PAGE'],
                         type=int),
        current_app.config['PROFILE_COMMENTS_PER_PAGE'])
    user = User.query.get_or_404(id)
    user_comments_in_news = (db.session.query(
        News.id, News.title, func.count(Comment.id),
        func.max(Comment.modify_datetime).label('last_modified')).join(
            Comment, News.id == Comment.entity_id).filter(
                Comment.author == user).group_by(News.id).order_by(
                    desc('last_modified')))
    p = BaseQuery.paginate(user_comments_in_news, page, per_page)

    return {
        'paginator': {
            'page': page,
            'pages': p.pages,
        },
        'objects': [
            dict(
                zip([
                    'news_id', 'news_title', 'comments_amount',
                    'last_modified_date'
                ], map(str, x))) for x in p.items
        ],
    }
Ejemplo n.º 5
0
    def __iter__(self):
        """
        Override __iter__ to pull results from cache if particular
        attributes have been configured.

        This approach does *not* detach the loaded objects from the current
        session. If the cache backend is an in-process cache (like "memory")
        and lives beyond the scope of the current session's transaction, those
        objects may be expired.

        The method here can be modified to first expunge() each loaded item
        from the current session before returning the list of items, so that
        the items in the cache are not the same ones in the current Session.
        """
        if hasattr(self, '_cache'):
            func = lambda: list(BaseQuery.__iter__(self))
            return iter(self.get_value(createfunc=func))
        else:
            return BaseQuery.__iter__(self)
Ejemplo n.º 6
0
    def __iter__(self):
        """
        Override __iter__ to pull results from cache if particular
        attributes have been configured.

        This approach does *not* detach the loaded objects from the current
        session. If the cache backend is an in-process cache (like "memory")
        and lives beyond the scope of the current session's transaction, those
        objects may be expired.

        The method here can be modified to first expunge() each loaded item
        from the current session before returning the list of items, so that
        the items in the cache are not the same ones in the current Session.
        """
        if hasattr(self, '_cache'):
            func = lambda: list(BaseQuery.__iter__(self))
            return iter(self.get_value(createfunc=func))
        else:
            return BaseQuery.__iter__(self)
Ejemplo n.º 7
0
def get_user_news(id):
    page = request.args.get('page', 1, type=int)
    per_page = min(request.args.get('per_page', current_app.config['PROFILE_NEWS_PER_PAGE'],
                                    type=int), current_app.config['PROFILE_NEWS_PER_PAGE'])
    user = User.query.get_or_404(id)
    user_news = (
        db.session.query(News.id, News.title, News.datetime)
        .filter(News.author == user)
        .order_by(News.datetime.desc())
    )
    p = BaseQuery.paginate(user_news, page, per_page)

    return {
        'paginator': {
            'page': page,
            'pages': p.pages,
        },
        'objects': [dict(zip(['news_id', 'news_title', 'created_date'], map(str, x))) for x in p.items],
    }
Ejemplo n.º 8
0
def get_user_news(id):
    page = request.args.get('page', 1, type=int)
    per_page = min(
        request.args.get('per_page',
                         current_app.config['PROFILE_NEWS_PER_PAGE'],
                         type=int),
        current_app.config['PROFILE_NEWS_PER_PAGE'])
    user = User.query.get_or_404(id)
    user_news = (db.session.query(News.id, News.title, News.datetime).filter(
        News.author == user).order_by(News.datetime.desc()))
    p = BaseQuery.paginate(user_news, page, per_page)

    return {
        'paginator': {
            'page': page,
            'pages': p.pages,
        },
        'objects': [
            dict(zip(['news_id', 'news_title', 'created_date'], map(str, x)))
            for x in p.items
        ],
    }
Ejemplo n.º 9
0
    def _query(self):
        if not self.model:
            raise NotImplementedError()

        return BaseQuery(self.model, self.model.query.session)
Ejemplo n.º 10
0
 def __iter__(self):
     if hasattr(self, '_cache_region'):
         return self.get_value(createfunc=lambda: list(BaseQuery.__iter__(self)))
     else:
         return BaseQuery.__iter__(self)
Ejemplo n.º 11
0
 def __init__(self, regions, *args, **kw):
     self.cache_regions = regions
     BaseQuery.__init__(self, *args, **kw)
Ejemplo n.º 12
0
 def __init__(self, entities, *args, **kw):
     BaseQuery.__init__(self, entities=entities, *args, **kw)
Ejemplo n.º 13
0
 def __iter__(self):
     if hasattr(self, '_cache_region'):
         return self.get_value(
             createfunc=lambda: list(BaseQuery.__iter__(self)))
     else:
         return BaseQuery.__iter__(self)
Ejemplo n.º 14
0
 def __init__(self, regions, *args, **kw):
     self.cache_regions = regions
     BaseQuery.__init__(self, *args, **kw)