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 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 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 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 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 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)