Ejemplo n.º 1
0
def create_report(request, several_occurences=False, mobile_notification=False):
    fingerprint = RequestFingerprint(request)

    if not fingerprint.is_duplicate():
        fingerprint.save()

        # Check if citizen or pro
        if request.POST.get('username', False):
            report = create_pro(request, several_occurences, mobile_notification)
        else:
            report = create_citizen(request, several_occurences, mobile_notification)

        if isinstance(report, HttpResponse):
            return report

        # Set source
        if request.POST.get('source', '').lower() == 'web':
            report.source = Report.SOURCES['WEB']
        else:
            report.source = Report.SOURCES['MOBILE']
        report.save()

        # Return json response
        response = {
            'id': report.id,
            'point': [report.point.x, report.point.y],
            'status': report.status,
            'get_public_status_display': report.get_public_status_display(),
            'get_status_display': report.get_status_display(),
            'created': report.created,
            'created_by': report.created_by.get_display_name() if report.created_by else "",
            'is_pro': report.is_pro(),
            'contractor': report.contractor,
            'category': report.category.name,
            'secondary_category': report.secondary_category.name,
            'sub_category': report.sub_category.name if report.sub_category else "",
            'address': report.display_address(),
            'address_number': report.address_number,
            'address_regional': report.is_regional(),
            'postalcode': report.postalcode,
            'source': report.source,
        }

        return HttpResponse(json.dumps(response, cls=DjangoJSONEncoder), content_type="application/json; charset=utf-8")

    response = { "error" : "Duplicate fingerprint"}
    return HttpResponse(json.dumps(response, cls=DjangoJSONEncoder), content_type="application/json; charset=utf-8")
Ejemplo n.º 2
0
def new(request):
    pnt = dict_to_point(request.REQUEST)
    ReportFileFormSet = inlineformset_factory(Report,
                                              ReportFile,
                                              form=ReportFileForm,
                                              extra=0)
    report = None
    if request.method == "POST":
        report_form = ProReportForm(request.POST,
                                    request.FILES,
                                    prefix='report')
        comment_form = ReportCommentForm(request.POST,
                                         request.FILES,
                                         prefix='comment')

        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 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():
                user = FMSUser.objects.get(pk=request.user.id)

                fingerprint.save()

                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.report = report
                    comment.created_by = user
                    # 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 = user
                    # 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"),
                                     extra_tags="new_report")
                return HttpResponseRedirect(report.get_absolute_url_pro())
            else:
                report = None

    else:
        report_form = ProReportForm(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')

    return render_to_response("pro/reports/new.html", {
        "report":
        report,
        "all_zipcodes":
        ZipCode.objects.all(),
        "category_classes":
        ReportMainCategoryClass.objects.prefetch_related('categories').all(),
        "report_form":
        report_form,
        "pnt":
        pnt,
        "file_formset":
        file_formset,
        "comment_form":
        comment_form,
        "isNewIncident":
        True
    },
                              context_instance=RequestContext(request))
Ejemplo n.º 3
0
def new(request):
    pnt = dict_to_point(request.REQUEST)
    ReportFileFormSet = inlineformset_factory(Report, ReportFile, form=ReportFileForm, extra=0)
    report = None
    if request.method == "POST":
        report_form = ProReportForm(request.POST, request.FILES, prefix='report')
        comment_form = ReportCommentForm(request.POST, request.FILES, prefix='comment')

        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 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():
                user = FMSUser.objects.get(pk=request.user.id)

                fingerprint.save()

                report.save()

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

                if request.POST["comment-text"]:
                    comment = comment_form.save(commit=False)
                    comment.report = report
                    comment.created_by = user
                    # 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 = user
                    # 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"), extra_tags="new_report")
                return HttpResponseRedirect(report.get_absolute_url_pro())
            else:
                report = None

    else:
        report_form = ProReportForm(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')

    return render_to_response("pro/reports/new.html", {
        "report": report,
        "all_zipcodes": ZipCode.objects.all(),
        "category_classes": ReportMainCategoryClass.objects.prefetch_related('categories').all(),
        "report_form": report_form,
        "pnt": pnt,
        "file_formset": file_formset,
        "comment_form": comment_form,
    }, context_instance=RequestContext(request))
Ejemplo n.º 4
0
def create_report(request,
                  several_occurences=False,
                  mobile_notification=False):
    fingerprint = RequestFingerprint(request)

    if not fingerprint.is_duplicate():
        fingerprint.save()

        # Check if citizen or pro
        if request.POST.get('username', False):
            report = create_pro(request, several_occurences,
                                mobile_notification)
        else:
            report = create_citizen(request, several_occurences,
                                    mobile_notification)

        if isinstance(report, HttpResponse):
            return report

        # Set source
        if request.POST.get('source', '').lower() == 'web':
            report.source = Report.SOURCES['WEB']
        else:
            report.source = Report.SOURCES['MOBILE']
        report.save()

        # Return json response
        response = {
            'id':
            report.id,
            'point': [report.point.x, report.point.y],
            'status':
            report.status,
            'get_public_status_display':
            report.get_public_status_display(),
            'get_status_display':
            report.get_status_display(),
            'created':
            report.created,
            'created_by':
            report.created_by.get_display_name() if report.created_by else "",
            'is_pro':
            report.is_pro(),
            'contractor':
            report.contractor,
            'category':
            report.category.name,
            'secondary_category':
            report.secondary_category.name,
            'sub_category':
            report.sub_category.name if report.sub_category else "",
            'address':
            report.display_address(),
            'address_number':
            report.address_number,
            'address_regional':
            report.is_regional(),
            'postalcode':
            report.postalcode,
            'source':
            report.source,
        }

        return HttpResponse(json.dumps(response, cls=DjangoJSONEncoder),
                            content_type="application/json; charset=utf-8")

    response = {"error": "Duplicate fingerprint"}
    return HttpResponse(json.dumps(response, cls=DjangoJSONEncoder),
                        content_type="application/json; charset=utf-8")