예제 #1
0
 def dispatch(self, request, *args, **kwargs):
     """
     Prevent dynamically generated pages from returning
     content from too far into the future or the past.
     This primarily exists to prevent google from crawling
     to infinity and beyond.
     """
     start_date = self.get_start_date().date()
     if is_date_in_valid_range(start_date):
         return super(CalendarEventsBaseListView, self).dispatch(request, *args, **kwargs)
     else:
         raise Http404
예제 #2
0
    def clean(self):
        cleaned_data = super(EventInstanceForm, self).clean()

        start = cleaned_data.get('start')
        end = cleaned_data.get('end')
        until = cleaned_data.get('until')
        location = cleaned_data.get('location')
        new_location_title = cleaned_data.get('new_location_title')
        new_location_url = cleaned_data.get('new_location_url')

        if start and end:
            if start > end:
                self._errors['end'] = self.error_class(['The end day/time must occur after the start day/time'])
            if not is_date_in_valid_range(start.date()):
                self._errors['start'] = self.error_class(['Please provide a start date that falls between %s and %s' % (get_earliest_valid_date(date_format='%m/%d/%Y'), get_latest_valid_date(date_format='%m/%d/%Y'))])
            if not is_date_in_valid_range(end.date()):
                self._errors['end'] = self.error_class(['Please provide a end date that falls between %s and %s' % (get_earliest_valid_date(date_format='%m/%d/%Y'), get_latest_valid_date(date_format='%m/%d/%Y'))])
        else:
            if not start:
                self._errors['start'] = self.error_class(['Please enter a valid date/time, e.g. 11/16/2014 at 12:45 PM'])
            if not end:
                self._errors['end'] = self.error_class(['Please enter a valid date/time, e.g. 11/16/2014 at 12:45 PM'])

        if until:
            if not is_date_in_valid_range(until):
                self._errors['until'] = self.error_class(['Please provide an until date that falls between %s and %s' % (get_earliest_valid_date(date_format='%m/%d/%Y'), get_latest_valid_date(date_format='%m/%d/%Y'))])
            if end.date() >= until:
                self._errors['until'] = self.error_class(['The until date must fall after the end date/time'])

        if not location:
            if new_location_title:
                if not new_location_url:
                    self._errors['new_location_url'] = self.error_class(['URL needs to be provided for new locations'])
            else:
                self._errors['location'] = self.error_class(['No location was specified'])

        return cleaned_data
예제 #3
0
 def dispatch(self, request, *args, **kwargs):
     """
     Prevent dynamically generated pages from returning
     content from too far into the future or the past.
     This primarily exists to prevent google from crawling
     to infinity and beyond.
     """
     try:
         start_date = date(int(self.kwargs.get('year')), int(self.kwargs.get('month')), 1)
     except ValueError:
         # Invalid date
         raise Http404
     if is_date_in_valid_range(start_date):
         return super(CalendarWidgetView, self).dispatch(request, *args, **kwargs)
     else:
         raise Http404
예제 #4
0
파일: widgets.py 프로젝트: UCF/unify-events
def calendar_widget(context, calendars, year, month, pk=None, day=None, is_manager=0, size='small', use_pagers=True):

    # Catch requests for frontend widget with no specified calendar
    if calendars is "" and is_manager is 0:
        raise Http404

    if pk:
        calendars = get_object_or_404(Calendar, pk=pk)

    if day is None or day is "":
        relative_day = None
    else:
        if isinstance(day, datetime):
            relative_day = day.date()
        elif isinstance(day, date):
            relative_day = day
        else:
            raise TypeError('day must be a datetime.date or datetime.datetime, not a %s' % type(day))

    # Get this month, next and last month (1st day of month)
    if relative_day is None:
        this_month = date(int(year), int(month), 1)
    else:
        this_month = date(relative_day.year, relative_day.month, 1)
    next_month = date((this_month + relativedelta(months=+1)).year, (this_month + relativedelta(months=+1)).month, 1)
    last_month = date((this_month + relativedelta(months=-1)).year, (this_month + relativedelta(months=-1)).month, 1)

    # Make sure last and next month fall within a valid year/month range
    if not is_date_in_valid_range(next_month):
        next_month = None
    if not is_date_in_valid_range(last_month):
        last_month = None

    # Create new list of days in month (strip week grouping)
    this_month_cal = list(itertools.chain.from_iterable(calgenerator.Calendar(settings.FIRST_DAY_OF_WEEK).monthdatescalendar(this_month.year, this_month.month)))

    # Set dates as dict keys. Use OrderedDict to sort by date.
    this_month_cal = OrderedDict((v, []) for k, v in enumerate(this_month_cal))

    # Create map of month and day/event list.
    month_calendar_map = dict({this_month: this_month_cal})

    # Get a date range by which we will fetch events
    start = this_month_cal.keys()[0]
    end = datetime.combine(this_month_cal.keys()[-1], datetime.max.time())

    # Fetch posted events within our date range.
    calendar = None
    events = list()
    if (isinstance(calendars, Calendar)):
        events.extend(calendars.range_event_instances(start, end).filter(event__state__in=State.get_published_states()))
        calendar = calendars
    else:
        for cal in calendars:
            events.extend(cal.range_event_instances(start, end).filter(event__state__in=State.get_published_states()))

    # Assign event to all days the event falls on.
    events = map_event_range(start, end, events)
    for event in events:
        if event.start.date() in month_calendar_map[this_month].keys():
            month_calendar_map[this_month][event.start.date()].append(event)

    context = {
        'request': context['request'],
        'CANONICAL_ROOT': context['CANONICAL_ROOT'],
        'is_manager': is_manager,
        'calendar': calendar,
        'this_month': this_month,
        'next_month': next_month,
        'last_month': last_month,
        'today': date.today(),
        'relative': relative_day,
        'calendar_map': month_calendar_map,
        'use_pagers': use_pagers,
    }

    if size == 'small':
        template = loader.get_template('events/widgets/calendar-sidebar.html')
    else:
        template = loader.get_template('events/widgets/calendar-large.html')

    html = template.render(Context(context))

    return html