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
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 {}
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
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)))
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) ))
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
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
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
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
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
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
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'))
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)))
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)))