Example #1
0
    def get_year_and_month(self, net, qs, **kwargs):
        """
        Get the year and month. First tries from kwargs, then from
        querystrings. If none, or if cal_ignore qs is specified,
        sets year and month to this year and this month.
        """
        now = c.get_now()
        year = now.year
        month = now.month + net
        month_orig = None

        if 'cal_ignore=true' not in qs:
            if 'year' and 'month' in self.kwargs:  # try kwargs
                year, month_orig = map(
                    int, (self.kwargs['year'], self.kwargs['month'])
                )
                month = month_orig + net
            else:
                try:  # try querystring
                    year = int(self.request.GET['cal_year'])
                    month_orig = int(self.request.GET['cal_month'])
                    month = month_orig + net
                except Exception:
                    pass
        # return the year and month, and any errors that may have occurred do
        # to an invalid month/year being given.
        return c.clean_year_month(year, month, month_orig)
def show_calendar(context, req=None, mini=False, inherit_context=False):
    req = req or context.get('request', None)
    if not (req and hasattr(req, 'path') and hasattr(req, 'META')):
        raise TemplateSyntaxError(r"{% show_calendar %} should be called with HttpRequest instance as first argument or it should be available as `request` variable in template context")
    now = get_now()
    net, category, tag = get_net_category_tag(req)
    year = now.year
    month = now.month + net
    year, month, error = clean_year_month(year, month, None)

    prefetch = {'loc': True, 'cncl': True}
    if mini:
        prefetch['loc'] = False  # locations aren't displayed on mini calendar

    all_month_events = list(Event.objects.all_month_events(
        year, month, category, tag, **prefetch
    ))
    all_month_events.sort(key=lambda x: x.l_start_date.hour)
    qs = req.META['QUERY_STRING']
    if qs:  # get any querystrings that are not next/prev
        qs = get_qs(qs)
    if not inherit_context:
        context = {}
    return month_display(
        year, month, all_month_events, start_day, net, qs, mini=mini, request=req, context=context,
    )
Example #3
0
def show_calendar(context, req=None, mini=False, inherit_context=False):
    req = req or context.get('request', None)
    if not (req and hasattr(req, 'path') and hasattr(req, 'META')):
        raise TemplateSyntaxError(r"{% show_calendar %} should be called with HttpRequest instance as first argument "
                                  r"or it should be available as `request` variable in template context")
    now = get_now()
    net, category, tag = get_net_category_tag(req)
    year = now.year
    month = now.month + net
    year, month, error = clean_year_month(year, month, None)

    prefetch = {'loc': True, 'cncl': True}
    if mini:
        prefetch['loc'] = False  # locations aren't displayed on mini calendar

    all_month_events = list(Event.objects.all_month_events(
        year, month, category, tag, **prefetch
    ))
    all_month_events.sort(key=lambda x: x.l_start_date.hour)
    qs = req.META['QUERY_STRING']
    if qs:  # get any querystrings that are not next/prev
        qs = get_qs(qs)
    if not inherit_context:
        context = {}
    return month_display(
        year, month, all_month_events, start_day, net, qs, mini=mini, request=req, context=context,
    )
def upcoming_events(now=None, finish=90, num=5):
    if now is None:
        now = get_now()
    finish = now + timezone.timedelta(days=finish)
    finish = finish.replace(hour=23, minute=59, second=59, microsecond=999)
    all_upcoming = (UpcomingEvents(x, now, finish, num).get_upcoming_events()
                    for x in Event.objects.live(now))
    upcoming = heapq.nsmallest(
        num,
        (item for sublist in all_upcoming for item in sublist),
        key=lambda x: x[0]
    )
    return {'upcoming_events': upcoming}
Example #5
0
def upcoming_events(now=None, finish=90, num=5):
    if now is None:
        now = get_now()
    finish = now + timezone.timedelta(days=finish)
    finish = finish.replace(hour=23, minute=59, second=59, microsecond=999)
    all_upcoming = (UpcomingEvents(x, now, finish, num).get_upcoming_events()
                    for x in Event.objects.live(now))
    upcoming = heapq.nsmallest(
        num,
        (item for sublist in all_upcoming for item in sublist),
        key=lambda x: x[0]
    )
    return {'upcoming_events': upcoming}
def current_happenings(now=None):
    if now is None:
        now = get_now()
    temp = now
    drepl = lambda x: temp.replace(  # replace hr, min, sec, msec of 'now'
        hour=x.l_start_date.hour,
        minute=x.l_start_date.minute,
        second=x.l_start_date.second,
        microsecond=x.l_start_date.microsecond
    )
    the_haps = (
        (drepl(x), x) for x in Event.objects.live(now) if x.is_happening(now)
    )
    return {'events_happening_now': the_haps}
Example #7
0
def current_happenings(now=None):
    if now is None:
        now = get_now()
    temp = now
    drepl = lambda x: temp.replace(  # replace hr, min, sec, msec of 'now'
        hour=x.l_start_date.hour,
        minute=x.l_start_date.minute,
        second=x.l_start_date.second,
        microsecond=x.l_start_date.microsecond
    )
    the_haps = (
        (drepl(x), x) for x in Event.objects.live(now) if x.is_happening(now)
    )
    return {'events_happening_now': the_haps}
Example #8
0
def show_calendar(req, mini=False):
    now = get_now()
    net, category, tag = get_net_category_tag(req)
    year = now.year
    month = now.month + net
    year, month, error = clean_year_month(year, month, None)

    prefetch = {'loc': True, 'cncl': True}
    if mini:
        prefetch['loc'] = False  # locations aren't displayed on mini calendar

    all_month_events = list(Event.objects.all_month_events(
        year, month, category, tag, **prefetch
    ))
    all_month_events.sort(key=lambda x: x.l_start_date.hour)
    qs = req.META['QUERY_STRING']
    if qs:  # get any querystrings that are not next/prev
        qs = get_qs(qs)
    return month_display(
        year, month, all_month_events, start_day, net, qs, mini=mini
    )
Example #9
0
    def get_context_data(self, **kwargs):
        now = c.get_now()
        context = super(EventDetailView, self).get_context_data(**kwargs)
        e = self.object

        for choice in Event.REPEAT_CHOICES:
            if choice[0] == e.repeat:
                context['repeat'] = choice[1]

        context['cncl_days'] = self.get_cncl_days()

        event = [e]  # event needs to be an iterable, see get_next_event()
        if not e.repeats('NEVER'):  # event is ongoing; get next occurrence
            if e.will_occur(now):
                year, month, day = get_next_event(event, now)
                next_event = date(year, month, day)
                context['next_event'] = date(year, month, day)
                context['next_or_prev_cncl'] = self.check_cncl(next_event)
            else:  # event is finished repeating; get last occurrence
                end = e.end_repeat
                last_event = end
                if e.repeats('WEEKDAY'):
                    year, month, day = c.check_weekday(
                        end.year, end.month, end.day, reverse=True
                    )
                    last_event = date(year, month, day)
                context['last_event'] = last_event
                context['next_or_prev_cncl'] = self.check_cncl(last_event)
        else:
            if e.is_chunk():
                # list of days for single-day event chunk
                context['event_days'] = (  # list comp
                    (e.l_start_date + timedelta(days=x))
                    for x in range(e.start_end_diff + 1)
                )
            else:
                # let template know if this single-day, non-repeating event is
                # cancelled
                context['this_cncl'] = self.check_cncl(e.l_start_date.date())
        return context
Example #10
0
 def get_cncl_days(self):
     now = c.get_now()
     cncl = self.object.cancellations.all()
     return [(x.date, x.reason) for x in cncl if x.date >= now.date()]