Пример #1
0
def create_citizen(request):
    # Get user in DB or create it

    citizen_form = CitizenForm(request.POST, prefix='citizen')

    if not citizen_form.is_valid():
        return HttpResponse(unicode(citizen_form.errors), status=400)

    citizen = citizen_form.save()

    # Create report
    report_form = CitizenReportForm(request.POST, prefix='report')
    comment_form = ReportCommentForm(request.POST, prefix='comment')

    if not report_form.is_valid():
        return HttpResponse(unicode(report_form.errors), status=400)

    report = report_form.save(commit=False)
    report.citizen = citizen
    report.save()

    # Subscribe if wanted
    if (request.POST.get('subscription') == 'true'):
        report.subscribe_author_ws()

    # Create the comment if exists
    if ((request.POST["comment-text"] or comment_form.is_valid()) and request.POST["comment-text"] != ''):
        comment = comment_form.save(commit=False)
        comment.created_by = citizen
        comment.report = report
        comment.is_new_report = True
        comment.save()

    return report
Пример #2
0
def create_citizen(request,
                   several_occurences=False,
                   mobile_notification=False):
    # Get user in DB or create it

    citizen_form = CitizenForm(request.POST, prefix='citizen')

    if not citizen_form.is_valid():
        return HttpResponse(unicode(citizen_form.errors), status=400)

    citizen = citizen_form.save()

    # Create report
    report_form = CitizenReportForm(request.POST, prefix='report')
    comment_form = ReportCommentForm(request.POST, prefix='comment')

    if not report_form.is_valid():
        return HttpResponse(unicode(report_form.errors), status=400)

    report = report_form.save(commit=False)
    report.citizen = citizen
    report.several_occurences = several_occurences
    report.mobile_notification = mobile_notification
    report.forceAutoDispatching = False
    report.save()

    # Subscribe if wanted
    if (request.POST.get('subscription') == 'true'):
        report.subscribe_author_ws()

    # Create the comment if exists
    if ((request.POST["comment-text"] or comment_form.is_valid())
            and request.POST["comment-text"] != ''):
        comment = comment_form.save(commit=False)
        comment.created_by = citizen
        comment.report = report
        comment.is_new_report = True
        comment.save()

    # Commit report after comment to get this one when triggering to partners
    report.forceAutoDispatching = True
    report.save()
    report.forceAutoDispatching = False

    return report
Пример #3
0
def create_citizen(request, several_occurences=False, mobile_notification=False):
    # Get user in DB or create it

    citizen_form = CitizenForm(request.POST, prefix='citizen')

    if not citizen_form.is_valid():
        return HttpResponse(unicode(citizen_form.errors), status=400)

    citizen = citizen_form.save()

    # Create report
    report_form = CitizenReportForm(request.POST, prefix='report')
    comment_form = ReportCommentForm(request.POST, prefix='comment')

    if not report_form.is_valid():
        return HttpResponse(unicode(report_form.errors), status=400)

    report = report_form.save(commit=False)
    report.citizen = citizen
    report.several_occurences = several_occurences
    report.mobile_notification = mobile_notification
    report.forceAutoDispatching = False
    report.save()

    # Subscribe if wanted
    if (request.POST.get('subscription') == 'true'):
        report.subscribe_author_ws()

    # Create the comment if exists
    if ((request.POST["comment-text"] or comment_form.is_valid()) and request.POST["comment-text"] != ''):
        comment = comment_form.save(commit=False)
        comment.created_by = citizen
        comment.report = report
        comment.is_new_report = True
        comment.save()

    # Commit report after comment to get this one when triggering to partners
    report.forceAutoDispatching = True
    report.save()
    report.forceAutoDispatching = False

    return report
