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
def create_pro(request): # Login the user user = authenticate(username=request.POST.get('username'), password=request.POST.get('password')) if user and user.is_active: login(request, user) else: return HttpResponseForbidden('invalid username or password') # Create report report_form = ProReportForm(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.private = True report.save() report.subscribe_author() # 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.report = report comment.is_new_report = True comment.save() return report
def refuse(request, report_id): report = get_object_or_404(Report, id=report_id) comment_form = ReportCommentForm(request.POST) # TOFIX: Validate form if comment_form.is_valid() and request.POST.get('text'): comment = comment_form.save(commit=False) # Save refusal motivation comment.report = report # If the report is public, make the refusal motivation public too if not report.private: comment.security_level = ReportComment.PUBLIC comment.type = ReportAttachment.REFUSED comment.save() #Update the status of report report.status = Report.REFUSED report.save() #Redirect to the report show page if "pro" in request.path: return HttpResponseRedirect(report.get_absolute_url_pro()) else: return HttpResponseRedirect(report.get_absolute_url())
def create_pro(request, several_occurences=False, mobile_notification=False): # Login the user user = authenticate(username=request.POST.get('username'), password=request.POST.get('password')) if user and user.is_active: login(request, user) else: return HttpResponseForbidden('invalid username or password') # Create report report_form = ProReportForm(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.private = True report.several_occurences = several_occurences report.mobile_notification = mobile_notification report.save() report.subscribe_author() # 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.report = report comment.is_new_report = True comment.save() return report
def document(request, slug, report_id): ReportFileFormSet = inlineformset_factory(Report, ReportFile, form=ReportFileForm, extra=0) report = get_object_or_404(Report, id=report_id) if request.method == "POST": comment = None comment_form = ReportCommentForm(request.POST, request.FILES, prefix='comment') citizen_form = CitizenForm(request.POST, request.FILES, prefix='citizen') # 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()) # this checks update is_valid too if file_formset.is_valid() and ( not request.POST["comment-text"] or comment_form.is_valid()) and citizen_form.is_valid(): if len(file_formset.files) == 0 and (not request.POST["comment-text"] or len(request.POST["comment-text"]) == 0): messages.add_message(request, messages.ERROR, _("You must add a comment, a photo or a document to continue.")) else: # this saves the update as part of the report. citizen = citizen_form.save() if request.POST["comment-text"] and len(request.POST["comment-text"]) > 0: comment = comment_form.save(commit=False) comment.report = report comment.created_by = citizen comment.save() files = file_formset.save() for report_file in files: report_file.created_by = citizen report_file.save() # if request.POST.get("citizen_subscription", False): # ReportSubscription(report=report, subscriber=report.created_by).save() messages.add_message(request, messages.SUCCESS, _("Modification(s) registered")) return HttpResponseRedirect(report.get_absolute_url()) else: file_formset = ReportFileFormSet(prefix='files', queryset=ReportFile.objects.none()) comment_form = ReportCommentForm(prefix='comment') citizen_form = CitizenForm(prefix='citizen') return render_to_response("reports/document.html", { "report": report, "subscribed": request.user.is_authenticated() and ReportSubscription.objects.filter(report=report, subscriber=request.user).exists(), "file_formset": file_formset, "comment_form": comment_form, "citizen_form": citizen_form, }, context_instance=RequestContext(request))
def change_flag_and_add_comment(request, report_id): if request.fmsuser.is_pro(): report = get_object_or_404(Report, id=report_id) comment_form = ReportCommentForm(request.POST) hidden = None event_type = None #if you add a new flag using divAddComment in _actions.html, don't forget to retrieve the correct name # of the hidden field here. if request.POST.get("hiddenThirdPartyResponsibility"): hidden = request.POST.get("hiddenThirdPartyResponsibility") if hidden == 'true': #source of incident is due to a third party report.third_party_responsibility = True else: #source of incident is not a third party report.third_party_responsibility = False report.save() event_type = ReportEventLog.THIRD_PARTY_RESPONSIBILITY elif request.POST.get("hiddenPrivateProperty"): hidden = request.POST.get("hiddenPrivateProperty") if hidden == 'true': #the source of the incident is not on a private property report.private_property = True else: #the source of the incident is on a private property report.private_property = False report.save() event_type = ReportEventLog.PRIVATE_PROPERTY if event_type is not None: event = ReportEventLog( report=report, event_type=event_type, user=get_current_user(), text= hidden, #saving the new value of the flag in case we ever need it. ) event.save() # TOFIX: Validate form if comment_form.is_valid() and request.POST.get( 'text') and hidden is not None: comment = comment_form.save(commit=False) comment.report = report comment.save() return HttpResponseRedirect(report.get_absolute_url_pro()) raise PermissionDenied()
def change_flag_and_add_comment(request, report_id): if request.fmsuser.is_pro(): report = get_object_or_404(Report, id=report_id) comment_form = ReportCommentForm(request.POST) hidden = None event_type = None #if you add a new flag using divAddComment in _actions.html, don't forget to retrieve the correct name # of the hidden field here. if request.POST.get("hiddenThirdPartyResponsibility"): hidden = request.POST.get("hiddenThirdPartyResponsibility") if hidden == 'true': #source of incident is due to a third party report.third_party_responsibility = True else: #source of incident is not a third party report.third_party_responsibility = False report.save() event_type = ReportEventLog.THIRD_PARTY_RESPONSIBILITY elif request.POST.get("hiddenPrivateProperty"): hidden = request.POST.get("hiddenPrivateProperty") if hidden == 'true': #the source of the incident is not on a private property report.private_property = True else: #the source of the incident is on a private property report.private_property = False report.save() event_type = ReportEventLog.PRIVATE_PROPERTY if event_type is not None: event = ReportEventLog( report=report, event_type=event_type, user=get_current_user(), text=hidden, #saving the new value of the flag in case we ever need it. ) event.save() # TOFIX: Validate form if comment_form.is_valid() and request.POST.get('text') and hidden is not None: comment = comment_form.save(commit=False) comment.report = report comment.save() return HttpResponseRedirect(report.get_absolute_url_pro()) raise PermissionDenied()
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
def fixed(request, report_id): report = get_object_or_404(Report, id=report_id) comment_form = ReportCommentForm(request.POST) # TOFIX: Validate form if report.is_markable_as_solved() and comment_form.is_valid() and request.POST.get('text'): comment = comment_form.save(commit=False) # Save refusal motivation comment.report = report comment.type = ReportAttachment.MARK_AS_DONE comment.save() #Update the status of report report.status = Report.SOLVED report.fixed_at = datetime.now() report.save() return HttpResponseRedirect(report.get_absolute_url_pro())
def fixed(request, report_id): report = get_object_or_404(Report, id=report_id) comment_form = ReportCommentForm(request.POST) # TOFIX: Validate form if report.is_markable_as_solved() and comment_form.is_valid( ) and request.POST.get('text'): comment = comment_form.save(commit=False) # Save refusal motivation comment.report = report comment.type = ReportAttachment.MARK_AS_DONE comment.save() #Update the status of report report.status = Report.SOLVED report.fixed_at = datetime.now() report.save() return HttpResponseRedirect(report.get_absolute_url_pro())
def show(request, slug, report_id): ReportFileFormSet = inlineformset_factory(Report, ReportFile, form=ReportFileForm, extra=0) report = get_object_or_404(Report, id=report_id) if request.method == "POST": file_formset = ReportFileFormSet(request.POST, hack_multi_file(request), instance=report, prefix='files', queryset=ReportFile.objects.none()) comment_form = ReportCommentForm(request.POST, request.FILES, prefix='comment') comment = None # this checks update is_valid too if file_formset.is_valid() and (not request.POST["comment-text"] or comment_form.is_valid()): # this saves the update as part of the report. user = FMSUser.objects.get(pk=request.user.id) if request.POST["comment-text"] and len( request.POST["comment-text"]) > 0: comment = comment_form.save(commit=False) comment.created_by = user comment.report = report 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, _("Modification(s) registered")) return HttpResponseRedirect(report.get_absolute_url_pro()) else: file_formset = ReportFileFormSet(prefix='files', queryset=ReportFile.objects.none()) comment_form = ReportCommentForm(prefix='comment') organisation = request.fmsuser.organisation managers = FMSUser.objects.filter(organisation=organisation).filter( manager=True).order_by('name_' + get_language()) region_institution = OrganisationEntity.objects.filter( type=OrganisationEntity.REGION).filter( active=True).order_by('name_' + get_language()) entities = OrganisationEntity.objects.filter( type=OrganisationEntity.COMMUNE).filter( active=True).order_by('name_' + get_language()) departments = [] contractors = [] contact_list = [] if organisation: entities.exclude(pk=organisation.id) departments = organisation.associates.all().filter( type=OrganisationEntity.DEPARTMENT).order_by('name_' + get_language()) contractors = organisation.associates.filter( type=OrganisationEntity.SUBCONTRACTOR).order_by('name_' + get_language()) contact_list = organisation.users.filter(is_active=True) else: contractors = OrganisationEntity.objects.filter( type=OrganisationEntity.SUBCONTRACTOR).order_by('name_' + get_language()) applicants = OrganisationEntity.objects.filter( type=OrganisationEntity.APPLICANT).order_by('name_' + get_language()) can_edit_attachment = request.fmsuser.memberships.filter(organisation=report.responsible_department).exists() \ and not report.is_merged() from_verification = False if 'HTTP_REFERER' in request.META: if "/rapport/verification" in request.META['HTTP_REFERER']: from_verification = True return render_to_response("pro/reports/show.html", { "fms_user": request.fmsuser, "report": report, "subscribed": request.user.is_authenticated() and ReportSubscription.objects.filter( report=report, subscriber=request.user).exists(), "comment_form": comment_form, "file_formset": file_formset, "region_institution": region_institution, "managers": managers, "departments": departments, "contractors": contractors, "applicants": applicants, "entities": entities, "refuse_form": ReportCommentForm(), "add_comment_form": ReportCommentForm(), "transfer_form": TransferForm(), "mark_as_done_form": ReportCommentForm(), "priority_form": PriorityForm(instance=report), "activity_list": report.activities.all().order_by('-event_at'), "attachment_edit": can_edit_attachment, "category_list": ReportMainCategoryClass.objects.all().order_by('name_' + get_language()), "contact_list": contact_list, "from_verification": from_verification, }, 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) # 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))
def show(request, slug, report_id): ReportFileFormSet = inlineformset_factory(Report, ReportFile, form=ReportFileForm, extra=0) report = get_object_or_404(Report, id=report_id) if request.method == "POST": file_formset = ReportFileFormSet(request.POST, hack_multi_file(request), instance=report, prefix='files', queryset=ReportFile.objects.none()) comment_form = ReportCommentForm(request.POST, request.FILES, prefix='comment') comment = None # this checks update is_valid too if file_formset.is_valid() and (not request.POST["comment-text"] or comment_form.is_valid()): # this saves the update as part of the report. user = FMSUser.objects.get(pk=request.user.id) if request.POST["comment-text"] and len(request.POST["comment-text"]) > 0: comment = comment_form.save(commit=False) comment.created_by = user comment.report = report 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, _("Modification(s) registered")) return HttpResponseRedirect(report.get_absolute_url_pro()) else: file_formset = ReportFileFormSet(prefix='files', queryset=ReportFile.objects.none()) comment_form = ReportCommentForm(prefix='comment') organisation = request.fmsuser.organisation managers = FMSUser.objects.filter(organisation=organisation).filter(manager=True).order_by('name_' + get_language()) region_institution = OrganisationEntity.objects.filter(type=OrganisationEntity.REGION).filter(active=True).order_by('name_' + get_language()) entities = OrganisationEntity.objects.filter(type=OrganisationEntity.COMMUNE).filter(active=True).order_by('name_' + get_language()) departments = [] contractors = [] contact_list = [] if organisation: entities.exclude(pk=organisation.id) departments = organisation.associates.all().filter(type=OrganisationEntity.DEPARTMENT).order_by('name_' + get_language()) contractors = organisation.associates.filter(type=OrganisationEntity.SUBCONTRACTOR).order_by('name_' + get_language()) contact_list = organisation.users.filter(is_active=True) else: contractors = OrganisationEntity.objects.filter(type=OrganisationEntity.SUBCONTRACTOR).order_by('name_' + get_language()) applicants = OrganisationEntity.objects.filter(type=OrganisationEntity.APPLICANT).order_by('name_' + get_language()) can_edit_attachment = request.fmsuser.memberships.filter(organisation=report.responsible_department).exists() \ and not report.is_merged() return render_to_response("pro/reports/show.html", { "fms_user": request.fmsuser, "report": report, "subscribed": request.user.is_authenticated() and ReportSubscription.objects.filter(report=report, subscriber=request.user).exists(), "comment_form": comment_form, "file_formset": file_formset, "region_institution": region_institution, "managers": managers, "departments": departments, "contractors": contractors, "applicants": applicants, "entities": entities, "refuse_form": ReportCommentForm(), "add_comment_form": ReportCommentForm(), "transfer_form": TransferForm(), "mark_as_done_form": ReportCommentForm(), "priority_form": PriorityForm(instance=report), "activity_list": report.activities.all().order_by('-event_at'), "attachment_edit": can_edit_attachment, "category_list": ReportMainCategoryClass.objects.all().order_by('name_' + get_language()), "contact_list": contact_list, }, 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) # 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))
def add_attachment_for_user(request, report, user): ReportFileFormSet = inlineformset_factory(Report, ReportFile, form=ReportFileForm, extra=0) comment_form = ReportCommentForm(request.POST, request.FILES, prefix='comment') file_formset = ReportFileFormSet(request.POST, hack_multi_file(request), instance=report, prefix='files', queryset=ReportFile.objects.none()) is_incident_creation_form = IsIncidentCreationForm(request.POST) if is_incident_creation_form.is_valid(): is_incident_creation = is_incident_creation_form.cleaned_data['is_incident_creation'] else: is_incident_creation = False if 'rotation' in request.POST and request.POST['rotation'] in ['90', '180', '270']: image_rotation = int(request.POST['rotation']) else: image_rotation = 0 response = get_response() try: if comment_form.is_valid(): comment = comment_form.save(commit=False) comment.created_by = user comment.report = report comment.is_incident_creation = is_incident_creation comment.save() attachment = comment elif file_formset.is_valid(): if len(file_formset.files) > 0: file_formset.form.is_incident_creation = is_incident_creation files = file_formset.save() for report_file in files: report_file.created_by = user report_file.is_incident_creation = is_incident_creation report_file.save(refresh_report=True) attachment = report_file if report_file.file_type == ReportFile.IMAGE and image_rotation > 0: src_im = Image.open(report_file.image.path) im = src_im.rotate(0 - image_rotation, expand=True) im.save(report_file.image.path) src_im = Image.open(report_file.image.thumbnail.path) im = src_im.rotate(0 - image_rotation, expand=True) im.save(report_file.image.thumbnail.path) else: return exit_with_error("Attachment is not valid (no file received)", 400) else: return exit_with_error("Attachment is not valid (file_formset is not valid) : " + + ", ".join(comment_form.errors) + ", ".join(file_formset.errors), 400) except Exception as e: return exit_with_error("Attachment is not valid (exception): " + str(e), 400) res = { "id": attachment.id, "created": attachment.created.strftime('%d/%m/%Y') } if attachment.created_by is None or attachment.created_by.is_citizen(): res["created_by"] = { "en": get_translated_value("A citizen", "fr", True), "fr": get_translated_value("A citizen", "fr", True), "nl": get_translated_value("A citizen", "nl", True) } else: res["created_by"] = { "en": get_translated_value(attachment.get_display_name_as_citizen, "fr").name, "fr": get_translated_value(attachment.get_display_name_as_citizen, "fr").name, "nl": get_translated_value(attachment.get_display_name_as_citizen, "nl").name, } response['response'] = res return return_response(response)
def document(request, slug, report_id): ReportFileFormSet = inlineformset_factory(Report, ReportFile, form=ReportFileForm, extra=0) report = get_object_or_404(Report, id=report_id) if request.method == "POST": comment = None comment_form = ReportCommentForm(request.POST, request.FILES, prefix='comment') # 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()) # this checks update is_valid too if file_formset.is_valid() and (not request.POST["comment-text"] or comment_form.is_valid()): if len(file_formset.files) == 0 and ( not request.POST["comment-text"] or len(request.POST["comment-text"]) == 0): messages.add_message( request, messages.ERROR, _("You must add a comment, a photo or a document to continue." )) else: # this saves the update as part of the report. user = FMSUser.objects.get(pk=request.user.id) if request.POST["comment-text"] and len( request.POST["comment-text"]) > 0: 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, _("Modification(s) registered")) return HttpResponseRedirect(report.get_absolute_url_pro()) else: file_formset = ReportFileFormSet(prefix='files', queryset=ReportFile.objects.none()) comment_form = ReportCommentForm(prefix='comment') return render_to_response("reports/document.html", { "report": report, "subscribed": request.user.is_authenticated() and ReportSubscription.objects.filter( report=report, subscriber=request.user).exists(), "file_formset": file_formset, "comment_form": comment_form, }, context_instance=RequestContext(request))
def add_attachment_for_user(request, report, user): ReportFileFormSet = inlineformset_factory(Report, ReportFile, form=ReportFileForm, extra=0) comment_form = ReportCommentForm(request.POST, request.FILES, prefix='comment') file_formset = ReportFileFormSet(request.POST, hack_multi_file(request), instance=report, prefix='files', queryset=ReportFile.objects.none()) is_incident_creation_form = IsIncidentCreationForm(request.POST) if is_incident_creation_form.is_valid(): is_incident_creation = is_incident_creation_form.cleaned_data[ 'is_incident_creation'] else: is_incident_creation = False if 'rotation' in request.POST and request.POST['rotation'] in [ '90', '180', '270' ]: image_rotation = int(request.POST['rotation']) else: image_rotation = 0 response = get_response() try: if comment_form.is_valid(): comment = comment_form.save(commit=False) comment.created_by = user comment.report = report comment.is_incident_creation = is_incident_creation comment.save() attachment = comment elif file_formset.is_valid(): if len(file_formset.files) > 0: file_formset.form.is_incident_creation = is_incident_creation files = file_formset.save() for report_file in files: report_file.created_by = user report_file.is_incident_creation = is_incident_creation report_file.save(refresh_report=True) attachment = report_file if report_file.file_type == ReportFile.IMAGE and image_rotation > 0: src_im = Image.open(report_file.image.path) im = src_im.rotate(0 - image_rotation, expand=True) im.save(report_file.image.path) src_im = Image.open(report_file.image.thumbnail.path) im = src_im.rotate(0 - image_rotation, expand=True) im.save(report_file.image.thumbnail.path) else: return exit_with_error( "Attachment is not valid (no file received)", 400) else: return exit_with_error( "Attachment is not valid (file_formset is not valid) : " + + ", ".join(comment_form.errors) + ", ".join(file_formset.errors), 400) except Exception as e: return exit_with_error( "Attachment is not valid (exception): " + str(e), 400) res = { "id": attachment.id, "created": attachment.created.strftime('%d/%m/%Y') } if attachment.created_by is None or attachment.created_by.is_citizen(): res["created_by"] = { "en": get_translated_value("A citizen", "fr", True), "fr": get_translated_value("A citizen", "fr", True), "nl": get_translated_value("A citizen", "nl", True) } else: res["created_by"] = { "en": get_translated_value(attachment.get_display_name_as_citizen, "fr").name, "fr": get_translated_value(attachment.get_display_name_as_citizen, "fr").name, "nl": get_translated_value(attachment.get_display_name_as_citizen, "nl").name, } response['response'] = res return return_response(response)