Esempio n. 1
0
def make_request(request, public_body=None):
    public_body_form = None
    if public_body is not None:
        public_body = get_object_or_404(PublicBody, slug=public_body)
        if not public_body.email:
            raise Http404
        all_laws = FoiLaw.objects.filter(jurisdiction=public_body.jurisdiction)
    else:
        all_laws = FoiLaw.objects.all()
        public_body_form = PublicBodyForm()
    initial = {
        "subject": request.GET.get("subject", ""),
        "reference": request.GET.get('ref', '')
    }
    if 'body' in request.GET:
        initial['body'] = request.GET['body']
    initial['jurisdiction'] = request.GET.get("jurisdiction", None)
    rq_form = RequestForm(all_laws,
                          FoiLaw.get_default_law(public_body),
                          True,
                          initial=initial)
    topic = request.GET.get("topic", "")
    user_form = None
    if not request.user.is_authenticated():
        user_form = NewUserForm()
    return render(
        request, 'foirequest/request.html', {
            "public_body": public_body,
            "public_body_form": public_body_form,
            "request_form": rq_form,
            "user_form": user_form,
            "topic": topic
        })
Esempio n. 2
0
def make_request(request, public_body=None, public_body_id=None):
    if not request.user.is_authenticated:
        messages.add_message(
            request, messages.ERROR,
            _('You are not currently logged in, please log in / sign up below to make a UIPA request.'
              ))
        return redirect("account-login")

    if public_body_id is not None:
        public_body = get_object_or_404(PublicBody, pk=int(public_body_id))
        url = reverse('foirequest-make_request',
                      kwargs={'public_body': public_body.slug})
        # Keep the query string for subject, body intact on redirect
        return redirect('%s?%s' % (url, request.META['QUERY_STRING']),
                        permanent=True)

    public_body_form = None
    if public_body is not None:
        public_body = get_object_or_404(PublicBody, slug=public_body)
        if not public_body.email:
            raise Http404
        all_laws = FoiLaw.objects.filter(jurisdiction=public_body.jurisdiction)
    else:
        all_laws = FoiLaw.objects.all()
        public_body_form = PublicBodyForm()
    initial = {
        "subject": request.GET.get("subject", ""),
        "reference": request.GET.get('ref', ''),
        "redirect_url": request.GET.get('redirect', '')
    }
    if 'body' in request.GET:
        initial['body'] = request.GET['body']
    initial['jurisdiction'] = request.GET.get("jurisdiction", None)
    public_body_search = request.GET.get("topic", "")
    initial['public_body_search'] = public_body_search

    default_law = FoiLaw.get_default_law(public_body)
    if default_law is None:
        messages.add_message(request, messages.INFO,
                             _('You need to setup a default FOI Law object'))
        return render(request, '500.html')

    rq_form = RequestForm(user=request.user,
                          list_of_laws=all_laws,
                          default_law=default_law,
                          initial=initial)
    user_form = None
    if not request.user.is_authenticated:
        user_form = NewUserForm()
    return render(
        request, 'foirequest/request.html', {
            "public_body": public_body,
            "public_body_form": public_body_form,
            "request_form": rq_form,
            "user_form": user_form,
            "public_body_search": public_body_search
        })
