Exemplo n.º 1
0
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})
Exemplo n.º 2
0
def view_report(request, display_name, year, month):
    """View report view."""
    month_number = utils.month2number(month)
    report = get_object_or_404(Report,
                               user__userprofile__display_name=display_name,
                               month__year=int(year),
                               month__month=month_number)

    if request.method == 'POST':
        if not request.user.is_authenticated():
            messages.error(request, 'Permission denied.')
            return redirect('main')

        report_comment = ReportComment(report=report, user=request.user)
        report_comment_form = forms.ReportCommentForm(request.POST,
                                                      instance=report_comment)
        if report_comment_form.is_valid():
            report_comment_form.save()
            messages.success(request, 'Comment saved.')

            # provide a new clean form
            report_comment_form = forms.ReportCommentForm()
    else:
        report_comment_form = forms.ReportCommentForm()

    report_url = reverse('reports_view_report',
                         kwargs={
                             'display_name': display_name,
                             'year': year,
                             'month': month
                         })

    if (request.user.groups.filter(name='Admin').exists()
            or (request.user == report.user)
            or (request.user.is_authenticated()
                and report.user in request.user.mentees.all())):
        editable = True
    else:
        editable = False

    q = Q(name='Admin') | Q(name='Council') | Q(name='Mentor')
    if (request.user.groups.filter(q).exists() or request.user == report.user):
        view_status = True
    else:
        view_status = False

    return render(
        request, 'view_report.html', {
            'pageuser': report.user,
            'user_profile': report.user.userprofile,
            'editable': editable,
            'view_status': view_status,
            'report': report,
            'month': month,
            'year': year,
            'comments': report.reportcomment_set.all(),
            'report_comment_form_url': report_url,
            'report_comment_form': report_comment_form
        })
Exemplo n.º 3
0
def view_report(request, display_name, year, month):
    """View report view."""
    month_number = utils.month2number(month)
    report = get_object_or_404(Report,
                               user__userprofile__display_name=display_name,
                               month__year=int(year),
                               month__month=month_number)

    if request.method == 'POST':
        if not request.user.is_authenticated():
            messages.error(request, 'Permission denied.')
            return redirect('main')

        report_comment = ReportComment(report=report, user=request.user)
        report_comment_form = forms.ReportCommentForm(request.POST,
                                                      instance=report_comment)
        if report_comment_form.is_valid():
            report_comment_form.save()
            messages.success(request, 'Comment saved.')

            # provide a new clean form
            report_comment_form = forms.ReportCommentForm()
    else:
        report_comment_form = forms.ReportCommentForm()

    report_url = reverse('reports_view_report',
                         kwargs={'display_name': display_name,
                                 'year': year,
                                 'month': month})

    if (request.user.groups.filter(name='Admin').exists() or
        (request.user == report.user) or
        (request.user.is_authenticated() and
         report.user in request.user.mentees.all())):
        editable = True
    else:
        editable = False

    q = Q(name='Admin') | Q(name='Council') | Q(name='Mentor')
    if (request.user.groups.filter(q).exists() or request.user == report.user):
        view_status = True
    else:
        view_status = False

    return render(request, 'view_report.html',
                  {'pageuser': report.user,
                   'user_profile': report.user.userprofile,
                   'editable': editable,
                   'view_status': view_status,
                   'report': report,
                   'month': month,
                   'year': year,
                   'comments': report.reportcomment_set.all(),
                   'report_comment_form_url': report_url,
                   'report_comment_form': report_comment_form})
Exemplo n.º 4
0
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})
Exemplo n.º 5
0
def delete_report(request, display_name, year, month):
    """Delete report view."""
    user = get_object_or_404(User, userprofile__display_name=display_name)
    if request.method == 'POST':
        year_month = datetime.datetime(year=int(year),
                                       month=utils.month2number(month), day=1)
        report = get_object_or_404(Report, user=user, month=year_month)
        report.delete()
        messages.success(request, 'Report successfully deleted.')

    if request.user == user:
        return redirect('profiles_view_my_profile')
    else:
        redirect_url = reverse(
            'profiles_view_profile',
            kwargs={'display_name': user.userprofile.display_name})
        return redirect(redirect_url)
