コード例 #1
0
ファイル: api.py プロジェクト: IMIO/django-fixmystreet
    def create_pro(self, request):
        """Create pro report"""
        # Login the user
        user = authenticate(username=request.data.get("username"), password=request.data.get("password"))
        if user and user.is_active:
            login(request, user)
        else:
            return HttpResponseForbidden("invalid username or password")

        # Create report self'''
        report_form = ProReportForm(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.private = True
        report.save()

        report.subscribe_author()

        # 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.report = report
            comment.save()

        return report
コード例 #2
0
ファイル: main.py プロジェクト: IMIO/django-fixmystreet
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_zips": 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))