예제 #1
0
    def during(self, begin, end):
        """Return a query for all events that overlap either the beginning, the end
        or both

        :param begin: the beginning of the timeframe
        :param end: the end of the timeframe
        """
        q = self.filter(
            db.or_(db.and_(Event.start_date >= begin, Event.start_date <= end),
                   db.and_(Event.end_date >= begin,
                           Event.end_date <= end))).order_by(Event.start_date)
        return q
예제 #2
0
 def get_user_votes_on(self, user_id, entry_ids):
     if entry_ids:
         user_votes = defaultdict(int, dict(
             db.session.query(Vote.entry_id, Vote.score) \
                       .filter(db.and_(Vote.user_id == user_id,
                                       Vote.entry_id.in_(entry_ids)))))
         return user_votes
     else:
         return {}
예제 #3
0
    def during(self, begin, end):
        """Return a query for all events that overlap either the beginning, the end
        or both

        :param begin: the beginning of the timeframe
        :param end: the end of the timeframe
        """
        q = self.filter(db.or_(
            db.and_(
                Event.start_date >= begin,
                Event.start_date <= end
            ),
            db.and_(
                Event.end_date >= begin,
                Event.end_date <= end
            )
        )).order_by(Event.start_date)
        return q
예제 #4
0
 def get_user_votes_on(self, user_id, entry_ids):
     if entry_ids:
         user_votes = defaultdict(int, dict(
             db.session.query(Vote.entry_id, Vote.score) \
                       .filter(db.and_(Vote.user_id == user_id,
                                       Vote.entry_id.in_(entry_ids)))))
         return user_votes
     else:
         return {}
예제 #5
0
 def by_date(self, year, month=None, day=None):
     """Filter all the items that match the given date."""
     if month is None:
         return self.filter(
             db.and_(Article.pub_date >= datetime(year, 1, 1),
                     Article.pub_date < datetime(year + 1, 1, 1)))
     elif day is None:
         return self.filter(
             db.and_(
                 Article.pub_date >= datetime(year, month, 1),
                 Article.pub_date <
                 (month == 12 and datetime(year + 1, 1, 1)
                  or datetime(year, month + 1, 1))))
     return self.filter(
         db.and_(
             Article.pub_date >= datetime(year, month, day),
             Article.pub_date <
             datetime(year, month, day) + timedelta(days=1)))
예제 #6
0
 def by_date(self, year, month=None, day=None):
     """Filter all the items that match the given date."""
     if month is None:
         return self.filter(db.and_(
             Article.pub_date >= datetime(year, 1, 1),
             Article.pub_date < datetime(year + 1, 1, 1)
         ))
     elif day is None:
         return self.filter(db.and_(
             Article.pub_date >= datetime(year, month, 1),
             Article.pub_date < (month == 12 and
                            datetime(year + 1, 1, 1) or
                            datetime(year, month + 1, 1))
         ))
     return self.filter(db.and_(
         Article.pub_date >= datetime(year, month, day),
         Article.pub_date < datetime(year, month, day) +
                          timedelta(days=1)
     ))
예제 #7
0
    def public(self):
        """Return a query for all published articles.

        An article is either published if :attr:`Article.public` is `True`
        and :attr:`Article.pub_date` is less or the same than current UTC.
        """
        q = self.filter(db.and_(
            Article.public == True,
            Article.pub_date <= datetime.utcnow(),
        ))
        return q
예제 #8
0
    def public(self):
        """Return a query for all published articles.

        An article is either published if :attr:`Article.public` is `True`
        and :attr:`Article.pub_date` is less or the same than current UTC.
        """
        q = self.filter(
            db.and_(
                Article.public == True,
                Article.pub_date <= datetime.utcnow(),
            ))
        return q
예제 #9
0
    def end_in(self, year, month):
        """Return a query for all events that end in the given year and month

        :param year: The year that the event ends have to match
        :param month: The month that the event ends have to match
        """
        days_in_month = calendar.monthrange(year, month)[1]
        interval_begin = datetime(year, month, 1, 0, 0, 0)
        interval_end = datetime(year, month, days_in_month, 23, 59, 59)
        q = self.filter(
            db.and_(Event.end_date >= interval_begin,
                    Event.end_date <= interval_end)).order_by(Event.end_date)
        return q
예제 #10
0
    def oncoming(self, year, month, duration=10):
        """Return a query for all events that start in the given year and month

        :param year: The year that the event starts have to match
        :param month: The month that the event starts have to match
        :param duration: The number of days from event start
        """
        interval_begin = datetime(year, month, 1, 0, 0, 0)
        interval_end = interval_begin + timedelta(duration)
        q = self.filter(db.and_(
            Event.start_date >= interval_begin,
            Event.start_date <= interval_end
        )).order_by(Event.start_date)
        return q
예제 #11
0
    def end_in(self, year, month):
        """Return a query for all events that end in the given year and month

        :param year: The year that the event ends have to match
        :param month: The month that the event ends have to match
        """
        days_in_month = calendar.monthrange(year, month)[1]
        interval_begin = datetime(year, month, 1, 0, 0, 0)
        interval_end = datetime(year, month, days_in_month, 23, 59, 59)
        q = self.filter(db.and_(
            Event.end_date >= interval_begin,
            Event.end_date <= interval_end
        )).order_by(Event.end_date)
        return q
예제 #12
0
    def oncoming(self, year, month, duration=10):
        """Return a query for all events that start in the given year and month

        :param year: The year that the event starts have to match
        :param month: The month that the event starts have to match
        :param duration: The number of days from event start
        """
        interval_begin = datetime(year, month, 1, 0, 0, 0)
        interval_end = interval_begin + timedelta(duration)
        q = self.filter(
            db.and_(Event.start_date >= interval_begin,
                    Event.start_date <= interval_end)).order_by(
                        Event.start_date)
        return q
예제 #13
0
 def get_taggable_content(self, tag):
     return Question.query.order_by(Question.score, Question.view_count) \
                          .filter(db.and_(
         question_tag.c.tag_id == tag.id,
         question_tag.c.question_id == Question.id
     )).options(db.noload('votes'), db.noload('author'))
예제 #14
0
 def tagged(self, tags):
     """Filter questions by tags."""
     tag_ids = {t.id for t in tags}
     return self.filter(db.and_(
         Question.id == question_tag.c.question_id,
         question_tag.c.tag_id.in_(tag_ids)))
예제 #15
0
 def get_taggable_content(self, tag):
     return Question.query.order_by(Question.score, Question.view_count) \
                          .filter(db.and_(
         question_tag.c.tag_id == tag.id,
         question_tag.c.question_id == Question.id
     )).options(db.noload('votes'), db.noload('author'))
예제 #16
0
 def tagged(self, tags):
     """Filter questions by tags."""
     tag_ids = {t.id for t in tags}
     return self.filter(
         db.and_(Question.id == question_tag.c.question_id,
                 question_tag.c.tag_id.in_(tag_ids)))