def put(self, request, pk): """ Edit details of a good. This includes description, control codes and PV grading. """ good = get_good(pk) if good.organisation.id != get_request_user_organisation_id(request): raise PermissionDenied() if good.status == GoodStatus.SUBMITTED: return JsonResponse(data={ "errors": "This good is already on a submitted application" }, status=status.HTTP_400_BAD_REQUEST) data = request.data.copy() if data.get("is_good_controlled") is None or data.get( "is_pv_graded") == GoodPvGraded.GRADING_REQUIRED: for good_on_application in GoodOnApplication.objects.filter( good=good): good_on_application.delete() data["organisation"] = get_request_user_organisation_id(request) serializer = GoodCreateSerializer(instance=good, data=data, partial=True) return create_or_update_good(serializer, data.get("validate_only"), is_created=False)
def delete(self, request, pk, doc_pk): """ Deletes good document """ good = get_good(pk) if good.organisation.id != get_request_user_organisation_id(request): raise PermissionDenied() if good.status != GoodStatus.DRAFT: return JsonResponse(data={ "errors": "This good is already on a submitted application" }, status=status.HTTP_400_BAD_REQUEST) good_document = Document.objects.get(id=doc_pk) document = get_good_document(good, good_document.id) document.delete_s3() good_document.delete() if GoodDocument.objects.filter(good=good).count() == 0: for good_on_application in GoodOnApplication.objects.filter( good=good): good_on_application.delete() return JsonResponse({"document": "deleted success"})
def get(self, request, pk): good = get_good(pk) if hasattr(request.user, "exporteruser"): if good.organisation.id != get_request_user_organisation_id( request): raise PermissionDenied() if str_to_bool(request.GET.get("full_detail")): serializer = GoodSerializerExporterFullDetail( good, context={ "exporter_user": request.user.exporteruser, "organisation_id": get_request_user_organisation_id(request), }, ) else: serializer = GoodSerializerExporter(good) # If there's a query with this good, update the notifications on it query = GoodsQuery.objects.filter(good=good) if query: delete_exporter_notifications( user=request.user.exporteruser, organisation_id=get_request_user_organisation_id(request), objects=query, ) else: serializer = GoodSerializerInternal(good) return JsonResponse(data={"good": serializer.data}, status=status.HTTP_200_OK)
def put(self, request, pk): """ Edit the TAU details of a good. This includes military use, component and information security use. """ good = get_good(pk) data = request.data.copy() # return bad request if trying to edit software_or_technology details outside of category group 3 if good.item_category in ItemCategory.group_one and "software_or_technology_details" in data: raise BadRequestError( {"non_field_errors": [strings.Goods.CANNOT_SET_DETAILS_ERROR]}) # return bad request if trying to edit component and component details outside of category group 1 if good.item_category in ItemCategory.group_three and data.get( "is_component_step"): raise BadRequestError( {"non_field_errors": [strings.Goods.CANNOT_SET_DETAILS_ERROR]}) # return bad request if editing any of the firearm details on a good that is not in group 2 firearms if good.item_category not in ItemCategory.group_two and data.get( "firearm_details"): check_if_firearm_details_edited_on_unsupported_good(data) if good.status == GoodStatus.SUBMITTED: raise BadRequestError( {"non_field_errors": [strings.Goods.CANNOT_EDIT_GOOD]}) serializer = GoodCreateSerializer(instance=good, data=data, partial=True) return create_or_update_good(serializer, data.get("validate_only"), is_created=False)
def post(self, request, pk): good = get_good(pk) data = request.data if data.get("has_document_to_upload"): document_to_upload = str_to_bool(data["has_document_to_upload"]) if not document_to_upload: good.missing_document_reason = data["missing_document_reason"] serializer = GoodMissingDocumentSerializer(instance=good, data=data, partial=True) if serializer.is_valid(): serializer.save() good_data = GoodCreateSerializer(good).data else: return JsonResponse(data={"errors": serializer.errors}, status=status.HTTP_400_BAD_REQUEST) else: good.missing_document_reason = None good.save() good_data = GoodCreateSerializer(good).data else: return JsonResponse( data={ "errors": { "has_document_to_upload": [strings.Goods.DOCUMENT_CHECK_OPTION_NOT_SELECTED] } }, status=status.HTTP_400_BAD_REQUEST, ) return JsonResponse(data={"good": good_data}, status=status.HTTP_200_OK)
def post(self, request): """ Create a new GoodsQuery case instance """ data = request.data good = get_good(data["good_id"]) data["organisation"] = get_request_user_organisation_id(request) is_clc_required = good.is_good_controlled is None is_pv_grading_required = good.is_pv_graded == GoodPvGraded.GRADING_REQUIRED errors = self._check_request_for_errors(good, is_clc_required, is_pv_grading_required) if errors: return JsonResponse(data={"errors": errors}, status=status.HTTP_400_BAD_REQUEST) good.status = GoodStatus.QUERY goods_query = GoodsQuery.objects.create( clc_control_list_entry=data.get("clc_control_list_entry"), clc_raised_reasons=data.get("clc_raised_reasons"), pv_grading_raised_reasons=data.get("pv_grading_raised_reasons"), good=good, organisation_id=data["organisation"], case_type_id=CaseTypeEnum.GOODS.id, status=get_starting_status(is_clc_required), submitted_at=django.utils.timezone.now(), submitted_by=request.user.exporteruser, ) # attach flags based on what's required if is_clc_required: flag = Flag.objects.get(id=SystemFlags.GOOD_CLC_QUERY_ID) goods_query.flags.add(flag) goods_query.clc_responded = False if is_pv_grading_required: flag = Flag.objects.get(id=SystemFlags.GOOD_PV_GRADING_QUERY_ID) goods_query.flags.add(flag) goods_query.pv_grading_responded = False good.save() goods_query.save() audit_trail_service.create( actor=request.user, verb=AuditType.CREATED, action_object=goods_query.get_case(), payload={"status": {"new": goods_query.status.status}}, ) apply_flagging_rules_to_case(goods_query) return JsonResponse(data={"id": goods_query.id}, status=status.HTTP_201_CREATED)
def get(self, request, pk): good = get_good(pk) if hasattr(request.user, "exporteruser"): if good.organisation.id != get_request_user_organisation_id( request): raise PermissionDenied() else: serializer = TinyGoodDetailsSerializer(good) return JsonResponse(data={"good": serializer.data}, status=status.HTTP_200_OK)
def delete(self, request, pk): good = get_good(pk) if good.organisation.id != get_request_user_organisation_id(request): raise PermissionDenied() if good.status != GoodStatus.DRAFT: return JsonResponse( data={"errors": "Good is already on a submitted application"}, status=status.HTTP_400_BAD_REQUEST) for document in GoodDocument.objects.filter(good=good): document.delete_s3() good.delete() return JsonResponse(data={"status": "Good Deleted"}, status=status.HTTP_200_OK)
def post(self, request, pk): """ Adds a document to the specified good """ good = get_good(pk) good_id = str(good.id) data = request.data if good.organisation.id != get_request_user_organisation_id(request): delete_documents_on_bad_request(data) raise PermissionDenied() if good.status != GoodStatus.DRAFT: delete_documents_on_bad_request(data) return JsonResponse(data={ "errors": "This good is already on a submitted application" }, status=status.HTTP_400_BAD_REQUEST) for document in data: document["good"] = good_id document["user"] = request.user.pk document["organisation"] = get_request_user_organisation_id( request) serializer = GoodDocumentCreateSerializer(data=data, many=True) if serializer.is_valid(): try: serializer.save() except Exception as e: # noqa return JsonResponse( {"errors": { "file": strings.Documents.UPLOAD_FAILURE }}, status=status.HTTP_400_BAD_REQUEST) # Delete missing document reason as a document has now been uploaded good.missing_document_reason = None good.save() return JsonResponse({"documents": serializer.data}, status=status.HTTP_201_CREATED) delete_documents_on_bad_request(data) return JsonResponse({"errors": serializer.errors}, status=status.HTTP_400_BAD_REQUEST)
def get(self, request, pk, doc_pk): """ Returns a list of documents on the specified good """ good = get_good(pk) if good.organisation.id != get_request_user_organisation_id(request): raise PermissionDenied() if good.status != GoodStatus.DRAFT: return JsonResponse(data={ "errors": "This good is already on a submitted application" }, status=status.HTTP_400_BAD_REQUEST) good_document = get_good_document(good, doc_pk) serializer = GoodDocumentViewSerializer(good_document) return JsonResponse({"document": serializer.data})