Esempio n. 1
0
 def _events_by_day(self, before, after):
     ebd = defaultdict(list)
     timezones = set()
     add_events_for_days(
         self.request,
         Event.annotated(self.request.organizer.events,
                         'web').using(settings.DATABASE_REPLICA).filter(
                             sales_channels__contains=self.request.
                             sales_channel.identifier), before, after, ebd,
         timezones)
     add_subevents_for_days(
         filter_qs_by_attr(
             SubEvent.annotated(
                 SubEvent.objects.filter(
                     event__organizer=self.request.organizer,
                     event__is_public=True,
                     event__live=True,
                     event__sales_channels__contains=self.request.
                     sales_channel.identifier).prefetch_related(
                         'event___settings_objects',
                         'event__organizer___settings_objects')),
             self.request).using(settings.DATABASE_REPLICA), before, after,
         ebd, timezones)
     self._multiple_timezones = len(timezones) > 1
     return ebd
Esempio n. 2
0
 def _events_by_day(self, before, after):
     ebd = defaultdict(list)
     timezones = set()
     add_events_for_days(self.request, Event.annotated(self.request.organizer.events, 'web').using(settings.DATABASE_REPLICA), before, after, ebd, timezones)
     add_subevents_for_days(filter_qs_by_attr(SubEvent.annotated(SubEvent.objects.filter(
         event__organizer=self.request.organizer,
         event__is_public=True,
         event__live=True,
     ).prefetch_related(
         'event___settings_objects', 'event__organizer___settings_objects'
     )), self.request).using(settings.DATABASE_REPLICA), before, after, ebd, timezones)
     self._multiple_timezones = len(timezones) > 1
     return ebd
Esempio n. 3
0
 def _events_by_day(self, before, after):
     ebd = defaultdict(list)
     timezones = set()
     add_events_for_days(self.request, Event.annotated(self.request.organizer.events, 'web'), before, after, ebd, timezones)
     add_subevents_for_days(filter_qs_by_attr(SubEvent.annotated(SubEvent.objects.filter(
         event__organizer=self.request.organizer,
         event__is_public=True,
         event__live=True,
     ).prefetch_related(
         'event___settings_objects', 'event__organizer___settings_objects'
     )), self.request), before, after, ebd, timezones)
     self._multiple_timezones = len(timezones) > 1
     return ebd
