Skip to content

Views

hypha.apply.activity.views

ActivityContextMixin

Mixin to add related 'comments' of the current view's 'self.object'

get_context_data

get_context_data(**kwargs)
Source code in hypha/apply/activity/views.py
def get_context_data(self, **kwargs):
    extra = {
        # Do not prefetch on the related_object__author as the models
        # are not homogeneous and this will fail
        "comments": services.get_related_comments_for_user(
            self.object, self.request.user
        ),
        "comments_count": services.get_comment_count(
            self.object, self.request.user
        ),
    }
    return super().get_context_data(**extra, **kwargs)

CommentFormView

Bases: DelegatedViewMixin, CreateView

form_class class-attribute instance-attribute

form_class = CommentForm

context_name class-attribute instance-attribute

context_name = 'comment_form'

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

form_valid

form_valid(form)
Source code in hypha/apply/activity/views.py
def form_valid(self, form):
    source = self.kwargs["object"]
    form.instance.user = self.request.user
    form.instance.source = source
    form.instance.type = COMMENT
    form.instance.timestamp = timezone.now()
    response = super().form_valid(form)
    messenger(
        MESSAGES.COMMENT,
        request=self.request,
        user=self.request.user,
        source=source,
        related=self.object,
    )
    return response

get_success_url

get_success_url()
Source code in hypha/apply/activity/views.py
def get_success_url(self):
    return self.object.source.get_absolute_url() + "#communications"

get_form_kwargs

get_form_kwargs()

Get the kwargs for the CommentForm.

Returns:

  • dict –

    A dict of kwargs to be passed to CommentForm.

  • dict –

    The submission instance is removed from this return, while a boolean of has_partners is

  • dict –

    added based off the submission.

Source code in hypha/apply/activity/views.py
def get_form_kwargs(self) -> dict:
    """Get the kwargs for the [`CommentForm`][hypha.apply.activity.forms.CommentForm].

    Returns:
        A dict of kwargs to be passed to [`CommentForm`][hypha.apply.activity.forms.CommentForm].
        The submission instance is removed from this return, while a boolean of `has_partners` is
        added based off the submission.
    """
    kwargs = super().get_form_kwargs()
    instance = kwargs.pop("instance")
    if isinstance(instance, ApplicationSubmission):
        kwargs["submission_partner_list"] = instance.partners.all()
    return kwargs

AttachmentView

Bases: PrivateMediaView

storage class-attribute instance-attribute

storage = PrivateStorage()

model class-attribute instance-attribute

get

get(*args, **kwargs)
Source code in hypha/apply/utils/storage.py
def get(self, *args, **kwargs):
    file_to_serve = self.get_media(*args, **kwargs)
    return FileResponse(file_to_serve)

dispatch

dispatch(*args, **kwargs)
Source code in hypha/apply/activity/views.py
def dispatch(self, *args, **kwargs):
    file_pk = kwargs.get("file_pk")
    self.instance = get_object_or_404(ActivityAttachment, uuid=file_pk)

    return super().dispatch(*args, **kwargs)

get_media

get_media(*args, **kwargs)
Source code in hypha/apply/activity/views.py
def get_media(self, *args, **kwargs):
    return self.instance.file

NotificationsView

Bases: ListView

model class-attribute instance-attribute

model = Activity

template_name class-attribute instance-attribute

template_name = 'activity/notifications.html'

filterset_class class-attribute instance-attribute

filterset_class = NotificationFilter

get_template_names

get_template_names()
Source code in hypha/apply/activity/views.py
def get_template_names(self):
    if self.request.htmx and self.request.GET.get("type") == "header_dropdown":
        return ["activity/include/notifications_dropdown.html"]
    return super().get_template_names()

get_queryset

get_queryset()
Source code in hypha/apply/activity/views.py
def get_queryset(self):
    # List only last 30 days' activities
    queryset = Activity.objects.filter(current=True).latest()

    self.filterset = self.filterset_class(self.request.GET, queryset=queryset)
    qs = self.filterset.qs.distinct().order_by("-timestamp")

    if self.request.htmx and self.request.GET.get("type") == "header_dropdown":
        qs = qs[:5]
    return qs

get_context_data

get_context_data(*, object_list=None, **kwargs)
Source code in hypha/apply/activity/views.py
def get_context_data(self, *, object_list=None, **kwargs):
    context = super(NotificationsView, self).get_context_data()
    context["filter"] = self.filterset
    return context

partial_comments

partial_comments(request, content_type, pk)

Render a partial view of comments for a given content type and primary key.

This view handles comments for both 'submission' and 'project' content types. It checks the user's permissions and fetches the related comments for the user. The comments are paginated and rendered in the 'comment_list' template.

Parameters:

  • request (HttpRequest) –

    The HTTP request object.

  • content_type (str) –

    The type of content ('submission' or 'project').

  • pk (int) –

    The primary key of the content object.

Returns:

  • HttpResponse –

    The rendered 'comment_list' template with the context data.

Source code in hypha/apply/activity/views.py
@login_required
@require_http_methods(["GET"])
def partial_comments(request, content_type: str, pk: int):
    """
    Render a partial view of comments for a given content type and primary key.

    This view handles comments for both 'submission' and 'project' content types.
    It checks the user's permissions and fetches the related comments for the user.
    The comments are paginated and rendered in the 'comment_list' template.

    Args:
        request (HttpRequest): The HTTP request object.
        content_type (str): The type of content ('submission' or 'project').
        pk (int): The primary key of the content object.

    Returns:
        HttpResponse: The rendered 'comment_list' template with the context data.
    """
    if content_type == "submission":
        obj = get_object_or_404(ApplicationSubmission, pk=pk)
        has_funds_permission(
            "submission_view", request.user, object=obj, raise_exception=True
        )
        editable = not obj.is_archive
    elif content_type == "project":
        obj = get_object_or_404(Project, pk=pk)
        has_projects_permission(
            "project_access", request.user, object=obj, raise_exception=True
        )
        editable = False if obj.status == "complete" else True
    else:
        return render(request, "activity/include/comment_list.html", {})

    qs = services.get_related_comments_for_user(obj, request.user)
    page = Paginator(qs, per_page=10, orphans=5).page(request.GET.get("page", 1))

    ctx = {
        "page": page,
        "comments": page.object_list,
        "editable": editable,
    }
    return render(request, "activity/include/comment_list.html", ctx)

edit_comment

edit_comment(request, pk)

Edit a comment.

Source code in hypha/apply/activity/views.py
@login_required
def edit_comment(request, pk):
    """Edit a comment."""
    activity = get_object_or_404(Activity, id=pk)

    if activity.type != COMMENT or activity.user != request.user:
        raise PermissionError("You can only edit your own comments")

    if request.GET.get("action") == "cancel":
        return render(
            request,
            "activity/partial_comment_message.html",
            {"activity": activity},
        )

    if request.method == "POST":
        activity = services.edit_comment(activity, request.POST.get("message"))

        return render(
            request,
            "activity/partial_comment_message.html",
            {"activity": activity, "success": True},
        )

    return render(request, "activity/ui/edit_comment_form.html", {"activity": activity})