Esempio n. 3
0
def submit_request(request, public_body=None):
    error = False
    foilaw = None
    if public_body is not None:
        public_body = get_object_or_404(PublicBody, slug=public_body)
        if not public_body.email:
            raise Http404
        all_laws = FoiLaw.objects.filter(jurisdiction=public_body.jurisdiction)
    else:
        all_laws = FoiLaw.objects.all()
    context = {"public_body": public_body}

    request_form = RequestForm(all_laws, FoiLaw.get_default_law(), True, request.POST)
    context["request_form"] = request_form
    context["public_body_form"] = PublicBodyForm()
    if public_body is None and request.POST.get("public_body") == "new":
        pb_form = PublicBodyForm(request.POST)
        context["public_body_form"] = pb_form
        if pb_form.is_valid():
            data = pb_form.cleaned_data
            data["confirmed"] = False
            # Take the first jurisdiction there is
            data["jurisdiction"] = Jurisdiction.objects.all()[0]
            data["slug"] = slugify(data["name"])
            public_body = PublicBody(**data)
        else:
            error = True

    if not request_form.is_valid():
        error = True
    else:
        if (
            public_body is None
            and request_form.cleaned_data["public_body"] != ""
            and request_form.cleaned_data["public_body"] != "new"
        ):
            public_body = request_form.public_body_object

    context["user_form"] = None
    user = None
    if not request.user.is_authenticated():
        user_form = NewUserForm(request.POST)
        context["user_form"] = user_form
        if not user_form.is_valid():
            error = True
    else:
        user = request.user

    if not error:
        password = None
        if user is None:
            user, password = AccountManager.create_user(**user_form.cleaned_data)
        sent_to_pb = 1
        if public_body is not None and public_body.pk is None:
            public_body._created_by = user
            public_body.save()
            sent_to_pb = 2
        elif public_body is None:
            sent_to_pb = 0

        if foilaw is None:
            if public_body is not None:
                foilaw = public_body.default_law
            else:
                foilaw = request_form.foi_law

        foi_request = FoiRequest.from_request_form(
            user, public_body, foilaw, form_data=request_form.cleaned_data, post_data=request.POST
        )

        if user.is_active:
            if sent_to_pb == 0:
                messages.add_message(
                    request, messages.INFO, _("Others can now suggest the Public Bodies for your request.")
                )
            elif sent_to_pb == 2:
                messages.add_message(
                    request,
                    messages.INFO,
                    _(
                        "Your request will be sent as soon as the newly created Public Body was confirmed by an administrator."
                    ),
                )

            else:
                messages.add_message(request, messages.INFO, _("Your request has been sent."))
            return redirect(u"%s%s" % (foi_request.get_absolute_url(), _("?request-made")))
        else:
            AccountManager(user).send_confirmation_mail(request_id=foi_request.pk, password=password)
            messages.add_message(
                request, messages.INFO, _("Please check your inbox for mail from us to confirm your mail address.")
            )
            # user cannot access the request yet!
            return redirect("/")
    messages.add_message(
        request, messages.ERROR, _("There were errors in your form submission. Please review and submit again.")
    )
    return render(request, "foirequest/request.html", context, status=400)
Esempio n. 4
0
def submit_request(request, public_body=None):
    error = False
    foilaw = None
    if public_body is not None:
        public_body = get_object_or_404(PublicBody,
                slug=public_body)
        if not public_body.email:
            raise Http404
        all_laws = FoiLaw.objects.filter(jurisdiction=public_body.jurisdiction)
    else:
        all_laws = FoiLaw.objects.all()
    context = {"public_body": public_body}

    request_form = RequestForm(all_laws, FoiLaw.get_default_law(),
            True, request.POST)
    context['request_form'] = request_form
    context['public_body_form'] = PublicBodyForm()
    if (public_body is None and
            request.POST.get('public_body') == "new"):
        pb_form = PublicBodyForm(request.POST)
        context["public_body_form"] = pb_form
        if pb_form.is_valid():
            data = pb_form.cleaned_data
            data['confirmed'] = False
            # Take the first jurisdiction there is
            data['jurisdiction'] = Jurisdiction.objects.all()[0]
            data['slug'] = slugify(data['name'])
            public_body = PublicBody(**data)
        else:
            error = True

    if not request_form.is_valid():
        error = True
    else:
        if (public_body is None and
                request_form.cleaned_data['public_body'] != '' and
                request_form.cleaned_data['public_body'] != 'new'):
            public_body = request_form.public_body_object

    context['user_form'] = None
    user = None
    if not request.user.is_authenticated():
        user_form = NewUserForm(request.POST)
        context['user_form'] = user_form
        if not user_form.is_valid():
            error = True
    else:
        user = request.user

    if not error:
        password = None
        if user is None:
            user, password = AccountManager.create_user(**user_form.cleaned_data)
        sent_to_pb = 1
        if public_body is not None and public_body.pk is None:
            public_body._created_by = user
            public_body.save()
            sent_to_pb = 2
        elif public_body is None:
            sent_to_pb = 0

        if foilaw is None:
            if public_body is not None:
                foilaw = public_body.default_law
            else:
                foilaw = request_form.foi_law

        foi_request = FoiRequest.from_request_form(
                user,
                public_body,
                foilaw,
                form_data=request_form.cleaned_data,
                post_data=request.POST
        )

        if user.is_active:
            if sent_to_pb == 0:
                messages.add_message(request, messages.INFO,
                    _('Others can now suggest the Public Bodies for your request.'))
            elif sent_to_pb == 2:
                messages.add_message(request, messages.INFO,
                    _('Your request will be sent as soon as the newly created Public Body was confirmed by an administrator.'))

            else:
                messages.add_message(request, messages.INFO,
                    _('Your request has been sent.'))
            return redirect(u'%s%s' % (foi_request.get_absolute_url(), _('?request-made')))
        else:
            AccountManager(user).send_confirmation_mail(request_id=foi_request.pk,
                    password=password)
            messages.add_message(request, messages.INFO,
                    _('Please check your inbox for mail from us to confirm your mail address.'))
            # user cannot access the request yet!
            return redirect("/")
    messages.add_message(request, messages.ERROR,
        _('There were errors in your form submission. Please review and submit again.'))
    return render(request, 'foirequest/request.html', context, status=400)
