def create(self, request): fingerprint = RequestFingerprint(request) if not fingerprint.is_duplicate(): fingerprint.save() if request.data.get("username", False): report = self.create_pro(request) else: report = self.create_citizen(request) if isinstance(report, HttpResponse): return report # Set source report.source = Report.SOURCES["MOBILE"] report.save() return report
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))
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) file_formset = ReportFileFormSet(request.POST, request.FILES, 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 comment.save() files = file_formset.save() for report_file in files: report_file.created_by = user 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') # reports_nearby = Report.objects.all().visible().unfinished() # reports_nearby = reports_nearby.near(pnt, 150).related_fields() 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, # "reports": Report.objects.all().visible(), "file_formset": file_formset, "comment_form": comment_form, # "reports_nearby": reports_nearby }, context_instance=RequestContext(request))