Skip to content

Views

hypha.apply.utils.views

ViewDispatcher

Bases: View

admin_view class-attribute instance-attribute

admin_view = None

reviewer_view class-attribute instance-attribute

reviewer_view = None

partner_view class-attribute instance-attribute

partner_view = None

community_view class-attribute instance-attribute

community_view = None

applicant_view class-attribute instance-attribute

applicant_view = None

finance_view class-attribute instance-attribute

finance_view = None

contracting_view class-attribute instance-attribute

contracting_view = None

admin_check

admin_check(request)
Source code in hypha/apply/utils/views.py
def admin_check(self, request):
    return request.user.is_apply_staff

reviewer_check

reviewer_check(request)
Source code in hypha/apply/utils/views.py
def reviewer_check(self, request):
    return request.user.is_reviewer

partner_check

partner_check(request)
Source code in hypha/apply/utils/views.py
def partner_check(self, request):
    return request.user.is_partner

community_check

community_check(request)
Source code in hypha/apply/utils/views.py
def community_check(self, request):
    return request.user.is_community_reviewer

finance_check

finance_check(request)
Source code in hypha/apply/utils/views.py
def finance_check(self, request):
    return request.user.is_finance

contracting_check

contracting_check(request)
Source code in hypha/apply/utils/views.py
def contracting_check(self, request):
    return request.user.is_contracting

applicant_check

applicant_check(request)
Source code in hypha/apply/utils/views.py
def applicant_check(self, request):
    return request.user.is_applicant

dispatch

dispatch(request, *args, **kwargs)
Source code in hypha/apply/utils/views.py
def dispatch(self, request, *args, **kwargs):
    view = None

    if self.admin_check(request):
        view = self.admin_view
    elif self.reviewer_check(request):
        view = self.reviewer_view
    elif self.partner_check(request):
        view = self.partner_view
    elif self.community_check(request):
        view = self.community_view
    elif settings.PROJECTS_ENABLED and self.finance_check(request):
        view = self.finance_view
    elif settings.PROJECTS_ENABLED and self.contracting_check(request):
        view = self.contracting_view
    elif self.applicant_check(request):
        view = self.applicant_view

    if view:
        return view.as_view()(request, *args, **kwargs)
    return HttpResponseForbidden()

DelegatableBase

DelegatableBase(*args, **kwargs)

Bases: ContextMixin

A view which passes its context to child form views to allow them to post to the same URL DelegateableViews objects should contain form views that inherit from DelegatedViewMixin and FormView

Source code in hypha/apply/utils/views.py
def __init__(self, *args, **kwargs):
    self._form_views = {
        self.form_prefix + form_view.context_name: form_view
        for form_view in self.form_views
    }

form_prefix class-attribute instance-attribute

form_prefix = 'form-submitted-'

get_form_kwargs

get_form_kwargs()
Source code in hypha/apply/utils/views.py
def get_form_kwargs(self):
    return {}

get_context_data

get_context_data(**kwargs)
Source code in hypha/apply/utils/views.py
def get_context_data(self, **kwargs):
    forms = {}
    for form_view in self._form_views.values():
        view = form_view()
        view.setup(self.request, self.args, self.kwargs)
        context_key, form = view.contribute_form(self)
        forms[context_key] = form

    return super().get_context_data(
        form_prefix=self.form_prefix,
        **forms,
        **kwargs,
    )

post

post(request, *args, **kwargs)
Source code in hypha/apply/utils/views.py
def post(self, request, *args, **kwargs):
    # Information to pretend we originate from this view
    kwargs["context"] = self.get_context_data()
    kwargs["template_names"] = self.get_template_names()

    for form_key, form_view in self._form_views.items():
        if form_key in request.POST:
            return form_view.as_view()(request, *args, parent=self, **kwargs)

    # Fall back to get if not form exists as submitted
    return redirect(request.path)

DelegateableView

DelegateableView(*args, **kwargs)

Bases: DelegatableBase

Source code in hypha/apply/utils/views.py
def __init__(self, *args, **kwargs):
    self._form_views = {
        self.form_prefix + form_view.context_name: form_view
        for form_view in self.form_views
    }

form_prefix class-attribute instance-attribute

form_prefix = 'form-submitted-'

get_context_data

get_context_data(**kwargs)
Source code in hypha/apply/utils/views.py
def get_context_data(self, **kwargs):
    forms = {}
    for form_view in self._form_views.values():
        view = form_view()
        view.setup(self.request, self.args, self.kwargs)
        context_key, form = view.contribute_form(self)
        forms[context_key] = form

    return super().get_context_data(
        form_prefix=self.form_prefix,
        **forms,
        **kwargs,
    )

get_form_kwargs

get_form_kwargs()
Source code in hypha/apply/utils/views.py
def get_form_kwargs(self):
    return {
        "user": self.request.user,
        "instance": self.object,
    }

post

post(request, *args, **kwargs)
Source code in hypha/apply/utils/views.py
def post(self, request, *args, **kwargs):
    self.object = self.get_object()

    kwargs["object"] = self.object

    return super().post(request, *args, **kwargs)

DelegateableListView

DelegateableListView(*args, **kwargs)

Bases: DelegatableBase

Source code in hypha/apply/utils/views.py
def __init__(self, *args, **kwargs):
    self._form_views = {
        self.form_prefix + form_view.context_name: form_view
        for form_view in self.form_views
    }

form_prefix class-attribute instance-attribute

form_prefix = 'form-submitted-'

get_context_data

get_context_data(**kwargs)
Source code in hypha/apply/utils/views.py
def get_context_data(self, **kwargs):
    forms = {}
    for form_view in self._form_views.values():
        view = form_view()
        view.setup(self.request, self.args, self.kwargs)
        context_key, form = view.contribute_form(self)
        forms[context_key] = form

    return super().get_context_data(
        form_prefix=self.form_prefix,
        **forms,
        **kwargs,
    )

