def post(self, request, *args, **kwargs): doc_pk = kwargs.get('document_pk') Model = self.get_model() model_name = self.get_model_name() try: document = Model.objects.get(pk=doc_pk) except Model.DoesNotExist: msg = "{model} not found".format(model=model_name) return Response({"detail": msg}, status=status.HTTP_404_NOT_FOUND) if document.state != BillingDocument.STATES.DRAFT: msg = "{model} entries can be added only when the {model_lower} is"\ " in draft state.".format(model=model_name, model_lower=model_name.lower()) return Response({"detail": msg}, status=status.HTTP_403_FORBIDDEN) serializer = DocumentEntrySerializer(data=request.data, context={'request': request}) if serializer.is_valid(raise_exception=True): # This will be eiter {invoice: <invoice_object>} or # {proforma: <proforma_object>} as a DocumentEntry can have a # foreign key to either an invoice or a proforma extra_context = {model_name.lower(): document} serializer.save(**extra_context) return Response(serializer.data, status=status.HTTP_201_CREATED)
def post(self, request, *args, **kwargs): doc_pk = kwargs.get('document_pk') Model = self.get_model() model_name = self.get_model_name() try: document = Model.objects.get(pk=doc_pk) except Model.DoesNotExist: msg = "{model} not found".format(model=model_name) return Response({"detail": msg}, status=status.HTTP_404_NOT_FOUND) if document.state != BillingDocument.STATES.DRAFT: msg = "{model} entries can be added only when the {model_lower} is"\ " in draft state.".format(model=model_name, model_lower=model_name.lower()) return Response({"detail": msg}, status=status.HTTP_403_FORBIDDEN) serializer = DocumentEntrySerializer(data=request.DATA, context={'request': request}) if serializer.is_valid(raise_exception=True): # This will be eiter {invoice: <invoice_object>} or # {proforma: <proforma_object>} as a DocumentEntry can have a # foreign key to either an invoice or a proforma extra_context = {model_name.lower(): document} serializer.save(**extra_context) return Response(serializer.data, status=status.HTTP_201_CREATED)
def put(self, request, *args, **kwargs): doc_pk = kwargs.get('document_pk') entry_pk = kwargs.get('entry_pk') Model = self.get_model() model_name = self.get_model_name() document = get_object_or_404(Model, pk=doc_pk) if document.state != BillingDocument.STATES.DRAFT: msg = "{model} entries can be added only when the {model_lower} is"\ " in draft state.".format(model=model_name, model_lower=model_name.lower()) return Response({"detail": msg}, status=status.HTTP_403_FORBIDDEN) searched_fields = {model_name.lower(): document, 'pk': entry_pk} entry = get_object_or_404(DocumentEntry, **searched_fields) serializer = DocumentEntrySerializer(entry, data=request.data, context={'request': request}) if serializer.is_valid(raise_exception=True): serializer.save() return Response(serializer.data)
def put(self, request, *args, **kwargs): doc_pk = kwargs.get('document_pk') entry_pk = kwargs.get('entry_pk') Model = self.get_model() model_name = self.get_model_name() document = get_object_or_404(Model, pk=doc_pk) if document.state != BillingDocument.STATES.DRAFT: msg = "{model} entries can be added only when the {model_lower} is"\ " in draft state.".format(model=model_name, model_lower=model_name.lower()) return Response({"detail": msg}, status=status.HTTP_403_FORBIDDEN) searched_fields = {model_name.lower(): document, 'pk': entry_pk} entry = get_object_or_404(DocumentEntry, **searched_fields) serializer = DocumentEntrySerializer(entry, data=request.DATA, context={'request': request}) if serializer.is_valid(raise_exception=True): serializer.save() return Response(serializer.data)