Skip to content

Views

hypha.apply.api.v1.views

SubmissionViewSet

Bases: ReadOnlyModelViewSet, GenericViewSet

permission_classes class-attribute instance-attribute

permission_classes = (IsAuthenticated, IsApplyStaffUser)

filter_backends class-attribute instance-attribute

filter_backends = (DjangoFilterBackend)

filter_class class-attribute instance-attribute

filter_class = SubmissionsFilter

pagination_class class-attribute instance-attribute

pagination_class = StandardResultsSetPagination

get_serializer_class

get_serializer_class()
Source code in hypha/apply/api/v1/views.py
def get_serializer_class(self):
    if self.action == "list":
        return SubmissionListSerializer
    return SubmissionDetailSerializer

get_queryset

get_queryset()
Source code in hypha/apply/api/v1/views.py
def get_queryset(self):
    if self.action == "list":
        return (
            ApplicationSubmission.objects.exclude_draft()
            .current()
            .with_latest_update()
        )
    return ApplicationSubmission.objects.exclude_draft().prefetch_related(
        Prefetch("reviews", Review.objects.submitted()),
    )

set_summary

set_summary(request, pk=None)
Source code in hypha/apply/api/v1/views.py
@action(detail=True, methods=["put"])
def set_summary(self, request, pk=None):
    submission = self.get_object()
    serializer = SubmissionSummarySerializer(data=request.data)
    serializer.is_valid(raise_exception=True)
    summary = serializer.validated_data["summary"]
    submission.summary = summary
    submission.save(update_fields=["summary"])
    serializer = self.get_serializer(submission)
    return Response(serializer.data)

meta_terms

meta_terms(request, pk=None)
Source code in hypha/apply/api/v1/views.py
@action(detail=True, methods=["post"])
def meta_terms(self, request, pk=None):
    submission = self.get_object()
    serializer = SubmissionMetaTermsSerializer(data=request.data)
    serializer.is_valid(raise_exception=True)
    meta_terms_ids = serializer.validated_data["meta_terms"]
    submission.meta_terms.set(meta_terms_ids)
    serializer = self.get_serializer(submission)
    return Response(serializer.data)

SubmissionFilters

Bases: APIView

permission_classes class-attribute instance-attribute

permission_classes = (IsAuthenticated, IsApplyStaffUser)

filter_unique_options

filter_unique_options(options)
Source code in hypha/apply/api/v1/views.py
def filter_unique_options(self, options):
    unique_items = [
        dict(item) for item in {tuple(option.items()) for option in options}
    ]
    return list(filter(lambda x: len(x.get("label")), unique_items))

format

format(filterKey, label, options)
Source code in hypha/apply/api/v1/views.py
def format(self, filterKey, label, options):
    if label == "Screenings":
        options.insert(0, {"key": None, "label": "No Screening"})
    return {"filterKey": filterKey, "label": label, "options": options}

get

get(request, format=None)
Source code in hypha/apply/api/v1/views.py
def get(self, request, format=None):
    filter_options = [
        self.format(
            "fund",
            "Funds",
            [
                {"key": fund.get("id"), "label": fund.get("title")}
                for fund in get_used_funds().values()
            ],
        ),
        self.format(
            "round",
            "Rounds",
            [
                {"key": round.get("id"), "label": round.get("title")}
                for round in get_used_rounds().values()
            ],
        ),
        self.format(
            "status",
            "Statuses",
            [
                {"key": list(STATUSES.get(label)), "label": label}
                for label in dict(STATUSES)
            ],
        ),
        self.format(
            "screening_statuses",
            "Screenings",
            self.filter_unique_options(
                [
                    {"key": screening.get("id"), "label": screening.get("title")}
                    for screening in get_screening_statuses().values()
                ]
            ),
        ),
        self.format(
            "lead",
            "Leads",
            [
                {
                    "key": lead.get("id"),
                    "label": lead.get("full_name") or lead.get("email"),
                }
                for lead in get_round_leads().values()
            ],
        ),
        self.format(
            "reviewers",
            "Reviewers",
            self.filter_unique_options(
                [
                    {
                        "key": reviewer.get("id"),
                        "label": reviewer.get("full_name") or reviewer.get("email"),
                    }
                    for reviewer in get_all_reviewers().values()
                ]
            ),
        ),
        self.format(
            "category_options",
            "Category",
            self.filter_unique_options(
                [
                    {"key": option.get("id"), "label": option.get("value")}
                    for option in get_category_options().values()
                ]
            ),
        ),
    ]
    return Response(filter_options)

