def test_invalid_month_orig(self):
     """Test that an invalid month in the url returns current
     month and error."""
     year, month, error = clean_year_month(2014, 3, 13)
     self.assertEqual(year, 2014)
     self.assertEqual(month, timezone.localtime(timezone.now()).month)
     self.assertEqual(error, ERROR)
 def test_invalid_out_of_bounds_year(self):
     """Test that a next or prev qs that puts the year out of bounds
     returns an error."""
     year, month, error = clean_year_month(2014, 100000, 1)
     self.assertEqual(year, now.year)
     self.assertEqual(month, timezone.localtime(timezone.now()).month)
     self.assertEqual(error, ERROR)
Beispiel #3
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,
    )
Beispiel #5
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 test_fix_year_month_prev(self):
     """
     Test that it adds to month and subtracts from year to fix date
     when a 'prev' querystring makes month < 12.
     """
     # -6 - 10 = prev querystring of -16
     year, month, error = clean_year_month(2014, -6, 10)
     self.assertEqual(year, 2013)
     self.assertEqual(month, 6)
     self.assertEqual(error, False)
 def test_fix_year_month_next_gt_12(self):
     """
     Test that it subtracts from month and adds to year to fix date
     when a 'next' querystring makes month > 12 (w/ next qs > 12).
     """
     # 23 - 10 = next query of 13
     year, month, error = clean_year_month(2014, 23, 10)
     self.assertEqual(year, 2015)
     self.assertEqual(month, 11)
     self.assertEqual(error, False)
Beispiel #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
    )
 def test_valid(self):
     year, month, error = clean_year_month(2014, 3, 3)
     self.assertEqual(year, 2014)
     self.assertEqual(month, 3)
     self.assertEqual(error, False)