def manage_subscription(request, slug, subscribe=True): """Manage user's event subscription. When /subscribe/ == True then subscribe user to event if not already subscribed, else unsubscribe user. """ event = get_object_or_404(Event, slug=slug) if request.method == "POST": attendance, created = get_or_create_instance(Attendance, user=get_user(request), event=event) if subscribe: if not created: messages.warning(request, ("You are already subscribed to " "this event.")) else: attendance.save() subscribed_text = render_to_string("includes/subscribe_to_ical.html", {"event": event}) messages.info(request, Markup(subscribed_text)) else: if created: messages.warning(request, ("You are not subscribed " "to this event.")) elif request.user == event.owner: messages.error(request, ("Event owner cannot unsubscribe " "from event.")) else: attendance.delete() messages.success(request, ("You have unsubscribed " "from this event.")) return redirect("events_view_event", slug=event.slug)
def edit_report(request, display_name, year, month): """Edit report view.""" user = get_object_or_404(User, userprofile__display_name=display_name) year_month = datetime.datetime(year=int(year), month=utils.month2number(month), day=1) today = datetime.datetime.today() # check if it's too early to file this report if (year_month.month > today.month and year_month.year >= today.year): messages.error(request, 'I\'m sorry, are you from the future?') if request.user == user: return redirect('profiles_view_my_profile') else: redirect_url = reverse('profiles_view_profile', kwargs={'display_name': display_name}) return redirect(redirect_url) report, created = utils.get_or_create_instance(Report, user=user, month=year_month) if request.method == 'POST': # Make sure that users without permission do not modify # overdue field. data = request.POST.copy() if not request.user.has_perm('reports.can_edit_report'): data['overdue'] = report.overdue report_form = forms.ReportForm(data, instance=report) report_event_formset = forms.ReportEventFormset(data, instance=report) report_link_formset = forms.ReportLinkFormset(data, instance=report) if (report_form.is_valid() and report_event_formset.is_valid() and report_link_formset.is_valid()): report_form.save() report_event_formset.save() report_link_formset.save() if created: messages.success(request, 'Report successfully created.') else: messages.success(request, 'Report successfully updated.') return redirect(reverse('reports_view_report', kwargs={'display_name': display_name, 'year': year, 'month': month})) else: report_form = forms.ReportForm(instance=report) report_event_formset = forms.ReportEventFormset(instance=report) report_link_formset = forms.ReportLinkFormset(instance=report) return render(request, 'edit_report.html', {'report_form': report_form, 'report_event_formset': report_event_formset, 'report_link_formset': report_link_formset, 'pageuser': user, 'year': year, 'month': month, 'created': created})
def edit_voting(request, slug=None): """Create/Edit voting view.""" poll, created = get_or_create_instance(Poll, slug=slug) can_delete_voting = False extra = 0 current_voting_edit = False range_poll_formset = None radio_poll_formset = None if created: poll.created_by = request.user extra = 1 else: if (RangePoll.objects.filter(poll=poll).count() or RadioPoll.objects.filter(poll=poll).count()) == 0: extra = 1 can_delete_voting = True date_now = datetime2pdt() if poll.start < date_now and poll.end > date_now: current_voting_edit = True if current_voting_edit: poll_form = forms.PollEditForm(request.POST or None, instance=poll) else: RangePollFormset = (inlineformset_factory(Poll, RangePoll, formset=forms.BaseRangePollInlineFormSet, extra=extra, can_delete=True)) RadioPollFormset = (inlineformset_factory(Poll, RadioPoll, formset=forms.BaseRadioPollInlineFormSet, extra=extra, can_delete=True)) nominee_list = User.objects.filter( groups__name='Rep', userprofile__registration_complete=True) range_poll_formset = RangePollFormset(request.POST or None, instance=poll, user_list=nominee_list) radio_poll_formset = RadioPollFormset(request.POST or None, instance=poll) poll_form = forms.PollAddForm(request.POST or None, instance=poll, radio_poll_formset=radio_poll_formset, range_poll_formset=range_poll_formset) if poll_form.is_valid(): poll_form.save() if created: messages.success(request, 'Voting successfully created.') else: messages.success(request, 'Voting successfully edited.') return redirect('voting_edit_voting', slug=poll.slug) return render(request, 'edit_voting.html', {'creating': created, 'poll': poll, 'poll_form': poll_form, 'range_poll_formset': range_poll_formset, 'radio_poll_formset': radio_poll_formset, 'can_delete_voting': can_delete_voting, 'current_voting_edit': current_voting_edit})
def edit_event(request, slug=None, clone=None): """Edit event view.""" event, created = get_or_create_instance(Event, slug=slug) if created: event.owner = request.user else: # This is for backwards compatibility for all the events # that were set before the change in the minutes section # of the drop down widget to multiples of 5. # Start time: Floor rounding # End time: Ceilling rounding event.start -= timedelta(minutes=event.start.minute % 5) if (event.end.minute % 5) != 0: event.end += timedelta(minutes=(5 - (event.end.minute % 5))) initial = {'country': request.user.userprofile.country, 'city': request.user.userprofile.city, 'region': request.user.userprofile.region} if request.user.groups.filter(name='Admin').count(): event_form = forms.EventForm(request.POST or None, editable_owner=True, instance=event, initial=initial) else: event_form = forms.EventForm(request.POST or None, editable_owner=False, instance=event, initial=initial) metrics_formset = forms.EventMetricsFormset(request.POST or None, instance=event) if (event_form.is_valid() and metrics_formset.is_valid() and request.POST): event_form.save(clone=clone) metrics_formset.save(clone=clone) if created: messages.success(request, 'Event successfully created.') else: messages.success(request, 'Event successfully updated.') return redirect('events_view_event', slug=event_form.instance.slug) can_delete_event = False if (not created and (event.owner == request.user or request.user.has_perm('events.can_delete_events'))): can_delete_event = True selected_categories = map(int, event_form['categories'].value()) return render(request, 'edit_event.html', {'creating': created, 'event': event, 'event_form': event_form, 'selected_categories': selected_categories, 'metrics_formset': metrics_formset, 'can_delete_event': can_delete_event})
def edit_report(request, display_name, year, month): """Edit report view.""" user = get_object_or_404(User, userprofile__display_name=display_name) year_month = datetime.datetime(year=int(year), month=utils.month2number(month), day=1) report, created = utils.get_or_create_instance(Report, user=user, month=year_month) if request.method == 'POST': # Make sure that users without permission do not modify # overdue field. data = request.POST.copy() if not request.user.has_perm('reports.can_edit_report'): data['overdue'] = report.overdue report_form = forms.ReportForm(data, instance=report) report_event_formset = forms.ReportEventFormset(data, instance=report) report_link_formset = forms.ReportLinkFormset(data, instance=report) if (report_form.is_valid() and report_event_formset.is_valid() and report_link_formset.is_valid()): report_form.save() report_event_formset.save() report_link_formset.save() if created: messages.success(request, 'Report successfully created.') else: messages.success(request, 'Report successfully updated.') return redirect(reverse('reports_view_report', kwargs={'display_name': display_name, 'year': year, 'month': month})) else: report_form = forms.ReportForm(instance=report) report_event_formset = forms.ReportEventFormset(instance=report) report_link_formset = forms.ReportLinkFormset(instance=report) return render(request, 'edit_report.html', {'report_form': report_form, 'report_event_formset': report_event_formset, 'report_link_formset': report_link_formset, 'pageuser': user, 'year': year, 'month': month, 'created': created})
def manage_subscription(request, slug, subscribe=True): """Manage user's event subscription. When /subscribe/ == True then subscribe user to event if not already subscribed, else unsubscribe user. """ event = get_object_or_404(Event, slug=slug) if request.method == 'POST': attendance, created = get_or_create_instance(Attendance, user=get_user(request), event=event) if subscribe: if not created: messages.warning(request, ('You are already subscribed to ' 'this event.')) else: attendance.save() if not event.is_past_event: subscribed_text = render_to_string( 'includes/subscribe_to_ical.html', {'event': event}) messages.info(request, mark_safe(subscribed_text)) statsd.incr('events.subscribe_to_event') else: messages.info(request, 'You subscribed to this event.') else: if created: messages.warning(request, ('You are not subscribed ' 'to this event.')) elif request.user == event.owner: messages.error(request, ('Event owner cannot unsubscribe ' 'from event.')) else: attendance.delete() messages.success(request, ('You have unsubscribed ' 'from this event.')) statsd.incr('events.unsubscribe_from_event') return redirect('events_view_event', slug=event.slug)
def edit_event(request, slug=None): """Edit event view.""" event, created = get_or_create_instance(Event, slug=slug) if created: event.owner = request.user if request.user.groups.filter(name='Admin').count(): event_form = forms.EventForm(request.POST or None, editable_owner=True, instance=event) else: event_form = forms.EventForm(request.POST or None, editable_owner=False, instance=event) metrics_formset = forms.EventMetricsFormset(request.POST or None, instance=event) if (event_form.is_valid() and metrics_formset.is_valid()): event_form.save() metrics_formset.save() if created: messages.success(request, 'Event successfully created.') else: messages.success(request, 'Event successfully updated.') return redirect('events_view_event', slug=event_form.instance.slug) can_delete_event = False if (not created and (event.owner == request.user or request.user.has_perm('events.can_delete_events'))): can_delete_event = True return render( request, 'edit_event.html', { 'creating': created, 'event': event, 'event_form': event_form, 'metrics_formset': metrics_formset, 'can_delete_event': can_delete_event })
def manage_subscription(request, slug, subscribe=True): """Manage user's event subscription. When /subscribe/ == True then subscribe user to event if not already subscribed, else unsubscribe user. """ event = get_object_or_404(Event, slug=slug) if request.method == 'POST': attendance, created = get_or_create_instance(Attendance, user=get_user(request), event=event) if subscribe: if not created: messages.warning(request, ('You are already subscribed to ' 'this event.')) else: attendance.save() messages.success(request, ('You have subscribed to ' 'this event.')) else: if created: messages.warning(request, ('You are not subscribed ' 'to this event.')) elif request.user == event.owner: messages.error(request, ('Event owner cannot unsubscribe ' 'from event.')) else: attendance.delete() messages.success(request, ('You have unsubscribed ' 'from this event.')) return redirect('events_view_event', slug=event.slug)
def edit_event(request, slug=None): """Edit event view.""" event, created = get_or_create_instance(Event, slug=slug) if created: event.owner = request.user if request.user.groups.filter(name='Admin').count(): event_form = forms.EventForm(request.POST or None, editable_owner=True, instance=event) else: event_form = forms.EventForm(request.POST or None, editable_owner=False, instance=event) metrics_formset = forms.EventMetricsFormset(request.POST or None, instance=event) if (event_form.is_valid() and metrics_formset.is_valid()): event_form.save() metrics_formset.save() if created: messages.success(request, 'Event successfully created.') else: messages.success(request, 'Event successfully updated.') return redirect('events_view_event', slug=event_form.instance.slug) can_delete_event = False if (not created and (event.owner == request.user or request.user.has_perm('events.can_delete_events'))): can_delete_event = True return render(request, 'edit_event.html', {'creating': created, 'event': event, 'event_form': event_form, 'metrics_formset': metrics_formset, 'can_delete_event': can_delete_event})
def edit_voting(request, slug=None): """Create/Edit voting view.""" poll, created = get_or_create_instance(Poll, slug=slug) can_delete_voting = False extra_range_polls = 0 extra_radio_polls = 0 current_voting_edit = False range_poll_formset = None radio_poll_formset = None if created: poll.created_by = request.user extra_range_polls = 1 extra_radio_polls = 1 else: if not poll.range_polls.exists(): extra_range_polls = 1 if not poll.radio_polls.exists(): extra_radio_polls = 1 can_delete_voting = True if poll.is_current_voting: current_voting_edit = True if not user_has_poll_permissions(request.user, poll): messages.error(request, 'Permission denied.') return redirect('voting_list_votings') nominee_list = User.objects.filter(groups__name='Rep', userprofile__registration_complete=True) if current_voting_edit: poll_form = forms.PollEditForm(request.POST or None, instance=poll) else: RangePollFormset = (inlineformset_factory( Poll, RangePoll, formset=forms.BaseRangePollInlineFormSet, extra=extra_range_polls, can_delete=True)) RadioPollFormset = (inlineformset_factory( Poll, RadioPoll, formset=forms.BaseRadioPollInlineFormSet, extra=extra_radio_polls, can_delete=True)) range_poll_formset = RangePollFormset(request.POST or None, instance=poll, user_list=nominee_list) radio_poll_formset = RadioPollFormset(request.POST or None, instance=poll) poll_form = forms.PollAddForm(request.POST or None, instance=poll, radio_poll_formset=radio_poll_formset, range_poll_formset=range_poll_formset) if poll_form.is_valid(): poll_form.save() if created: messages.success(request, 'Voting successfully created.') statsd.incr('voting.create_voting') else: messages.success(request, 'Voting successfully edited.') statsd.incr('voting.edit_voting') if not current_voting_edit: statsd.incr('voting.create_range_poll', poll_form.radio_poll_formset.total_form_count()) statsd.incr('voting.create_radio_poll', poll_form.range_poll_formset.total_form_count()) return redirect('voting_edit_voting', slug=poll.slug) return render( request, 'edit_voting.html', { 'created': created, 'poll': poll, 'poll_form': poll_form, 'range_poll_formset': range_poll_formset, 'radio_poll_formset': radio_poll_formset, 'can_delete_voting': can_delete_voting, 'current_voting_edit': current_voting_edit, 'nominee_list': nominee_list })
def edit_event(request, slug=None, clone=None): """Edit event view.""" initial = {} extra_formsets = 2 event, created = get_or_create_instance(Event, slug=slug) if created: event.owner = request.user initial = { "country": request.user.userprofile.country, "city": request.user.userprofile.city, "region": request.user.userprofile.region, "timezone": request.user.userprofile.timezone, } else: # This is for backwards compatibility for all the events # that were set before the change in the minutes section # of the drop down widget to multiples of 5. # Start time: Floor rounding # End time: Ceilling rounding event.start -= timedelta(minutes=event.start.minute % 5) if (event.end.minute % 5) != 0: event.end += timedelta(minutes=(5 - (event.end.minute % 5))) if event.eventmetricoutcome_set.exists(): extra_formsets = 0 editable = False if request.user.groups.filter(name="Admin").count(): editable = True # Compatibility code for old metrics min_forms = 0 metrics_form = forms.EventMetricsForm event_form = forms.EventForm( request.POST or None, editable_owner=editable, instance=event, initial=initial, clone=clone, user=request.user ) if event.has_new_metrics or clone: min_forms = 2 if event.is_past_event and not clone: metrics_form = forms.PostEventMetricsForm event_form = forms.PostEventForm( request.POST or None, editable_owner=editable, instance=event, initial=initial ) EventMetricsFormset = inlineformset_factory( Event, Event.metrics.through, form=metrics_form, formset=forms.BaseEventMetricsFormset, extra=extra_formsets ) EventMetricsFormset.form = staticmethod(curry(metrics_form, clone=clone)) metrics_formset = EventMetricsFormset(request.POST or None, instance=event, min_forms=min_forms, clone=clone) if event_form.is_valid() and metrics_formset.is_valid(): event_form.save() if created: messages.success(request, "Event successfully created.") statsd.incr("events.create_event") elif clone: messages.success(request, "Event successfully created.") statsd.incr("events.clone_event") else: messages.success(request, "Event successfully updated.") statsd.incr("events.edit_event") metrics_formset.save() return redirect("events_view_event", slug=event_form.instance.slug) can_delete_event = False if not created and (event.owner == request.user or request.user.has_perm("events.can_delete_events")): can_delete_event = True selected_categories = map(int, event_form["categories"].value()) selected_goals = map(int, event_form["goals"].value()) return render( request, "edit_event.html", { "creating": created, "event": event, "event_form": event_form, "selected_categories": selected_categories, "selected_goals": selected_goals, "metrics_formset": metrics_formset, "can_delete_event": can_delete_event, }, )
def edit_event(request, slug=None, clone=None): """Edit event view.""" initial = {} extra_formsets = 2 event, created = get_or_create_instance(Event, slug=slug) if created: event.owner = request.user initial = {'country': request.user.userprofile.country, 'city': request.user.userprofile.city, 'region': request.user.userprofile.region, 'timezone': request.user.userprofile.timezone} else: # This is for backwards compatibility for all the events # that were set before the change in the minutes section # of the drop down widget to multiples of 5. # Start time: Floor rounding # End time: Ceilling rounding event.start -= timedelta(minutes=event.start.minute % 5) if (event.end.minute % 5) != 0: event.end += timedelta(minutes=(5 - (event.end.minute % 5))) if event.eventmetricoutcome_set.exists(): extra_formsets = 0 editable = False if request.user.groups.filter(name='Admin').count(): editable = True # Compatibility code for old metrics min_forms = 0 metrics_form = forms.EventMetricsForm event_form = forms.EventForm(request.POST or None, editable_owner=editable, instance=event, initial=initial) if event.has_new_metrics: min_forms = 2 if event.is_past_event: metrics_form = forms.PostEventMetricsForm event_form = forms.PostEventForm(request.POST or None, editable_owner=editable, instance=event, initial=initial) EventMetricsFormset = inlineformset_factory( Event, Event.metrics.through, form=metrics_form, formset=forms.BaseEventMetricsFormset, extra=extra_formsets ) metrics_formset = EventMetricsFormset(request.POST or None, instance=event, min_forms=min_forms) if event_form.is_valid() and metrics_formset.is_valid(): event_form.save(clone=clone) metrics_formset.save(clone=clone) if created: messages.success(request, 'Event successfully created.') if clone: statsd.incr('events.clone_event') else: statsd.incr('events.create_event') else: messages.success(request, 'Event successfully updated.') statsd.incr('events.edit_event') return redirect('events_view_event', slug=event_form.instance.slug) can_delete_event = False if (not created and (event.owner == request.user or request.user.has_perm('events.can_delete_events'))): can_delete_event = True selected_categories = map(int, event_form['categories'].value()) selected_goals = map(int, event_form['goals'].value()) return render(request, 'edit_event.html', {'creating': created, 'event': event, 'event_form': event_form, 'selected_categories': selected_categories, 'selected_goals': selected_goals, 'metrics_formset': metrics_formset, 'can_delete_event': can_delete_event})
def edit_voting(request, slug=None): """Create/Edit voting view.""" poll, created = get_or_create_instance(Poll, slug=slug) can_delete_voting = False extra = 0 current_voting_edit = False range_poll_formset = None radio_poll_formset = None if created: poll.created_by = request.user extra = 1 else: if (RangePoll.objects.filter(poll=poll).count() or RadioPoll.objects.filter(poll=poll).count()) == 0: extra = 1 can_delete_voting = True date_now = make_aware(datetime.utcnow(), pytz.UTC) if poll.start < date_now and poll.end > date_now: current_voting_edit = True if current_voting_edit: poll_form = forms.PollEditForm(request.POST or None, instance=poll) else: poll_form = forms.PollAddForm(request.POST or None, instance=poll) RangePollFormset = (inlineformset_factory(Poll, RangePoll, formset=forms.BaseRangePollInlineFormSet, extra=extra, can_delete=True)) RadioPollFormset = (inlineformset_factory(Poll, RadioPoll, formset=forms.BaseRadioPollInlineFormSet, extra=extra, can_delete=True)) range_poll_formset = RangePollFormset(request.POST or None, instance=poll) radio_poll_formset = RadioPollFormset(request.POST or None, instance=poll) if poll_form.is_valid(): if current_voting_edit: poll_form.save() elif range_poll_formset.is_valid() and radio_poll_formset.is_valid(): poll_form.save() range_poll_formset.save_all() radio_poll_formset.save_all() if created: messages.success(request, 'Voting successfully created.') else: messages.success(request, 'Voting successfully edited.') return redirect('voting_edit_voting', slug=poll.slug) return render(request, 'edit_voting.html', {'creating': created, 'poll': poll, 'poll_form': poll_form, 'range_poll_formset': range_poll_formset, 'radio_poll_formset': radio_poll_formset, 'can_delete_voting': can_delete_voting, 'current_voting_edit': current_voting_edit})
def edit_voting(request, slug=None): """Create/Edit voting view.""" poll, created = get_or_create_instance(Poll, slug=slug) can_delete_voting = False extra_range_polls = 0 extra_radio_polls = 0 current_voting_edit = False range_poll_formset = None radio_poll_formset = None if created: poll.created_by = request.user extra_range_polls = 1 extra_radio_polls = 1 else: if not poll.range_polls.exists(): extra_range_polls = 1 if not poll.radio_polls.exists(): extra_radio_polls = 1 can_delete_voting = True if poll.is_current_voting: current_voting_edit = True if not user_has_poll_permissions(request.user, poll): messages.error(request, 'Permission denied.') return redirect('voting_list_votings') nominee_list = (User.objects .filter(groups__name='Rep', userprofile__registration_complete=True) .order_by('first_name')) if current_voting_edit: poll_form = forms.PollEditForm(request.POST or None, instance=poll) else: RangePollFormset = (inlineformset_factory(Poll, RangePoll, formset=forms.BaseRangePollInlineFormSet, extra=extra_range_polls, can_delete=True)) RadioPollFormset = (inlineformset_factory(Poll, RadioPoll, formset=forms.BaseRadioPollInlineFormSet, extra=extra_radio_polls, can_delete=True)) range_poll_formset = RangePollFormset(request.POST or None, instance=poll, user_list=nominee_list) radio_poll_formset = RadioPollFormset(request.POST or None, instance=poll) poll_form = forms.PollAddForm(request.POST or None, instance=poll, radio_poll_formset=radio_poll_formset, range_poll_formset=range_poll_formset) if poll_form.is_valid(): poll_form.save() if created: messages.success(request, 'Voting successfully created.') statsd.incr('voting.create_voting') else: messages.success(request, 'Voting successfully edited.') statsd.incr('voting.edit_voting') if not current_voting_edit: statsd.incr('voting.create_range_poll', poll_form.radio_poll_formset.total_form_count()) statsd.incr('voting.create_radio_poll', poll_form.range_poll_formset.total_form_count()) return redirect('voting_edit_voting', slug=poll.slug) return render(request, 'edit_voting.html', {'created': created, 'poll': poll, 'poll_form': poll_form, 'range_poll_formset': range_poll_formset, 'radio_poll_formset': radio_poll_formset, 'can_delete_voting': can_delete_voting, 'current_voting_edit': current_voting_edit, 'nominee_list': nominee_list})
def edit_report(request, display_name, year, month): """Edit report view.""" user = get_object_or_404(User, userprofile__display_name=display_name) year_month = datetime.datetime(year=int(year), month=utils.month2number(month), day=1) report, created = utils.get_or_create_instance(Report, user=user, month=year_month) ReportLinkFormset = inlineformset_factory(Report, ReportLink, extra=1) ReportEventFormset = inlineformset_factory(Report, ReportEvent, extra=1) if request.method == 'POST': # Make sure that users without permission do not modify # overdue field. data = request.POST.copy() if not request.user.has_perm('reports.can_edit_report'): data['overdue'] = report.overdue report_form = forms.ReportForm(data, instance=report) report_event_formset = ReportEventFormset(data, instance=report) report_link_formset = ReportLinkFormset(data, instance=report) if ((report_form.is_valid() and report_event_formset.is_valid() and report_link_formset.is_valid())): report_form.save() report_event_formset.save() report_link_formset.save() if created: messages.success(request, 'Report successfully created.') else: messages.success(request, 'Report successfully updated.') return redirect(reverse('reports_view_report', kwargs={'display_name': display_name, 'year': year, 'month': month})) else: initial = [] if created: events = user.events_attended.filter(start__year=year_month.year, start__month=year_month.month) for event in events: participation_type = participation_type_to_number( get_attendee_role_event(user, event)) event_url = reverse('events_view_event', kwargs={'slug': event.slug}) initial.append({'name': event.name, 'description': event.description, 'link': urljoin(settings.SITE_URL, event_url), 'participation_type': participation_type}) ReportEventFormset = inlineformset_factory(Report, ReportEvent, extra=events.count()+1) report_form = forms.ReportForm(instance=report) report_link_formset = ReportLinkFormset(instance=report) report_event_formset = ReportEventFormset(instance=report, initial=initial) return render(request, 'edit_report.html', {'report_form': report_form, 'report_event_formset': report_event_formset, 'report_link_formset': report_link_formset, 'pageuser': user, 'year': year, 'month': month, 'created': created})
def edit_event(request, slug=None, clone=None): """Edit event view.""" initial = {} extra_formsets = 2 event, created = get_or_create_instance(Event, slug=slug) if created: event.owner = request.user initial = { 'country': request.user.userprofile.country, 'city': request.user.userprofile.city, 'region': request.user.userprofile.region, 'timezone': request.user.userprofile.timezone } else: # This is for backwards compatibility for all the events # that were set before the change in the minutes section # of the drop down widget to multiples of 5. # Start time: Floor rounding # End time: Ceilling rounding event.start -= timedelta(minutes=event.start.minute % 5) if (event.end.minute % 5) != 0: event.end += timedelta(minutes=(5 - (event.end.minute % 5))) if event.eventmetricoutcome_set.exists(): extra_formsets = 0 editable = False if request.user.groups.filter(name='Admin').count(): editable = True # Compatibility code for old metrics min_forms = 0 metrics_form = forms.EventMetricsForm event_form = forms.EventForm(request.POST or None, editable_owner=editable, instance=event, initial=initial, clone=clone, user=request.user) if event.has_new_metrics or clone: min_forms = 2 if event.is_past_event and not clone: metrics_form = forms.PostEventMetricsForm event_form = forms.PostEventForm(request.POST or None, editable_owner=editable, instance=event, initial=initial) EventMetricsFormset = inlineformset_factory( Event, Event.metrics.through, form=metrics_form, formset=forms.BaseEventMetricsFormset, extra=extra_formsets) EventMetricsFormset.form = staticmethod(curry(metrics_form, clone=clone)) metrics_formset = EventMetricsFormset(request.POST or None, instance=event, min_forms=min_forms, clone=clone) if event_form.is_valid() and metrics_formset.is_valid(): event_form.save() if created: messages.success(request, 'Event successfully created.') statsd.incr('events.create_event') elif clone: messages.success(request, 'Event successfully created.') statsd.incr('events.clone_event') else: messages.success(request, 'Event successfully updated.') statsd.incr('events.edit_event') metrics_formset.save() return redirect('events_view_event', slug=event_form.instance.slug) can_delete_event = False if (not created and (event.owner == request.user or request.user.has_perm('events.can_delete_events'))): can_delete_event = True return render( request, 'edit_event.jinja', { 'creating': created, 'event': event, 'event_form': event_form, 'metrics_formset': metrics_formset, 'can_delete_event': can_delete_event })
def edit_report(request, display_name, year, month): """Edit report view.""" user = get_object_or_404(User, userprofile__display_name=display_name) year_month = datetime(year=int(year), month=utils.month2number(month), day=1) report, created = utils.get_or_create_instance(Report, user=user, month=year_month) ReportLinkFormset = inlineformset_factory(Report, ReportLink, extra=1) ReportEventFormset = inlineformset_factory(Report, ReportEvent, extra=1) if request.method == 'POST': # Make sure that users without permission do not modify # overdue field. data = request.POST.copy() if not request.user.has_perm('reports.can_edit_report'): data['overdue'] = report.overdue report_form = forms.ReportForm(data, instance=report) report_event_formset = ReportEventFormset(data, instance=report) report_link_formset = ReportLinkFormset(data, instance=report) if ((report_form.is_valid() and report_event_formset.is_valid() and report_link_formset.is_valid())): report_form.save() report_event_formset.save() report_link_formset.save() if created: messages.success(request, 'Report successfully created.') else: messages.success(request, 'Report successfully updated.') return redirect( reverse('reports_view_report', kwargs={ 'display_name': display_name, 'year': year, 'month': month })) else: initial = [] if created: events = user.events_attended.filter(start__year=year_month.year, start__month=year_month.month) for event in events: participation_type = participation_type_to_number( get_attendee_role_event(user, event)) event_url = reverse('events_view_event', kwargs={'slug': event.slug}) initial.append({ 'name': event.name, 'description': event.description, 'link': urljoin(settings.SITE_URL, event_url), 'participation_type': participation_type }) ReportEventFormset = inlineformset_factory(Report, ReportEvent, extra=events.count() + 1) report_form = forms.ReportForm(instance=report) report_link_formset = ReportLinkFormset(instance=report) report_event_formset = ReportEventFormset(instance=report, initial=initial) return render( request, 'edit_report.html', { 'report_form': report_form, 'report_event_formset': report_event_formset, 'report_link_formset': report_link_formset, 'pageuser': user, 'year': year, 'month': month, 'created': created })
def edit_event(request, slug=None, clone=None): """Edit event view.""" event, created = get_or_create_instance(Event, slug=slug) initial = {} if created: event.owner = request.user initial = { "country": request.user.userprofile.country, "city": request.user.userprofile.city, "region": request.user.userprofile.region, } else: # This is for backwards compatibility for all the events # that were set before the change in the minutes section # of the drop down widget to multiples of 5. # Start time: Floor rounding # End time: Ceilling rounding event.start -= timedelta(minutes=event.start.minute % 5) if (event.end.minute % 5) != 0: event.end += timedelta(minutes=(5 - (event.end.minute % 5))) editable = False if request.user.groups.filter(name="Admin").count(): editable = True event_form = forms.EventForm(request.POST or None, editable_owner=editable, instance=event, initial=initial) metrics_formset = forms.EventMetricsFormset(request.POST or None, instance=event) if event_form.is_valid() and metrics_formset.is_valid() and request.POST: event_form.save(clone=clone) metrics_formset.save(clone=clone) if created: messages.success(request, "Event successfully created.") else: messages.success(request, "Event successfully updated.") return redirect("events_view_event", slug=event_form.instance.slug) can_delete_event = False if not created and (event.owner == request.user or request.user.has_perm("events.can_delete_events")): can_delete_event = True selected_categories = map(int, event_form["categories"].value()) return render( request, "edit_event.html", { "creating": created, "event": event, "event_form": event_form, "selected_categories": selected_categories, "metrics_formset": metrics_formset, "can_delete_event": can_delete_event, }, )