def get_queryset(self): """ Get queryset based on date and state. """ events = None calendar = self.get_calendar() if calendar: if not self.request.user.is_superuser and calendar not in self.request.user.calendars: return HttpResponseNotFound('You do not have permission to access this calendar.') if self.is_date_selected(): events = calendar.range_event_instances(self.get_start_date(), self.get_end_date()) else: events = calendar.future_event_instances() else: if self.is_date_selected(): events = get_events_by_range(self.get_start_date(), self.get_end_date(), user=self.request.user) else: events = get_all_users_future_events(self.request.user) # get the state filter if self.kwargs.get('state') == 'subscribed' and events: events = events.filter(event__state=State.get_id('posted'), event__created_from__isnull=False) else: state_id = State.get_id(self.kwargs.get('state')) if state_id is None: state_id = State.get_id('posted') if events: events = events.filter(event__state=state_id) self.queryset = events return events
def get_context_data(self, **kwargs): """ Override form_action_next added from SuccessPreviousViewRedirectMixin if the previous view is the Event Update view (redirecting to this view on a successful delete will return a 404.) """ context = super(EventDelete, self).get_context_data(**kwargs) form_action_next = context.get('form_action_next', None) if form_action_next: next = urllib.unquote_plus(form_action_next) next_relative = self.get_relative_path_with_query(next) try: view, v_args, v_kwargs = resolve(next_relative.path) except Http404: pass else: if view.__name__ == 'EventUpdate': ctx = { 'form_action_next': urllib.quote_plus(reverse( 'dashboard-calendar-state', kwargs = { 'pk': self.object.calendar.pk, 'state': State.get_string(self.object.state) } )) } context.update(ctx) return context
def get_context_data(self, **kwargs): """ Set context for managers Dashboard. """ context = super(Dashboard, self).get_context_data(**kwargs) ctx = { 'rereview_count': None, 'pending_count': None, # Needed to determine whether to show the cancel/un-cancel button 'posted_state': State.posted, 'state': 'posted', 'start_date': self.get_start_date(), 'show_rereview': get_main_calendar() in self.request.user.editable_calendars.all() or self.request.user.is_superuser } # merge context data ctx.update(context) calendar = self.get_calendar() if calendar: ctx['rereview_count'] = calendar.future_event_instances().filter(event__state=State.rereview).count() ctx['pending_count'] = calendar.future_event_instances().filter(event__state=State.pending).count() else: ctx['rereview_count'] = get_all_users_future_events(self.request.user).filter(event__state=State.rereview).count() ctx['pending_count'] = get_all_users_future_events(self.request.user).filter(event__state=State.pending).count() # get the state filter state_id = State.get_id(self.kwargs.get('state')) if state_id is None and self.kwargs.get('state') == 'subscribed': ctx['state'] = 'subscribed' elif state_id is not None: ctx['state'] = self.kwargs.get('state') return ctx
def set_state_element(player, addition, num): try: event = Event.objects.get(id=num) State(player=player, addition=addition, element=event).save() except: pass
def update_event_state(request, pk=None, state=None): """ Update the state of the event. """ event = get_object_or_404(Event, pk=pk) if not request.user.is_superuser and event.calendar not in request.user.calendars: messages.error(request, 'You cannot modify the state for Event %s.' % event.title) else: event.state = state try: event.save() except Exception, e: log(str(e)) messages.error(request, 'Unable to set Event %(1)s to %(2)s.' % {"1": event.title, "2": State.get_string(state)}) else:
if not request.user.is_superuser and event.calendar not in request.user.calendars: messages.error(request, 'You cannot modify the state for Event %s.' % event.title) else: event.state = state try: event.save() except Exception, e: log(str(e)) messages.error(request, 'Unable to set Event %(1)s to %(2)s.' % {"1": event.title, "2": State.get_string(state)}) else: if event.is_submit_to_main and event.state != State.posted and not event.calendar.is_main_calendar: messages.info(request, 'Event %s was removed from the Main Calendar since the event is not posted on your calendar.' % event.title) update_subscriptions(event) messages.success(request, 'Successfully updated Event %(1)s to %(2)s.' % {"1": event.title, "2": State.get_string(state)}) return event def submit_event_to_main(request, pk=None): """ Submit the event to the main calendar. """ event = get_object_or_404(Event, pk=pk) if not request.user.is_superuser and event.calendar not in request.user.calendars: messages.error(request, 'You cannot submit Event %s to the Main Calendar.' % event.title) else: if event.state == State.posted: if not event.is_submit_to_main:
def index_queryset(self, using=None): """ Used when the entire index for model is updated. Only retrieve published events that are not archived (no pending events; allow canceled.) """ now = datetime.now() unarchived_event_pks = EventInstance.objects.filter(end__gte=now, event__state__in=State.get_published_states()).values('event__pk') published_events = self.get_model().objects.filter(pk__in=unarchived_event_pks) return published_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