Пример #4
0
def new(request):
    ReportFileFormSet = inlineformset_factory(Report, ReportFile, form=ReportFileForm, extra=0)

    pnt = dict_to_point(request.REQUEST)
    report = None
    file_formset = ReportFileFormSet(prefix='files', queryset=ReportFile.objects.none())

    if request.method == "POST":
        report_form = CitizenReportForm(request.POST, request.FILES, prefix='report')
        comment_form = ReportCommentForm(request.POST, request.FILES, prefix='comment')
        citizen_form = CitizenForm(request.POST, request.FILES, prefix='citizen')

        fingerprint = RequestFingerprint(request)

        # this checks update is_valid too
        if report_form.is_valid() \
                and (not request.POST["comment-text"] or comment_form.is_valid()) \
                and citizen_form.is_valid() \
                and not fingerprint.is_duplicate():

            # this saves the update as part of the report.
            report = report_form.save(commit=False)

            # Use `hack_multi_file` instead of ``request.FILES``.
            file_formset = ReportFileFormSet(request.POST, hack_multi_file(request), instance=report, prefix='files',
                                             queryset=ReportFile.objects.none())

            if file_formset.is_valid():
                fingerprint.save()

                citizen = citizen_form.save()

                report.citizen = citizen
                report.quality = citizen_form.cleaned_data["quality"]
                report.save()

                if report_form.cleaned_data['subscription']:
                    report.subscribe_author()

                if request.POST["comment-text"]:
                    comment = comment_form.save(commit=False)
                    comment.created_by = citizen
                    comment.report = report
                    # Used for comment post_save signal:
                    comment.is_new_report = True
                    comment.save()

                files = file_formset.save(commit=False)
                for report_file in files:
                    report_file.created_by = citizen
                    # report_file.report = report
                    # Used for file post_save signal:
                    report_file.is_new_report = True
                    report_file.save()
                messages.add_message(request, messages.SUCCESS, _("Newly created report successfull"))
                return HttpResponseRedirect(report.get_absolute_url())

            else:
                report = None

    else:
        report_form = CitizenReportForm(initial={
            'x': request.REQUEST.get('x'),
            'y': request.REQUEST.get('y')
        }, prefix='report')
        file_formset = ReportFileFormSet(prefix='files', queryset=ReportFile.objects.none())
        comment_form = ReportCommentForm(prefix='comment')
        citizen_form = CitizenForm(prefix='citizen')

    return render_to_response("reports/new.html", {
        "report": report,
        "available_zips": ZipCode.objects,
        "all_zipcodes": ZipCode.objects.all(),
        "category_classes": ReportMainCategoryClass.objects.prefetch_related('categories').all().order_by(
            'name_' + get_language()),
        "comment_form": comment_form,
        "file_formset": file_formset,
        "report_form": report_form,
        "citizen_form": citizen_form,
        "pnt": pnt
    }, context_instance=RequestContext(request))
Пример #5
0
def new(request):
    ReportFileFormSet = inlineformset_factory(Report,
                                              ReportFile,
                                              form=ReportFileForm,
                                              extra=0)

    pnt = dict_to_point(request.REQUEST)
    report = None
    file_formset = ReportFileFormSet(prefix='files',
                                     queryset=ReportFile.objects.none())

    if request.method == "POST":
        report_form = CitizenReportForm(request.POST,
                                        request.FILES,
                                        prefix='report')
        comment_form = ReportCommentForm(request.POST,
                                         request.FILES,
                                         prefix='comment')
        citizen_form = CitizenForm(request.POST,
                                   request.FILES,
                                   prefix='citizen')

        fingerprint = RequestFingerprint(request)

        # this checks update is_valid too
        if report_form.is_valid() \
                and (not request.POST["comment-text"] or comment_form.is_valid()) \
                and citizen_form.is_valid() \
                and not fingerprint.is_duplicate():

            # this saves the update as part of the report.
            report = report_form.save(commit=False)

            # Use `hack_multi_file` instead of ``request.FILES``.
            file_formset = ReportFileFormSet(
                request.POST,
                hack_multi_file(request),
                instance=report,
                prefix='files',
                queryset=ReportFile.objects.none())

            if file_formset.is_valid():
                fingerprint.save()

                citizen = citizen_form.save()

                report.citizen = citizen
                report.quality = citizen_form.cleaned_data["quality"]
                report.forceAutoDispatching = False
                report.save()

                if report_form.cleaned_data['subscription']:
                    report.subscribe_author()

                if request.POST["comment-text"]:
                    comment = comment_form.save(commit=False)
                    comment.created_by = citizen
                    comment.report = report
                    # Used for comment post_save signal:
                    comment.is_new_report = True
                    comment.save()

                files = file_formset.save(commit=False)
                for report_file in files:
                    report_file.created_by = citizen
                    # report_file.report = report
                    # Used for file post_save signal:
                    report_file.is_new_report = True
                    report_file.save()

                report.forceAutoDispatching = True
                report.save()

                messages.add_message(request, messages.SUCCESS,
                                     _("Newly created report successfull"))
                return HttpResponseRedirect(report.get_absolute_url())

            else:
                report = None

    else:
        report_form = CitizenReportForm(initial={
            'x': request.REQUEST.get('x'),
            'y': request.REQUEST.get('y')
        },
                                        prefix='report')
        file_formset = ReportFileFormSet(prefix='files',
                                         queryset=ReportFile.objects.none())
        comment_form = ReportCommentForm(prefix='comment')
        citizen_form = CitizenForm(prefix='citizen')

    return render_to_response("reports/new.html", {
        "report":
        report,
        "available_zips":
        ZipCode.objects,
        "all_zipcodes":
        ZipCode.objects.all(),
        "category_classes":
        ReportMainCategoryClass.objects.prefetch_related(
            'categories').all().order_by('name_' + get_language()),
        "comment_form":
        comment_form,
        "file_formset":
        file_formset,
        "report_form":
        report_form,
        "citizen_form":
        citizen_form,
        "pnt":
        pnt
    },
                              context_instance=RequestContext(request))