Esempio n. 5
0
def make_request(request, publicbody_slug=None, publicbody_ids=None):
    publicbody_form = None
    publicbodies = []
    if publicbody_ids is not None:
        publicbody_ids = publicbody_ids.split('+')
        publicbodies = PublicBody.objects.filter(pk__in=publicbody_ids)
        if len(publicbody_ids) != len(publicbodies):
            raise Http404
    elif publicbody_slug is not None:
        publicbody = get_object_or_404(PublicBody, slug=publicbody_slug)
        if not publicbody.email:
            raise Http404
        publicbodies = [publicbody]
    else:
        publicbody_form = PublicBodyForm()

    user_form = None

    if request.method == 'POST':
        error = False

        request_form = RequestForm(request.POST, user=request.user)

        throttle_message = check_throttle(request.user, FoiRequest)
        if throttle_message:
            request_form.add_error(None, throttle_message)

        if not request_form.is_valid():
            error = True

        config = {
            k: request_form.cleaned_data.get(k, False)
            for k in ('hide_similar', 'hide_public')
        }

        if publicbody_form:
            publicbody_form = PublicBodyForm(request.POST)
            if not publicbody_form.is_valid():
                error = True

        if request.user.is_authenticated and request.POST.get(
                'save_draft', ''):
            # Save as draft
            if publicbody_form:
                publicbodies = publicbody_form.get_publicbodies()

            service = SaveDraftService({
                'publicbodies': publicbodies,
                'request_form': request_form
            })
            service.execute(request)
            messages.add_message(
                request, messages.INFO,
                _('Your request has been saved to your drafts.'))

            return redirect('account-drafts')

        if not request.user.is_authenticated:
            user_form = NewUserForm(request.POST)
            if not user_form.is_valid():
                error = True

        if not error:
            data = dict(request_form.cleaned_data)
            data['user'] = request.user

            if publicbody_form:
                data['publicbodies'] = [
                    publicbody_form.cleaned_data['publicbody']
                ]
            else:
                data['publicbodies'] = publicbodies

            if not request.user.is_authenticated:
                data.update(user_form.cleaned_data)

            service = CreateRequestService(data)
            foirequest = service.execute(request)

            special_redirect = request_form.cleaned_data['redirect_url']

            if request.user.is_authenticated:
                messages.add_message(request, messages.INFO,
                                     _('Your request has been sent.'))
                req_url = '%s%s' % (foirequest.get_absolute_url(),
                                    _('?request-made'))
                return redirect(special_redirect or req_url)
            else:
                messages.add_message(
                    request, messages.INFO,
                    _('Please check your inbox for mail from us to '
                      'confirm your mail address.'))
                # user cannot access the request yet,
                # redirect to custom URL or homepage
                return redirect(special_redirect or '/')

        status_code = 400
        messages.add_message(
            request, messages.ERROR,
            _('There were errors in your form submission. '
              'Please review and submit again.'))
    else:
        status_code = 200
        initial = {
            "subject": request.GET.get('subject', ''),
            "reference": request.GET.get('ref', ''),
            "redirect_url": request.GET.get('redirect', '')
        }
        if 'body' in request.GET:
            initial['body'] = request.GET['body']

        if 'draft' in request.GET:
            initial['draft'] = request.GET['draft']

        config = {}
        for key in ('hide_similar', 'hide_public', 'hide_draft'):
            if key in request.GET:
                initial[key] = True
            config[key] = initial.get(key, False)

        if initial.get('hide_public'):
            initial['public'] = True
        if 'public' in request.GET:
            initial['public'] = request.GET['public'] == '1'

        if 'full_text' in request.GET:
            initial['full_text'] = request.GET['full_text'] == '1'

        initial['jurisdiction'] = request.GET.get("jurisdiction", None)

        request_form = RequestForm(initial=initial, user=request.user)
        if not request.user.is_authenticated:
            initial_user_data = {}
            if 'email' in request.GET:
                initial_user_data['user_email'] = request.GET['email']
            if 'first_name' in request.GET:
                initial_user_data['first_name'] = request.GET['first_name']
            if 'last_name' in request.GET:
                initial_user_data['last_name'] = request.GET['last_name']

            user_form = NewUserForm(initial=initial_user_data)

    publicbodies_json = ''
    if publicbodies:
        publicbodies_json = json.dumps([pb.as_data() for pb in publicbodies])

    return render(request,
                  'foirequest/request.html', {
                      'publicbody_form': publicbody_form,
                      'publicbodies': publicbodies,
                      'publicbodies_json': publicbodies_json,
                      'multi_request': len(publicbodies) > 1,
                      'request_form': request_form,
                      'user_form': user_form,
                      'config': config,
                      'public_body_search': request.GET.get('topic', '')
                  },
                  status=status_code)