Esempio n. 4
0
    def _get_event_list(self, request, **kwargs):
        data = {}
        o = getattr(request, 'event', request.organizer)
        list_type = self.request.GET.get("style", o.settings.event_list_type)
        data['list_type'] = list_type

        if hasattr(self.request,
                   'event') and data['list_type'] not in ("calendar", "week"):
            if self.request.event.subevents.filter(
                    date_from__gt=now()).count() > 50:
                if self.request.event.settings.event_list_type not in (
                        "calendar", "week"):
                    self.request.event.settings.event_list_type = "calendar"
                data['list_type'] = list_type = 'calendar'

        if hasattr(self.request, 'event'):
            data['name'] = str(request.event.name)
            data['frontpage_text'] = str(
                rich_text(request.event.settings.frontpage_text,
                          safelinks=False))

        cache_key = ':'.join([
            'widget.py',
            'eventlist',
            request.organizer.slug,
            request.event.slug if hasattr(request, 'event') else '-',
            list_type,
            request.GET.urlencode(),
            get_language(),
        ])
        cached_data = cache.get(cache_key)
        if cached_data:
            return self.response(cached_data)

        if list_type == "calendar":
            self._set_month_year()
            _, ndays = calendar.monthrange(self.year, self.month)

            data['date'] = date(self.year, self.month, 1)
            if hasattr(self.request, 'event'):
                tz = pytz.timezone(self.request.event.settings.timezone)
            else:
                tz = pytz.UTC
            before = datetime(self.year, self.month, 1, 0, 0, 0,
                              tzinfo=tz) - timedelta(days=1)
            after = datetime(self.year, self.month, ndays, 0, 0, 0,
                             tzinfo=tz) + timedelta(days=1)

            ebd = defaultdict(list)

            if hasattr(self.request, 'event'):
                add_subevents_for_days(
                    filter_qs_by_attr(
                        self.request.event.subevents_annotated('web').filter(
                            event__sales_channels__contains=self.request.
                            sales_channel.identifier), self.request), before,
                    after, ebd, set(), self.request.event,
                    kwargs.get('cart_namespace'))
            else:
                timezones = set()
                add_events_for_days(
                    self.request,
                    filter_qs_by_attr(
                        Event.annotated(
                            self.request.organizer.events,
                            'web').filter(sales_channels__contains=self.
                                          request.sales_channel.identifier),
                        self.request), before, after, ebd, timezones)
                add_subevents_for_days(
                    filter_qs_by_attr(
                        SubEvent.annotated(
                            SubEvent.objects.filter(
                                event__organizer=self.request.organizer,
                                event__is_public=True,
                                event__live=True,
                                event__sales_channels__contains=self.request.
                                sales_channel.identifier).prefetch_related(
                                    'event___settings_objects',
                                    'event__organizer___settings_objects')),
                        self.request), before, after, ebd, timezones)

            data['weeks'] = weeks_for_template(ebd, self.year, self.month)
            for w in data['weeks']:
                for d in w:
                    if not d:
                        continue
                    d['events'] = self._serialize_events(d['events'] or [])
        elif list_type == "week":
            self._set_week_year()

            if hasattr(self.request, 'event'):
                tz = pytz.timezone(self.request.event.settings.timezone)
            else:
                tz = pytz.UTC

            week = isoweek.Week(self.year, self.week)
            data['week'] = [self.year, self.week]
            before = datetime(week.monday().year,
                              week.monday().month,
                              week.monday().day,
                              0,
                              0,
                              0,
                              tzinfo=tz) - timedelta(days=1)
            after = datetime(week.sunday().year,
                             week.sunday().month,
                             week.sunday().day,
                             0,
                             0,
                             0,
                             tzinfo=tz) + timedelta(days=1)

            ebd = defaultdict(list)
            if hasattr(self.request, 'event'):
                add_subevents_for_days(
                    filter_qs_by_attr(
                        self.request.event.subevents_annotated('web'),
                        self.request), before, after, ebd, set(),
                    self.request.event, kwargs.get('cart_namespace'))
            else:
                timezones = set()
                add_events_for_days(
                    self.request,
                    filter_qs_by_attr(
                        Event.annotated(self.request.organizer.events, 'web'),
                        self.request), before, after, ebd, timezones)
                add_subevents_for_days(
                    filter_qs_by_attr(
                        SubEvent.annotated(
                            SubEvent.objects.filter(
                                event__organizer=self.request.organizer,
                                event__is_public=True,
                                event__live=True,
                            ).prefetch_related(
                                'event___settings_objects',
                                'event__organizer___settings_objects')),
                        self.request), before, after, ebd, timezones)

            data['days'] = days_for_template(ebd, week)
            for d in data['days']:
                d['events'] = self._serialize_events(d['events'] or [])
        else:
            if hasattr(self.request, 'event'):
                evs = self.request.event.subevents_sorted(
                    filter_qs_by_attr(
                        self.request.event.subevents_annotated(
                            self.request.sales_channel.identifier),
                        self.request))
                tz = pytz.timezone(request.event.settings.timezone)
                data['events'] = [{
                    'name':
                    str(ev.name),
                    'location':
                    str(ev.location),
                    'date_range':
                    self._get_date_range(ev, ev.event, tz),
                    'availability':
                    self._get_availability(ev, ev.event),
                    'event_url':
                    build_absolute_uri(ev.event, 'presale:event.index'),
                    'subevent':
                    ev.pk,
                } for ev in evs]
            else:
                data['events'] = []
                qs = self._get_event_queryset()
                for event in qs:
                    tz = pytz.timezone(
                        event.cache.get_or_set(
                            'timezone', lambda: event.settings.timezone))
                    if event.has_subevents:
                        dr = daterange(event.min_from.astimezone(tz),
                                       (event.max_fromto or event.max_to
                                        or event.max_from).astimezone(tz))
                        avail = {
                            'color': 'none',
                            'text': gettext('Event series')
                        }
                    else:
                        dr = self._get_date_range(event, event, tz)
                        avail = self._get_availability(event, event)
                    data['events'].append({
                        'name':
                        str(event.name),
                        'location':
                        str(event.location),
                        'date_range':
                        dr,
                        'availability':
                        avail,
                        'event_url':
                        build_absolute_uri(event, 'presale:event.index'),
                    })

        cache.set(cache_key, data, 30)
        # These pages are cached for a really short duration – this should make them pretty accurate, while still
        # providing some protection against burst traffic.
        return self.response(data)
