Ejemplo n.º 1
0
    def create_citizen(self, request):
        """Create citizen report"""
        try:
            citizen = FMSUser.objects.get(email=request.data.get("citizen-email"))
        except FMSUser.DoesNotExist:
            citizen_form = CitizenForm(request.data, prefix="citizen")
            if not citizen_form.is_valid():
                return HttpResponse(unicode(citizen_form.errors), status=400)
            citizen = citizen_form.save()

        # Create report self'''
        report_form = CitizenReportForm(request.data, prefix="report")
        comment_form = ReportCommentForm(request.data, prefix="comment")
        if not report_form.is_valid():
            return HttpResponse(unicode(report_form.errors), status=400)

        # report = report_form.saveForMobile(commit=False)
        report = report_form.save(commit=False)
        report.citizen = citizen
        # report.category = ReportMainCategoryClass(request.data['secondary_category'])
        # report.secondary_category = ReportCategory(request.data['category'])
        report.save()
        if request.data.get("subscription") == "true":
            report.subscribe_author_ws()

        # Create the comment is a comment has been given'''

        if (request.data["comment-text"] or comment_form.is_valid()) and request.data["comment-text"] != "":
            comment = comment_form.save(commit=False)
            comment.created_by = citizen
            comment.report = report
            comment.save()

        return report
Ejemplo n.º 2
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_zips": 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))