Esempio n. 6
0
def make_request(request, public_body=None, public_body_id=None):
    if public_body_id is not None:
        public_body = get_object_or_404(PublicBody, pk=int(public_body_id))
        url = reverse('foirequest-make_request',
                      kwargs={'public_body': public_body.slug})
        # Keep the query string for subject, body intact on redirect
        return redirect('%s?%s' % (url, request.META['QUERY_STRING']),
                        permanent=True)

    public_body_form = None
    if public_body is not None:
        public_body = get_object_or_404(PublicBody, slug=public_body)
        if not public_body.email:
            raise Http404
        all_laws = FoiLaw.objects.filter(jurisdiction=public_body.jurisdiction)
    else:
        all_laws = FoiLaw.objects.all()
        public_body_form = PublicBodyForm()
    initial = {
        "subject": request.GET.get('subject', ''),
        "reference": request.GET.get('ref', ''),
        "redirect_url": request.GET.get('redirect', '')
    }
    if 'body' in request.GET:
        initial['body'] = request.GET['body']

    if 'hide_public' in request.GET:
        initial['hide_public'] = True
        initial['public'] = False

    if 'hide_similar' in request.GET:
        initial['hide_similar'] = True

    initial['jurisdiction'] = request.GET.get("jurisdiction", None)
    public_body_search = request.GET.get("topic", "")
    initial['public_body_search'] = public_body_search

    default_law = FoiLaw.get_default_law(public_body)
    if default_law is None:
        messages.add_message(request, messages.INFO,
                             _('You need to setup a default FOI Law object'))
        return render(request, '500.html')

    rq_form = RequestForm(user=request.user,
                          list_of_laws=all_laws,
                          default_law=default_law,
                          initial=initial)

    user_form = None
    if not request.user.is_authenticated:
        initial_user_data = {}
        if 'email' in request.GET:
            initial_user_data['user_email'] = request.GET['email']
        if 'first_name' in request.GET:
            initial_user_data['first_name'] = request.GET['first_name']
        if 'last_name' in request.GET:
            initial_user_data['last_name'] = request.GET['last_name']
        user_form = NewUserForm(initial=initial_user_data)

    return render(
        request, 'foirequest/request.html', {
            "public_body": public_body,
            "public_body_form": public_body_form,
            "request_form": rq_form,
            "user_form": user_form,
            "public_body_search": public_body_search
        })