Esempio n. 5
0
    def _get_event_list(self, request, **kwargs):
        data = {}
        o = getattr(request, 'event', request.organizer)
        list_type = self.request.GET.get("style", o.settings.event_list_type)
        data['list_type'] = list_type

        cache_key = ':'.join([
            'widget.py',
            'eventlist',
            request.organizer.slug,
            request.event.slug if hasattr(request, 'event') else '-',
            list_type,
            request.GET.get("year") or "-",
            request.GET.get("month") or "-",
            request.GET.get("old") or "-",
            get_language(),
        ])
        cached_data = cache.get(cache_key)
        if cached_data:
            return self.response(cached_data)

        if list_type == "calendar":
            self._set_month_year()
            _, ndays = calendar.monthrange(self.year, self.month)

            data['date'] = date(self.year, self.month, 1)
            if hasattr(self.request, 'event'):
                tz = pytz.timezone(self.request.event.settings.timezone)
            else:
                tz = pytz.UTC
            before = datetime(self.year, self.month, 1, 0, 0, 0, tzinfo=tz) - timedelta(days=1)
            after = datetime(self.year, self.month, ndays, 0, 0, 0, tzinfo=tz) + timedelta(days=1)

            ebd = defaultdict(list)

            if hasattr(self.request, 'event'):
                add_subevents_for_days(
                    self.request.event.subevents_annotated('web'),
                    before, after, ebd, set(), self.request.event,
                    kwargs.get('cart_namespace')
                )
            else:
                timezones = set()
                add_events_for_days(self.request, Event.annotated(self.request.organizer.events, 'web'), before, after, ebd, timezones)
                add_subevents_for_days(filter_qs_by_attr(SubEvent.annotated(SubEvent.objects.filter(
                    event__organizer=self.request.organizer,
                    event__is_public=True,
                    event__live=True,
                ).prefetch_related(
                    'event___settings_objects', 'event__organizer___settings_objects'
                )), self.request), before, after, ebd, timezones)

            data['weeks'] = weeks_for_template(ebd, self.year, self.month)
            for w in data['weeks']:
                for d in w:
                    if not d:
                        continue
                    d['events'] = self._serialize_events(d['events'] or [])
        else:
            if hasattr(self.request, 'event'):
                evs = self.request.event.subevents_sorted(
                    self.request.event.subevents_annotated(self.request.sales_channel)
                )
                tz = pytz.timezone(request.event.settings.timezone)
                data['events'] = [
                    {
                        'name': str(ev.name),
                        'date_range': ev.get_date_range_display(tz) + (
                            (" " + ev.get_time_from_display(tz)) if ev.event.settings.show_times else ""
                        ),
                        'availability': self._get_availability(ev, ev.event),
                        'event_url': build_absolute_uri(ev.event, 'presale:event.index'),
                        'subevent': ev.pk,
                    } for ev in evs
                ]
            else:
                data['events'] = []
                qs = self._get_event_queryset()
                for event in qs:
                    tz = pytz.timezone(event.cache.get_or_set('timezone', lambda: event.settings.timezone))
                    if event.has_subevents:
                        dr = daterange(
                            event.min_from.astimezone(tz),
                            (event.max_fromto or event.max_to or event.max_from).astimezone(tz)
                        )
                        avail = {'color': 'none', 'text': ugettext('Event series')}
                    else:
                        dr = event.get_date_range_display(tz) + (
                            " " + event.get_time_from_display(tz) if event.settings.show_times else ""
                        )
                        avail = self._get_availability(event, event)
                    data['events'].append({
                        'name': str(event.name),
                        'date_range': dr,
                        'availability': avail,
                        'event_url': build_absolute_uri(event, 'presale:event.index'),
                    })

        cache.set(cache_key, data, 30)
        # These pages are cached for a really short duration – this should make them pretty accurate, while still
        # providing some protection against burst traffic.
        return self.response(data)
