Skip to content

Views

hypha.apply.api.v1.projects.views

InvoiceDeliverableViewSet

Bases: InvoiceNestedMixin, ProjectNestedMixin, CreateModelMixin, DestroyModelMixin, GenericViewSet

permission_classes class-attribute instance-attribute

permission_classes = (IsAuthenticated, HasDeliverableEditPermission, IsApplyStaffUser | IsFinance1User)

serializer_class class-attribute instance-attribute

pagination_class class-attribute instance-attribute

pagination_class = None

get_project_object

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

get_invoice_object

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

get_queryset

get_queryset()
Source code in hypha/apply/api/v1/projects/views.py
def get_queryset(self):
    invoice = self.get_invoice_object()
    return invoice.deliverables.all()

create

create(request, *args, **kwargs)
Source code in hypha/apply/api/v1/projects/views.py
def create(self, request, *args, **kwargs):
    ser = DeliverableSerializer(data=request.data)
    ser.is_valid(raise_exception=True)
    project = self.get_project_object()
    deliverable_id = ser.validated_data["id"]
    if not project.deliverables.filter(id=deliverable_id).exists():
        raise ValidationError({"detail": _("Not Found")})
    invoice = self.get_invoice_object()
    deliverable = get_object_or_404(Deliverable, id=deliverable_id)
    if invoice.deliverables.filter(deliverable=deliverable).exists():
        raise ValidationError({"detail": _("Invoice Already has this deliverable")})
    quantity = ser.validated_data["quantity"]
    if deliverable.available_to_invoice < quantity:
        raise ValidationError(
            {"detail": _("Required quantity is more than available")}
        )
    invoice_deliverable = InvoiceDeliverable.objects.create(
        deliverable=deliverable, quantity=ser.validated_data["quantity"]
    )
    invoice.deliverables.add(invoice_deliverable)
    ser = self.get_serializer(invoice.deliverables.all(), many=True)
    return Response(
        {
            "deliverables": ser.data,
            "total": invoice.deliverables_total_amount["total"],
        },
        status=status.HTTP_201_CREATED,
    )

get_serializer_context

get_serializer_context()
Source code in hypha/apply/api/v1/projects/views.py
def get_serializer_context(self):
    context = super().get_serializer_context()
    context["invoice"] = self.get_invoice_object()
    return context

destroy

destroy(request, *args, **kwargs)
Source code in hypha/apply/api/v1/projects/views.py
def destroy(self, request, *args, **kwargs):
    deliverable = self.get_object()
    invoice = self.get_invoice_object()
    invoice.deliverables.remove(deliverable)
    ser = self.get_serializer(invoice.deliverables.all(), many=True)
    return Response(
        {
            "deliverables": ser.data,
            "total": invoice.deliverables_total_amount["total"],
        },
    )