SubmissionActionViewSet

Bases: SubmissionNestedMixin, GenericViewSet

serializer_class class-attribute instance-attribute

serializer_class = SubmissionActionSerializer

permission_classes class-attribute instance-attribute

permission_classes = (IsAuthenticated, IsApplyStaffUser)

get_submission_object

get_submission_object()
Source code in hypha/apply/api/v1/mixin.py
def get_submission_object(self):
    return get_object_or_404(ApplicationSubmission, id=self.kwargs["submission_pk"])

get_object

get_object()
Source code in hypha/apply/api/v1/views.py
def get_object(self):
    return self.get_submission_object()

list

list(request, *args, **kwargs)

List all the actions that can be taken on a submission.

E.g. All the states this submission can be transistion to.

Source code in hypha/apply/api/v1/views.py
def list(self, request, *args, **kwargs):
    """
    List all the actions that can be taken on a submission.

    E.g. All the states this submission can be transistion to.
    """
    obj = self.get_object()
    ser = self.get_serializer(obj)
    return Response(ser.data)

create

create(request, *args, **kwargs)

Transistion a submission from one state to other.

E.g. To transition a submission from Screening to Internal Review following post data can be used:

{"action": "internal_review"}
Source code in hypha/apply/api/v1/views.py
def create(self, request, *args, **kwargs):
    """
    Transistion a submission from one state to other.

    E.g. To transition a submission from `Screening` to `Internal Review`
    following post data can be used:

    ```
    {"action": "internal_review"}
    ```
    """
    action = request.data.get("action")
    if not action:
        raise ValidationError("Action must be provided.")
    obj = self.get_object()

    redirect = DeterminationCreateOrUpdateView.should_redirect(request, obj, action)
    if redirect:
        raise NotFound(
            {
                "detail": "The action should be performed at the determination view",
                "target": redirect.url,
            }
        )
    try:
        obj.perform_transition(action, self.request.user, request=self.request)
    except DjangoPermissionDenied as e:
        raise PermissionDenied(str(e)) from e
    # refresh_from_db() raises errors for particular actions.
    obj = self.get_object()
    serializer = SubmissionDetailSerializer(
        obj,
        context={
            "request": request,
        },
    )
    return Response(
        {
            "id": serializer.data["id"],
            "status": serializer.data["status"],
            "actions": serializer.data["actions"],
            "phase": serializer.data["phase"],
        }
    )

RoundViewSet

Bases: RetrieveModelMixin, ListModelMixin, GenericViewSet

serializer_class class-attribute instance-attribute

serializer_class = RoundLabSerializer

permission_classes class-attribute instance-attribute

permission_classes = (IsAuthenticated, IsApplyStaffUser)

permission_classes_by_action class-attribute instance-attribute

permission_classes_by_action = {'open': [HasAPIKey | IsAuthenticated, HasAPIKey | IsApplyStaffUser]}

pagination_class class-attribute instance-attribute

pagination_class = StandardResultsSetPagination

queryset property

queryset

get_serializer_class

get_serializer_class()
Source code in hypha/apply/api/v1/views.py
def get_serializer_class(self):
    if self.action == "list":
        return RoundLabSerializer
    elif self.action == "open":
        return OpenRoundLabSerializer
    return RoundLabDetailSerializer

get_object

get_object()
Source code in hypha/apply/api/v1/views.py
def get_object(self):
    obj = super(RoundViewSet, self).get_object()
    return obj.specific

get_permissions

get_permissions()
Source code in hypha/apply/api/v1/views.py
def get_permissions(self):
    try:
        # return permission_classes depending on `action`
        return [
            permission()
            for permission in self.permission_classes_by_action[self.action]
        ]
    except KeyError:
        # action is not set return default permission_classes
        return [permission() for permission in self.permission_classes]

open

open(request)
Source code in hypha/apply/api/v1/views.py
@action(methods=["get"], detail=False)
def open(self, request):
    queryset = RoundsAndLabs.objects.open()
    page = self.paginate_queryset(queryset)
    if page is not None:
        serializer = self.get_serializer(page, many=True)
        return self.get_paginated_response(serializer.data)
    serializer = self.get_serializer(queryset, many=True)
    return Response(serializer.data)

CurrentUser

Bases: APIView

permission_classes class-attribute instance-attribute

permission_classes = [IsAuthenticated]

get

get(request, format=None)
Source code in hypha/apply/api/v1/views.py
def get(self, request, format=None):
    ser = UserSerializer(request.user)
    return Response(ser.data)