Esempio n. 6
0
    def _get_event_list(self, request, **kwargs):
        data = {}
        o = getattr(request, 'event', request.organizer)
        list_type = self.request.GET.get("style", o.settings.event_list_type)
        data['list_type'] = list_type

        if hasattr(self.request,
                   'event') and data['list_type'] not in ("calendar", "week"):
            # only allow list-view of more than 50 subevents if ordering is by data as this can be done in the database
            # ordering by name is currently not supported in database due to I18NField-JSON
            ordering = self.request.event.settings.get(
                'frontpage_subevent_ordering',
                default='date_ascending',
                as_type=str)
            if ordering not in ("date_ascending", "date_descending"
                                ) and self.request.event.subevents.filter(
                                    date_from__gt=now()).count() > 50:
                if self.request.event.settings.event_list_type not in (
                        "calendar", "week"):
                    self.request.event.settings.event_list_type = "calendar"
                data['list_type'] = list_type = 'calendar'

        if hasattr(self.request, 'event'):
            data['name'] = str(request.event.name)
            data['frontpage_text'] = str(
                rich_text(request.event.settings.frontpage_text,
                          safelinks=False))

        cache_key = ':'.join([
            'widget.py',
            'eventlist',
            request.organizer.slug,
            request.event.slug if hasattr(request, 'event') else '-',
            list_type,
            request.GET.urlencode(),
            get_language(),
        ])
        cached_data = cache.get(cache_key)
        if cached_data:
            return self.response(cached_data)

        if list_type == "calendar":
            self._set_month_year()
            _, ndays = calendar.monthrange(self.year, self.month)

            data['date'] = date(self.year, self.month, 1)
            if hasattr(self.request, 'event'):
                tz = pytz.timezone(self.request.event.settings.timezone)
            else:
                tz = pytz.UTC
            before = datetime(self.year, self.month, 1, 0, 0, 0,
                              tzinfo=tz) - timedelta(days=1)
            after = datetime(self.year, self.month, ndays, 0, 0, 0,
                             tzinfo=tz) + timedelta(days=1)

            ebd = defaultdict(list)

            if hasattr(self.request, 'event'):
                add_subevents_for_days(
                    filter_qs_by_attr(
                        self.request.event.subevents_annotated('web').filter(
                            event__sales_channels__contains=self.request.
                            sales_channel.identifier), self.request), before,
                    after, ebd, set(), self.request.event,
                    kwargs.get('cart_namespace'))
            else:
                timezones = set()
                add_events_for_days(
                    self.request,
                    filter_qs_by_attr(
                        Event.annotated(
                            self.request.organizer.events,
                            'web').filter(sales_channels__contains=self.
                                          request.sales_channel.identifier),
                        self.request), before, after, ebd, timezones)
                add_subevents_for_days(
                    filter_qs_by_attr(
                        SubEvent.annotated(
                            SubEvent.objects.filter(
                                event__organizer=self.request.organizer,
                                event__is_public=True,
                                event__live=True,
                                event__sales_channels__contains=self.request.
                                sales_channel.identifier).prefetch_related(
                                    'event___settings_objects',
                                    'event__organizer___settings_objects')),
                        self.request), before, after, ebd, timezones)

            data['weeks'] = weeks_for_template(ebd, self.year, self.month)
            for w in data['weeks']:
                for d in w:
                    if not d:
                        continue
                    d['events'] = self._serialize_events(d['events'] or [])
        elif list_type == "week":
            self._set_week_year()

            if hasattr(self.request, 'event'):
                tz = pytz.timezone(self.request.event.settings.timezone)
            else:
                tz = pytz.UTC

            week = isoweek.Week(self.year, self.week)
            data['week'] = [self.year, self.week]
            before = datetime(week.monday().year,
                              week.monday().month,
                              week.monday().day,
                              0,
                              0,
                              0,
                              tzinfo=tz) - timedelta(days=1)
            after = datetime(week.sunday().year,
                             week.sunday().month,
                             week.sunday().day,
                             0,
                             0,
                             0,
                             tzinfo=tz) + timedelta(days=1)

            ebd = defaultdict(list)
            if hasattr(self.request, 'event'):
                add_subevents_for_days(
                    filter_qs_by_attr(
                        self.request.event.subevents_annotated('web'),
                        self.request), before, after, ebd, set(),
                    self.request.event, kwargs.get('cart_namespace'))
            else:
                timezones = set()
                add_events_for_days(
                    self.request,
                    filter_qs_by_attr(
                        Event.annotated(self.request.organizer.events, 'web'),
                        self.request), before, after, ebd, timezones)
                add_subevents_for_days(
                    filter_qs_by_attr(
                        SubEvent.annotated(
                            SubEvent.objects.filter(
                                event__organizer=self.request.organizer,
                                event__is_public=True,
                                event__live=True,
                            ).prefetch_related(
                                'event___settings_objects',
                                'event__organizer___settings_objects')),
                        self.request), before, after, ebd, timezones)

            data['days'] = days_for_template(ebd, week)
            for d in data['days']:
                d['events'] = self._serialize_events(d['events'] or [])
        else:
            offset = int(self.request.GET.get("offset", 0))
            limit = 50
            if hasattr(self.request, 'event'):
                evs = self.request.event.subevents_sorted(
                    filter_qs_by_attr(
                        self.request.event.subevents_annotated(
                            self.request.sales_channel.identifier),
                        self.request))
                ordering = self.request.event.settings.get(
                    'frontpage_subevent_ordering',
                    default='date_ascending',
                    as_type=str)
                data['has_more_events'] = False
                if ordering in ("date_ascending", "date_descending"):
                    # fetch one more result than needed to check if more events exist
                    evs = list(evs[offset:offset + limit + 1])
                    if len(evs) > limit:
                        data['has_more_events'] = True
                        evs = evs[:limit]

                tz = pytz.timezone(request.event.settings.timezone)
                if self.request.event.settings.event_list_available_only:
                    evs = [
                        se for se in evs if not se.presale_has_ended and (
                            se.best_availability_state is not None
                            and se.best_availability_state >=
                            Quota.AVAILABILITY_RESERVED)
                    ]

                data['events'] = [{
                    'name':
                    str(ev.name),
                    'location':
                    str(ev.location),
                    'date_range':
                    self._get_date_range(ev, ev.event, tz),
                    'availability':
                    self._get_availability(ev, ev.event, tz=tz),
                    'event_url':
                    build_absolute_uri(ev.event, 'presale:event.index'),
                    'subevent':
                    ev.pk,
                } for ev in evs]
            else:
                data['events'] = []
                qs = self._get_event_queryset()
                for event in qs:
                    tz = pytz.timezone(
                        event.cache.get_or_set(
                            'timezone', lambda: event.settings.timezone))
                    if event.has_subevents:
                        dr = daterange(event.min_from.astimezone(tz),
                                       (event.max_fromto or event.max_to
                                        or event.max_from).astimezone(tz))
                        avail = {
                            'color': 'none',
                            'text': gettext('Event series')
                        }
                    else:
                        dr = self._get_date_range(event, event, tz)
                        avail = self._get_availability(event, event, tz=tz)
                    data['events'].append({
                        'name':
                        str(event.name),
                        'location':
                        str(event.location),
                        'date_range':
                        dr,
                        'availability':
                        avail,
                        'event_url':
                        build_absolute_uri(event, 'presale:event.index'),
                    })

        cache.set(cache_key, data, 30)
        # These pages are cached for a really short duration – this should make them pretty accurate, while still
        # providing some protection against burst traffic.
        return self.response(data)