get_form_kwargs

get_form_kwargs()
Source code in hypha/apply/utils/views.py
def get_form_kwargs(self):
    return {
        "user": self.request.user,
    }

post

post(request, *args, **kwargs)
Source code in hypha/apply/utils/views.py
def post(self, request, *args, **kwargs):
    self.object_list = self.get_queryset()
    return super().post(request, *args, **kwargs)

DelegatedViewMixin

Bases: View

For use on create views accepting forms from another view

setup

setup(request, *args, **kwargs)

Initialize attributes shared by all view methods.

Source code in hypha/apply/utils/views.py
def setup(self, request, *args, **kwargs):
    """Initialize attributes shared by all view methods."""
    self.request = request
    self.args = args
    self.kwargs = kwargs

get_object

get_object()
Source code in hypha/apply/utils/views.py
def get_object(self):
    # Make sure the form instance, bound at the parent class level,  is the same as the
    # value we work with on the class.
    # If we don't have self.object, bind the parent instance to it. This value will then
    # be used by the form. Any further calls to get_object will get a new instance of the object
    if not hasattr(self, "object"):
        parent_object = self.get_parent_object()
        if isinstance(parent_object, self.model):
            return parent_object

    return super().get_object()

get_template_names

get_template_names()
Source code in hypha/apply/utils/views.py
def get_template_names(self):
    return self.kwargs["template_names"]

get_form_name

get_form_name()
Source code in hypha/apply/utils/views.py
def get_form_name(self):
    return self.context_name

get_form_kwargs

get_form_kwargs()
Source code in hypha/apply/utils/views.py
def get_form_kwargs(self):
    form_kwargs = super().get_form_kwargs()
    form_kwargs["user"] = self.request.user
    form_kwargs.update(**self.get_parent_kwargs())
    return form_kwargs

get_parent_kwargs

get_parent_kwargs()
Source code in hypha/apply/utils/views.py
def get_parent_kwargs(self):
    try:
        return self.parent.get_form_kwargs()
    except AttributeError:
        return self.kwargs["parent"].get_form_kwargs()

get_parent_object

get_parent_object()
Source code in hypha/apply/utils/views.py
def get_parent_object(self):
    return self.get_parent_kwargs()["instance"]

get_form

get_form(*args, **kwargs)
Source code in hypha/apply/utils/views.py
def get_form(self, *args, **kwargs):
    form = super().get_form(*args, **kwargs)
    form.name = self.get_form_name()
    return form

get_context_data

get_context_data(**kwargs)
Source code in hypha/apply/utils/views.py
def get_context_data(self, **kwargs):
    # Use the previous context but override the validated form
    form = kwargs.pop("form")
    kwargs.update(self.kwargs["context"])
    kwargs.update(**{self.context_name: form})
    return super().get_context_data(**kwargs)

is_model_form classmethod

is_model_form()
Source code in hypha/apply/utils/views.py
@classmethod
def is_model_form(cls):
    return issubclass(cls.form_class, ModelForm)

contribute_form

contribute_form(parent)
Source code in hypha/apply/utils/views.py
def contribute_form(self, parent):
    self.parent = parent

    # We do not want to bind any forms generated this way
    # pretend we are doing a get request to avoid passing data to forms
    old_method = None
    if self.request.method in ("POST", "PUT"):
        old_method = self.request.method
        self.request.method = "GET"

    form = self.get_form()

    if old_method:
        self.request.method = old_method
    return self.context_name, form

get_success_url

get_success_url()
Source code in hypha/apply/utils/views.py
def get_success_url(self):
    query = self.request.GET.urlencode()
    if query:
        query = "?" + query
    return self.request.path + query

CreateOrUpdateView

Bases: SingleObjectTemplateResponseMixin, ModelFormMixin, ProcessFormView

get

get(request, *args, **kwargs)
Source code in hypha/apply/utils/views.py
def get(self, request, *args, **kwargs):
    try:
        self.object = self.get_object()
    except self.model.DoesNotExist:
        self.object = None

    return super().get(request, *args, **kwargs)

post

post(request, *args, **kwargs)
Source code in hypha/apply/utils/views.py
def post(self, request, *args, **kwargs):
    try:
        self.object = self.get_object()
    except self.model.DoesNotExist:
        self.object = None

    return super().post(request, *args, **kwargs)

custom_wagtail_page_delete

custom_wagtail_page_delete(request, page_id)

Currently, ProtectedError exception is not caught in Wagtail admin. This workaround shows warning to the user if the page model like Fund, Round can not be deleted instead of raising 500. More details at https://github.com/wagtail/wagtail/issues/1602 Once the issue is fixed in Wagtail core, we can remove this workaround.

Source code in hypha/apply/utils/views.py
@require_admin_access
def custom_wagtail_page_delete(request, page_id):
    """
    Currently, ProtectedError exception is not caught in Wagtail admin.
    This workaround shows warning to the user if the page model like Fund, Round
    can not be deleted instead of raising 500.
    More details at https://github.com/wagtail/wagtail/issues/1602
    Once the issue is fixed in Wagtail core, we can remove this workaround.
    """
    try:
        return delete(request, page_id)
    except ProtectedError as e:
        protected_details = ", ".join([str(obj) for obj in e.protected_objects])
        page = get_object_or_404(Page, id=page_id).specific
        parent_id = page.get_parent().id
        messages.warning(
            request,
            _("Page '{0}' can't be deleted because is in use in '{1}'.").format(
                page.get_admin_display_title(), protected_details
            ),
        )
        return redirect("wagtailadmin_explore", parent_id)