def _process(self): # Current events, which are always shown by default are events of this month and of the previous month. # If there are no events in this range, it will include the last and next month containing events. past_threshold = self.now - relativedelta(months=1, day=1, hour=0, minute=0) future_threshold = self.now + relativedelta(months=1, day=1, hour=0, minute=0) next_event_start_dt = (db.session.query(Event.start_dt) .filter(Event.start_dt >= self.now, Event.category_id == self.category.id) .order_by(Event.start_dt.asc()) .first() or (None,))[0] previous_event_start_dt = (db.session.query(Event.start_dt) .filter(Event.start_dt < self.now, Event.category_id == self.category.id) .order_by(Event.start_dt.desc()) .first() or (None,))[0] if next_event_start_dt is not None and next_event_start_dt > future_threshold: future_threshold = next_event_start_dt + relativedelta(months=1, day=1, hour=0, minute=0) if previous_event_start_dt is not None and previous_event_start_dt < past_threshold: past_threshold = previous_event_start_dt.replace(day=1, hour=0, minute=0) event_query = (Event.query.with_parent(self.category) .options(*self._event_query_options) .order_by(Event.start_dt.desc())) past_event_query = event_query.filter(Event.start_dt < past_threshold) future_event_query = event_query.filter(Event.start_dt >= future_threshold) current_event_query = event_query.filter(Event.start_dt >= past_threshold, Event.start_dt < future_threshold) events = current_event_query.filter(Event.start_dt < future_threshold).all() events_by_month = self.group_by_month(events) future_event_count = future_event_query.count() past_event_count = past_event_query.count() show_past_events = bool(self.category.id in session.get('fetch_past_events_in', set()) or (session.user and session.user.settings.get('show_past_events', False))) managers = sorted(self.category.get_manager_list(), key=attrgetter('principal_type.name', 'name')) threshold_format = '%Y-%m' params = {'event_count': len(events), 'events_by_month': events_by_month, 'format_event_date': self.format_event_date, 'future_event_count': future_event_count, 'future_threshold': future_threshold.strftime(threshold_format), 'happening_now': self.happening_now, 'is_recent': self.is_recent, 'managers': managers, 'past_event_count': past_event_count, 'show_past_events': show_past_events, 'past_threshold': past_threshold.strftime(threshold_format), 'atom_feed_url': url_for('.export_atom', self.category), 'atom_feed_title': _('Events of "{}"').format(self.category.title)} params.update(get_base_ical_parameters(session.user, 'category', '/export/categ/{0}.ics'.format(self.category.id), {'from': '-31d'})) if not self.category.is_root: return WPCategory.render_template('display/category.html', self.category, **params) news = get_recent_news() upcoming_events = get_upcoming_events() return WPCategory.render_template('display/root_category.html', self.category, news=news, upcoming_events=upcoming_events, **params)
def _process(self): params = get_category_view_params(self.category, self.now) if not self.category.is_root: return WPCategory.render_template('display/category.html', self.category, **params) news = get_recent_news() upcoming_events = get_upcoming_events() return WPCategory.render_template('display/root_category.html', self.category, news=news, upcoming_events=upcoming_events, **params)
def _process(self): info = get_category_timetable([self.category.id], self.start_dt, self.end_dt, detail_level=self.detail, tz=self.category.display_tzinfo, from_categ=self.category, grouped=False) events = info['events'] # Only categories with icons are listed in the sidebar subcategories = {event.category for event in events if event.category.has_icon} # Events spanning multiple days must appear on all days events = _flat_map(partial(self._process_multiday_events, info), events) def _event_sort_key(event): # Ongoing events are shown after all other events on the same day and are sorted by start_date ongoing = getattr(event, 'ongoing', False) return (event.start_dt.date(), ongoing, -mktime(event.first_occurence_start_dt.timetuple()) if ongoing else event.start_dt.time()) events = sorted(events, key=_event_sort_key) params = { 'detail': self.detail, 'period': self.period, 'subcategories': subcategories, 'start_dt': self.start_dt, 'end_dt': self.end_dt - relativedelta(days=1), # Display a close-ended interval 'previous_day_url': self._other_day_url(self.start_dt - relativedelta(days=1)), 'next_day_url': self._other_day_url(self.start_dt + relativedelta(days=1)), 'previous_month_url': self._other_day_url(self.start_dt - relativedelta(months=1)), 'next_month_url': self._other_day_url(self.start_dt + relativedelta(months=1)), 'previous_year_url': self._other_day_url(self.start_dt - relativedelta(years=1)), 'next_year_url': self._other_day_url(self.start_dt + relativedelta(years=1)), 'mathjax': self.detail != 'event' } if self.period == 'day': return WPCategory.render_template('display/overview/day.html', self.category, events=events, **params) elif self.period == 'week': days = self._get_week_days() template = 'display/overview/week.html' params['previous_week_url'] = self._other_day_url(self.start_dt - relativedelta(days=7)) params['next_week_url'] = self._other_day_url(self.start_dt + relativedelta(days=7)) elif self.period == 'month': days = self._get_calendar_days() template = 'display/overview/month.html' events_by_day = [] for day in days: events_by_day.append((day, self._pop_head_while(lambda x: x.start_dt.date() <= day.date(), events))) # Check whether all weekends are empty hide_weekend = (not any(map(itemgetter(1), events_by_day[5::7])) and not any(map(itemgetter(1), events_by_day[6::7]))) if hide_weekend: events_by_day = [x for x in events_by_day if x[0].weekday() not in (5, 6)] return WPCategory.render_template(template, self.category, events_by_day=events_by_day, hide_weekend=hide_weekend, **params)
def _process(self): if not request.is_xhr: return WPCategory.render_template( 'display/calendar.html', self.category, start_dt=request.args.get('start_dt')) tz = self.category.display_tzinfo start = tz.localize(dateutil.parser.parse( request.args['start'])).astimezone(utc) end = tz.localize(dateutil.parser.parse( request.args['end'])).astimezone(utc) query = (Event.query.filter(Event.starts_between(start, end), Event.is_visible_in(self.category), ~Event.is_deleted).options( load_only('id', 'title', 'start_dt', 'end_dt', 'category_id'))) events = self._get_event_data(query) ongoing_events = (Event.query.filter( Event.is_visible_in(self.category), Event.start_dt < start, Event.end_dt > end).options( load_only('id', 'title', 'start_dt', 'end_dt', 'timezone')).order_by(Event.title).all()) return jsonify_data( flash=False, events=events, ongoing_event_count=len(ongoing_events), ongoing_events_html=self._render_ongoing_events(ongoing_events))
def _process(self): if request.accept_mimetypes.best_match( ('application/json', 'text/html')) == 'application/json': return redirect( url_for('categories.statistics_json', category_id=self.category.id)) return WPCategory.render_template('category_statistics.html', self.category)
def _process(self): if not request.is_xhr: return WPCategory.render_template('display/calendar.html', self.category, start_dt=request.args.get('start_dt')) tz = self.category.display_tzinfo start = tz.localize(dateutil.parser.parse(request.args['start'])).astimezone(utc) end = tz.localize(dateutil.parser.parse(request.args['end'])).astimezone(utc) query = (Event.query .filter(Event.starts_between(start, end), Event.is_visible_in(self.category), ~Event.is_deleted) .options(load_only('id', 'title', 'start_dt', 'end_dt', 'category_id'))) events = self._get_event_data(query) ongoing_events = (Event.query .filter(Event.is_visible_in(self.category), Event.start_dt < start, Event.end_dt > end) .options(load_only('id', 'title', 'start_dt', 'end_dt', 'timezone')) .order_by(Event.title) .all()) return jsonify_data(flash=False, events=events, ongoing_event_count=len(ongoing_events), ongoing_events_html=self._render_ongoing_events(ongoing_events))
def _process(self): info = self._get_timetable() events = info['events'] # Only categories with icons are listed in the sidebar subcategory_ids = {event.category.effective_icon_data['source_id'] for event in events if event.category.has_effective_icon} subcategories = Category.query.filter(Category.id.in_(subcategory_ids)).all() # Events spanning multiple days must appear on all days events = _flat_map(partial(self._process_multiday_events, info), events) def _event_sort_key(event): # Ongoing events are shown after all other events on the same day and are sorted by start_date ongoing = getattr(event, 'ongoing', False) return (event.start_dt.date(), ongoing, -mktime(event.first_occurence_start_dt.timetuple()) if ongoing else event.start_dt.time()) events = sorted(events, key=_event_sort_key) params = { 'detail': self.detail, 'period': self.period, 'subcategories': subcategories, 'start_dt': self.start_dt, 'end_dt': self.end_dt - relativedelta(days=1), # Display a close-ended interval 'previous_day_url': self._other_day_url(self.start_dt - relativedelta(days=1)), 'next_day_url': self._other_day_url(self.start_dt + relativedelta(days=1)), 'previous_month_url': self._other_day_url(self.start_dt - relativedelta(months=1)), 'next_month_url': self._other_day_url(self.start_dt + relativedelta(months=1)), 'previous_year_url': self._other_day_url(self.start_dt - relativedelta(years=1)), 'next_year_url': self._other_day_url(self.start_dt + relativedelta(years=1)), 'mathjax': True } if self.detail != 'event': cte = self.category.get_protection_parent_cte() params['accessible_categories'] = {cat_id for cat_id, prot_parent_id in db.session.query(cte) if prot_parent_id == self.category.id} if self.period == 'day': return WPCategory.render_template('display/overview/day.html', self.category, events=events, **params) elif self.period == 'week': days = self._get_week_days() template = 'display/overview/week.html' params['previous_week_url'] = self._other_day_url(self.start_dt - relativedelta(days=7)) params['next_week_url'] = self._other_day_url(self.start_dt + relativedelta(days=7)) elif self.period == 'month': days = self._get_calendar_days() template = 'display/overview/month.html' events_by_day = [] for day in days: events_by_day.append((day, self._pop_head_while(lambda x: x.start_dt.date() <= day.date(), events))) # Check whether all weekends are empty hide_weekend = (not any(map(itemgetter(1), events_by_day[5::7])) and not any(map(itemgetter(1), events_by_day[6::7]))) if hide_weekend: events_by_day = [x for x in events_by_day if x[0].weekday() not in (5, 6)] return WPCategory.render_template(template, self.category, events_by_day=events_by_day, hide_weekend=hide_weekend, **params)
def _process(self): info = get_category_timetable( [self.category.id], self.start_dt, self.end_dt, detail_level=self.detail, tz=self.category.display_tzinfo, from_categ=self.category, grouped=False, ) events = info["events"] # Only categories with icons are listed in the sidebar subcategory_ids = { event.category.effective_icon_data["source_id"] for event in events if event.category.has_effective_icon } subcategories = Category.query.filter(Category.id.in_(subcategory_ids)) # Events spanning multiple days must appear on all days events = _flat_map(partial(self._process_multiday_events, info), events) def _event_sort_key(event): # Ongoing events are shown after all other events on the same day and are sorted by start_date ongoing = getattr(event, "ongoing", False) return ( event.start_dt.date(), ongoing, -mktime(event.first_occurence_start_dt.timetuple()) if ongoing else event.start_dt.time(), ) events = sorted(events, key=_event_sort_key) params = { "detail": self.detail, "period": self.period, "subcategories": subcategories, "start_dt": self.start_dt, "end_dt": self.end_dt - relativedelta(days=1), # Display a close-ended interval "previous_day_url": self._other_day_url(self.start_dt - relativedelta(days=1)), "next_day_url": self._other_day_url(self.start_dt + relativedelta(days=1)), "previous_month_url": self._other_day_url(self.start_dt - relativedelta(months=1)), "next_month_url": self._other_day_url(self.start_dt + relativedelta(months=1)), "previous_year_url": self._other_day_url(self.start_dt - relativedelta(years=1)), "next_year_url": self._other_day_url(self.start_dt + relativedelta(years=1)), "mathjax": True, } if self.detail != "event": cte = self.category.get_protection_parent_cte() params["accessible_categories"] = { cat_id for cat_id, prot_parent_id in db.session.query(cte) if prot_parent_id == self.category.id } if self.period == "day": return WPCategory.render_template("display/overview/day.html", self.category, events=events, **params) elif self.period == "week": days = self._get_week_days() template = "display/overview/week.html" params["previous_week_url"] = self._other_day_url(self.start_dt - relativedelta(days=7)) params["next_week_url"] = self._other_day_url(self.start_dt + relativedelta(days=7)) elif self.period == "month": days = self._get_calendar_days() template = "display/overview/month.html" events_by_day = [] for day in days: events_by_day.append((day, self._pop_head_while(lambda x: x.start_dt.date() <= day.date(), events))) # Check whether all weekends are empty hide_weekend = not any(map(itemgetter(1), events_by_day[5::7])) and not any( map(itemgetter(1), events_by_day[6::7]) ) if hide_weekend: events_by_day = [x for x in events_by_day if x[0].weekday() not in (5, 6)] return WPCategory.render_template( template, self.category, events_by_day=events_by_day, hide_weekend=hide_weekend, **params )
def _process(self): # Current events, which are always shown by default are events of this month and of the previous month. # If there are no events in this range, it will include the last and next month containing events. past_threshold = self.now - relativedelta(months=1, day=1, hour=0, minute=0) future_threshold = self.now + relativedelta(months=1, day=1, hour=0, minute=0) next_event_start_dt = ( db.session.query(Event.start_dt) .filter(Event.start_dt >= self.now, Event.category_id == self.category.id) .order_by(Event.start_dt.asc()) .first() or (None,) )[0] previous_event_start_dt = ( db.session.query(Event.start_dt) .filter(Event.start_dt < self.now, Event.category_id == self.category.id) .order_by(Event.start_dt.desc()) .first() or (None,) )[0] if next_event_start_dt is not None and next_event_start_dt > future_threshold: future_threshold = next_event_start_dt + relativedelta(months=1, day=1, hour=0, minute=0) if previous_event_start_dt is not None and previous_event_start_dt < past_threshold: past_threshold = previous_event_start_dt.replace(day=1, hour=0, minute=0) event_query = ( Event.query.with_parent(self.category).options(*self._event_query_options).order_by(Event.start_dt.desc()) ) past_event_query = event_query.filter(Event.start_dt < past_threshold) future_event_query = event_query.filter(Event.start_dt >= future_threshold) current_event_query = event_query.filter(Event.start_dt >= past_threshold, Event.start_dt < future_threshold) events = current_event_query.filter(Event.start_dt < future_threshold).all() events_by_month = self.group_by_month(events) future_event_count = future_event_query.count() past_event_count = past_event_query.count() show_past_events = bool( self.category.id in session.get("fetch_past_events_in", set()) or (session.user and session.user.settings.get("show_past_events", False)) ) managers = sorted(self.category.get_manager_list(), key=attrgetter("principal_type.name", "name")) threshold_format = "%Y-%m" params = { "event_count": len(events), "events_by_month": events_by_month, "format_event_date": self.format_event_date, "future_event_count": future_event_count, "future_threshold": future_threshold.strftime(threshold_format), "happening_now": self.happening_now, "is_recent": self.is_recent, "managers": managers, "past_event_count": past_event_count, "show_past_events": show_past_events, "past_threshold": past_threshold.strftime(threshold_format), "atom_feed_url": url_for(".export_atom", self.category), "atom_feed_title": _('Events of "{}"').format(self.category.title), } params.update( get_base_ical_parameters( session.user, "category", "/export/categ/{0}.ics".format(self.category.id), {"from": "-31d"} ) ) if not self.category.is_root: return WPCategory.render_template("display/category.html", self.category, **params) news = get_recent_news() upcoming_events = get_upcoming_events() return WPCategory.render_template( "display/root_category.html", self.category, news=news, upcoming_events=upcoming_events, **params )