Esempio n. 7
0
def submit_request(request, public_body=None):
    error = False
    foi_law = None
    if public_body is not None:
        public_body = get_object_or_404(PublicBody,
                slug=public_body)
        if not public_body.email:
            raise Http404
        all_laws = FoiLaw.objects.filter(jurisdiction=public_body.jurisdiction)
    else:
        all_laws = FoiLaw.objects.all()
    context = {"public_body": public_body}

    request_form = RequestForm(user=request.user,
                               list_of_laws=all_laws,
                               default_law=FoiLaw.get_default_law(),
                               data=request.POST)
    context['request_form'] = request_form
    context['public_body_form'] = PublicBodyForm()
    if (public_body is None and
            request.POST.get('public_body') == "new"):
        pb_form = PublicBodyForm(request.POST)
        context["public_body_form"] = pb_form
        if pb_form.is_valid():
            data = pb_form.cleaned_data
            data['confirmed'] = False
            # Take the first jurisdiction there is
            data['jurisdiction'] = Jurisdiction.objects.all()[0]
            data['slug'] = slugify(data['name'])
            public_body = PublicBody(**data)
        else:
            error = True

    if not request_form.is_valid():
        error = True
    else:
        if (public_body is None and
                request_form.cleaned_data['public_body'] != '' and
                request_form.cleaned_data['public_body'] != 'new'):
            public_body = request_form.public_body_object

    context['user_form'] = None
    user = None
    if not request.user.is_authenticated:
        user_form = NewUserForm(request.POST)
        context['user_form'] = user_form
        if not user_form.is_valid():
            error = True
    else:
        user = request.user

    if error:
        messages.add_message(request, messages.ERROR,
            _('There were errors in your form submission. Please review and submit again.'))
        return render(request, 'foirequest/request.html', context, status=400)

    password = None
    if user is None:
        user, password = AccountManager.create_user(**user_form.cleaned_data)
    sent_to_pb = 1
    if public_body is not None and public_body.pk is None:
        public_body._created_by = user
        public_body.save()
        sent_to_pb = 2
    elif public_body is None:
        sent_to_pb = 0

    if foi_law is None:
        if public_body is not None:
            foi_law = public_body.default_law
        else:
            foi_law = request_form.foi_law

    kwargs = registry.run_hook('pre_request_creation', request,
        user=user,
        public_body=public_body,
        foi_law=foi_law,
        form_data=request_form.cleaned_data,
        post_data=request.POST
    )
    foi_request = FoiRequest.from_request_form(**kwargs)

    if user.is_active:
        if sent_to_pb == 0:
            messages.add_message(request, messages.INFO,
                _('Others can now suggest the Public Bodies for your request.'))
        elif sent_to_pb == 2:
            messages.add_message(request, messages.INFO,
                _('Your request will be sent as soon as the newly created Public Body was confirmed by an administrator.'))

        else:
            messages.add_message(request, messages.INFO,
                _('Your request has been sent.'))
        if request_form.cleaned_data['redirect_url']:
            redirect_url = request_form.cleaned_data['redirect_url']
            if is_safe_url(redirect_url, allowed_hosts=settings.ALLOWED_REDIRECT_HOSTS):
                return redirect(redirect_url)
        return redirect(u'%s%s' % (foi_request.get_absolute_url(), _('?request-made')))
    else:
        AccountManager(user).send_confirmation_mail(request_id=foi_request.pk,
                password=password)
        messages.add_message(request, messages.INFO,
                _('Please check your inbox for mail from us to confirm your mail address.'))
        # user cannot access the request yet!
        return redirect("/")