Exemplo n.º 6
0
def delete_report(request, display_name, year, month):
    """Delete report view."""
    if request.method == 'POST':
        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 = get_object_or_404(Report, user=user, month=year_month)
        report.delete()
        messages.success(request, 'Report successfully deleted.')

    if request.user == user:
        return redirect('profiles_view_my_profile')
    else:
        redirect_url = reverse(
            'profiles_view_profile',
            kwargs={'display_name': user.userprofile.display_name})
        return redirect(redirect_url)
Exemplo n.º 7
0
def list_ng_reports(request, mentor=None, rep=None, functional_area_slug=None):
    today = now().date()
    report_list = NGReport.objects.filter(report_date__lte=today)
    pageheader = 'Activities for Reps'
    user = None
    pageuser_is_mentor = False

    if mentor or rep:
        user = get_object_or_404(User, userprofile__display_name__iexact=mentor or rep)

        if mentor:
            report_list = report_list.filter(mentor=user)
            pageheader += ' mentored by %s' % user.get_full_name()
            pageuser_is_mentor = True
        elif rep:
            report_list = report_list.filter(user=user)
            pageheader = 'Activities for %s' % user.get_full_name()

    if functional_area_slug:
        functional_area = get_object_or_404(FunctionalArea, slug=functional_area_slug)
        report_list = report_list.filter(functional_areas=functional_area)
        pageheader += ' for area %s' % functional_area.name

    month = request.GET.get('month')
    year = request.GET.get('year')
    if month and year:
        try:
            month = month2number(month)
            # Make sure that year is an integer too
            year = int(year)
        except (TypeError, ValueError):
            raise Http404()
        report_list = report_list.filter(report_date__year=year, report_date__month=month)

    if 'query' in request.GET:
        query = request.GET['query'].strip()
        report_list = report_list.filter(
            Q(ngreportcomment__comment__icontains=query) |
            Q(activity__name__icontains=query) |
            Q(activity_description__icontains=query) |
            Q(campaign__name__icontains=query) |
            Q(functional_areas__name__icontains=query) |
            Q(location__icontains=query) |
            Q(link__icontains=query) |
            Q(link_description__icontains=query) |
            Q(user__first_name__icontains=query) |
            Q(user__last_name__icontains=query) |
            Q(user__userprofile__local_name__icontains=query) |
            Q(user__userprofile__display_name__icontains=query) |
            Q(mentor__first_name__icontains=query) |
            Q(mentor__last_name__icontains=query) |
            Q(mentor__userprofile__local_name__icontains=query) |
            Q(mentor__userprofile__display_name__icontains=query))

    report_list = report_list.distinct()
    number_of_reports = report_list.count()

    sort_key = request.GET.get('sort_key', LIST_NG_REPORTS_DEFAULT_SORT)
    if sort_key not in LIST_NG_REPORTS_VALID_SORTS:
        sort_key = LIST_NG_REPORTS_DEFAULT_SORT

    sort_by = LIST_NG_REPORTS_VALID_SORTS[sort_key]
    report_list = report_list.order_by(*sort_by.split(','))

    paginator = Paginator(report_list, settings.ITEMS_PER_PAGE)

    # Make sure page request is an int. If not, deliver first page.
    try:
        page = int(request.GET.get('page', '1'))
    except ValueError:
        page = 1

    # If page request (9999) is out of range, deliver last page of results.
    try:
        reports = paginator.page(page)
    except (EmptyPage, InvalidPage):
        reports = paginator.page(paginator.num_pages)

    return render(request, 'list_ng_reports.jinja',
                  {'objects': reports,
                   'number_of_reports': number_of_reports,
                   'sort_key': sort_key,
                   'pageheader': pageheader,
                   'pageuser': user,
                   'pageuser_is_mentor': pageuser_is_mentor,
                   'query': request.GET.get('query', '')})
Exemplo n.º 8
0
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
        })
Exemplo n.º 9
0
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})