class RHExportDashboardICS(RHTokenProtected): @use_kwargs({ 'from_': HumanizedDate(data_key='from', missing=lambda: now_utc(False) - relativedelta(weeks=1)), 'include': fields.List(fields.Str(), missing={'linked', 'categories'}), 'limit': fields.Integer(missing=100, validate=lambda v: 0 < v <= 500) }) def _process(self, from_, include, limit): categories = get_related_categories(self.user) categories_events = [] if categories: category_ids = {c['categ'].id for c in categories.itervalues()} categories_events = get_events_in_categories(category_ids, self.user, limit=limit) linked_events = get_linked_events( self.user, from_, limit=limit, load_also=('description', 'own_room_id', 'own_venue_id', 'own_room_name', 'own_venue_name') ) all_events = set() if 'linked' in include: all_events |= set(linked_events) if 'categories' in include: all_events |= set(categories_events) all_events = sorted(all_events, key=lambda e: (e.start_dt, e.id))[:limit] response = {'results': [serialize_event_for_ical(event, 'events') for event in all_events]} serializer = Serializer.create('ics') return send_file('event.ics', BytesIO(serializer(response)), 'text/calendar')
class RHExportDashboardICS(RHProtected): def _get_user(self): return session.user @use_kwargs({ 'from_': HumanizedDate(data_key='from', load_default=lambda: now_utc(False) - relativedelta(weeks=1)), 'include': fields.List(fields.Str(), load_default={'linked', 'categories'}), 'limit': fields.Integer(load_default=100, validate=lambda v: 0 < v <= 500) }, location='query') def _process(self, from_, include, limit): user = self._get_user() all_events = set() if 'linked' in include: all_events |= set(get_linked_events( user, from_, limit=limit, load_also=('description', 'own_room_id', 'own_venue_id', 'own_room_name', 'own_venue_name') )) if 'categories' in include and (categories := get_related_categories(user)): category_ids = {c['categ'].id for c in categories.values()} all_events |= set(get_events_in_categories(category_ids, user, limit=limit)) all_events = sorted(all_events, key=lambda e: (e.start_dt, e.id))[:limit] response = {'results': [serialize_event_for_ical(event) for event in all_events]} serializer = Serializer.create('ics') return send_file('event.ics', BytesIO(serializer(response)), 'text/calendar')