Exemple #1
0
    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
Exemple #2
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
Exemple #3
0
def update(request, report_id):
    report = get_object_or_404(Report, id=report_id)

    if 'is_fixed' in request.REQUEST:
        form = MarkAsDoneForm(request.POST, instance=report)
        #Save the mark as done motivation in the database
        form.save()

        if "pro" in request.path:
            return HttpResponseRedirect(report.get_absolute_url_pro())
        else:
            return HttpResponseRedirect(report.get_absolute_url())

    # ????? DEPRECATED ?????
    if request.method == 'POST':
        if request.POST['form-type'] == u"comment-form":
            comment_form = ReportCommentForm(request.POST)
            if comment_form.is_valid():
                comment_form.save(request.user, report)

        if request.POST['form-type'] == u"file-form":
            #set default title if not given
            fileTitle = request.POST.get("title")
            if (fileTitle == ""):
                request.POST.__setitem__("title", request.FILES.get('file').name)
            file_form = ReportFileForm(request.POST, request.FILES)
            if file_form.is_valid:
                file_form.save(request.user, report)

        if "pro" in request.path:
            return HttpResponseRedirect(report.get_absolute_url_pro())
        else:
            return HttpResponseRedirect(report.get_absolute_url())
    raise Http404()
Exemple #4
0
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())
Exemple #5
0
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():
            # 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, _("You attachments has been sent"))
            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))
Exemple #6
0
def fixed(request, report_id):
    report = get_object_or_404(Report, id=report_id)

    comment_form = ReportCommentForm(request.POST)

    # TOFIX: Validate form
    if report.status != Report.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())
Exemple #7
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))
Exemple #8
0
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, _("You attachments has been sent"))
            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 = []

    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())
    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 = (
        report.is_in_progress and
        request.fmsuser.memberships.filter(organisation=report.responsible_department).exists() and
        (report.is_created() or report.is_in_progress()))

    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(),
        "transfer_form": TransferForm(),
        "mark_as_done_form": ReportCommentForm(),
        "priority_form": PriorityForm(instance=report),
        'activity_list': report.activities.all(),
        'attachment_edit': can_edit_attachment,
        "category_list": ReportMainCategoryClass.objects.all().order_by('name_' + get_language()),
    }, context_instance=RequestContext(request))
Exemple #9
0
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))