def report_add_event(sender, instance, raw, **kwargs): """Add event to report.""" if raw or not instance.user.groups.filter(name='Rep').exists(): return date = datetime.datetime(year=instance.event.end.year, month=instance.event.end.month, day=1) report, created = Report.objects.get_or_create(user=instance.user, month=date) link = (settings.SITE_URL + reverse('events_view_event', kwargs={'slug': instance.event.slug})) # Import here to avoid circular dependencies. from utils import participation_type_to_number participation_type = participation_type_to_number( get_attendee_role_event(instance.user, instance.event)) report_event = get_object_or_none(ReportEvent, report=report, link=link) if not report_event: report_event = ReportEvent(report=report, link=link, name=instance.event.name, description=instance.event.description) report_event.participation_type = participation_type report_event.save()
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_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})