def delete(self, request: Request, documentation_part_id: int, *args, **kwargs): if not has_permission(request.headers, PermissionSet.DocumentationEdit.value): return Response( data={'detail': 'User has no permissions for this action'}, status=status.HTTP_403_FORBIDDEN) documentation_part = DocumentationPart.objects.filter( id=documentation_part_id).first() if not documentation_part: return Response(data={'detail': 'DocumentationPart not found'}, status=status.HTTP_404_NOT_FOUND) order = documentation_part.order documentation_part.file.delete() documentation_part.delete() existing_documentations = DocumentationPart.objects.filter( order__gt=order).order_by('order') for dp in existing_documentations: dp.order -= 1 dp.save() return Response(data={})
def patch(self, request: Request, tariff_id: int, *args, **kwargs): if not has_permission(request.headers, PermissionSet.TariffEdit.value): return Response( data={'detail': 'User has no permissions for this action'}, status=status.HTTP_403_FORBIDDEN) serializer = ChangeTariffSerializer(data=request.data) serializer.is_valid(raise_exception=True) file = request.FILES.get('file') tariff = Tariff.objects.filter(id=tariff_id).first() if not tariff: return Response(data={'detail': 'Tariff not found'}, status=status.HTTP_404_NOT_FOUND) for k, v in serializer.validated_data.items(): if v: setattr(tariff, k, v) if file: tariff.file.delete() tariff.file = file tariff.save() return Response(data=TariffSerializer(tariff, context={ 'request': request }).data, status=status.HTTP_200_OK)
def patch(self, request: Request, supply_contract_id: int, *args, **kwargs): if not has_permission(request.headers, PermissionSet.SupplyContractEdit.value): return Response(data={'detail': 'User has no permissions for this action'}, status=status.HTTP_403_FORBIDDEN) serializer = ChangeSupplyContractSerializer(data=request.data) serializer.is_valid(raise_exception=True) file = request.FILES.get('file') supply_contract = SupplyContract.objects.filter(id=supply_contract_id).first() if not supply_contract: return Response(data={'detail': 'SupplyContract not found'}, status=status.HTTP_404_NOT_FOUND) supply_contract.type = serializer.validated_data.get('type', supply_contract.type) supply_contract.number = serializer.validated_data.get('number', supply_contract.number) supply_contract.notes = serializer.validated_data.get('notes', supply_contract.notes) supply_contract.expiration_date = serializer.validated_data.get('expiration_date', supply_contract.expiration_date) supply_contract.start_date = serializer.validated_data.get('start_date', supply_contract.start_date) if file: supply_contract.file.delete() supply_contract.file = file supply_contract.save() return Response(data=SupplyContractSerializer(supply_contract, context={'request': request}).data, status=status.HTTP_200_OK)
def patch(self, request: Request, document_id: int, *args, **kwargs): serializer = ChangeDocumentSerializer(data=request.data) serializer.is_valid(raise_exception=True) if not has_permission(request.headers, PermissionSet.DocumentEdit.value): return Response( data={'detail': 'User has no permissions for this action'}, status=status.HTTP_403_FORBIDDEN) file = request.FILES.get('file') document = Document.objects.filter(id=document_id).first() if not document: return Response(data={'detail': 'Document not found'}, status=status.HTTP_404_NOT_FOUND) document.name = serializer.validated_data.get('name', document.name) document.type = serializer.validated_data.get('type', document.type) if file: document.file.delete() document.file = file document.save() return Response(data=DocumentSerializer(document, context={ 'request': request }).data, status=status.HTTP_200_OK)
def get(self, request: Request, *args, **kwargs): if not has_permission(request.headers, PermissionSet.SupplyContractRead.value): return Response(data={'detail': 'User has no permissions for this action'}, status=status.HTTP_403_FORBIDDEN) return paginate( db_model=SupplyContract, serializer=SupplyContractSerializer, request=request, )
def get(self, request: Request, supply_contract_id: int, *args, **kwargs): if not has_permission(request.headers, PermissionSet.SupplyContractRead.value): return Response(data={'detail': 'User has no permissions for this action'}, status=status.HTTP_403_FORBIDDEN) supply_contract = SupplyContract.objects.filter(id=supply_contract_id).first() if not supply_contract: return Response(data={'detail': 'SupplyContract not found'}, status=status.HTTP_404_NOT_FOUND) return Response(data=SupplyContractSerializer(supply_contract, context={'request': request}).data)
def post(self, request: Request, *args, **kwargs): serializer = AddSupplyContractSerializer(data=request.data) serializer.is_valid(raise_exception=True) if not has_permission(request.headers, PermissionSet.SupplyContractEdit.value): return Response(data={'detail': 'User has no permissions for this action'}, status=status.HTTP_403_FORBIDDEN) if not (file := request.FILES.get('file')): return Response(data={'detail': 'No file received'}, status=status.HTTP_400_BAD_REQUEST)
def delete(self, request: Request, supply_contract_id: int, *args, **kwargs): if not has_permission(request.headers, PermissionSet.SupplyContractEdit.value): return Response(data={'detail': 'User has no permissions for this action'}, status=status.HTTP_403_FORBIDDEN) supply_contract = SupplyContract.objects.filter(id=supply_contract_id).first() if not supply_contract: return Response(data={'detail': 'SupplyContract not found'}, status=status.HTTP_404_NOT_FOUND) supply_contract.file.delete() supply_contract.delete() return Response(data={})
def delete(self, request: Request, tariff_id: int, *args, **kwargs): if not has_permission(request.headers, PermissionSet.TariffEdit.value): return Response( data={'detail': 'User has no permissions for this action'}, status=status.HTTP_403_FORBIDDEN) tariff = Tariff.objects.filter(id=tariff_id).first() if not tariff: return Response(data={'detail': 'Tariff not found'}, status=status.HTTP_404_NOT_FOUND) tariff.file.delete() tariff.delete() return Response(data={})
def get(self, request: Request, tariff_id: int, *args, **kwargs): if not has_permission(request.headers, PermissionSet.TariffRead.value): return Response( data={'detail': 'User has no permissions for this action'}, status=status.HTTP_403_FORBIDDEN) tariff = Tariff.objects.filter(id=tariff_id).first() if not tariff: return Response(data={'detail': 'Tariff not found'}, status=status.HTTP_404_NOT_FOUND) return Response( data=TariffSerializer(tariff, context={ 'request': request }).data)
def delete(self, request: Request, document_id: int, *args, **kwargs): if not has_permission(request.headers, PermissionSet.DocumentEdit.value): return Response( data={'detail': 'User has no permissions for this action'}, status=status.HTTP_403_FORBIDDEN) document = Document.objects.filter(id=document_id).first() if not document: return Response(data={'detail': 'Document not found'}, status=status.HTTP_404_NOT_FOUND) document.file.delete() document.delete() return Response(data={})
def post(self, request: Request, *args, **kwargs): serializer = AddTariffSerializer(data=request.data) serializer.is_valid(raise_exception=True) if not has_permission(request.headers, PermissionSet.TariffEdit.value): return Response( data={'detail': 'User has no permissions for this action'}, status=status.HTTP_403_FORBIDDEN) try: tariff = Tariff.objects.create(**serializer.validated_data, ) except IntegrityError: return Response(data={'detail': 'Wrong tariff info'}, status=status.HTTP_400_BAD_REQUEST) if file := request.FILES.get('file'): tariff.file = file tariff.save()
def has_permission(self, request, view): return has_permission(request.headers)
def patch(self, request: Request, documentation_part_id: int, *args, **kwargs): if not has_permission(request.headers, PermissionSet.DocumentationEdit.value): return Response( data={'detail': 'User has no permissions for this action'}, status=status.HTTP_403_FORBIDDEN) serializer = ChangeDocumentationPartSerializer(data=request.data) serializer.is_valid(raise_exception=True) file = request.FILES.get('file') documentation_part = DocumentationPart.objects.filter( id=documentation_part_id).first() if not documentation_part: return Response(data={'detail': 'DocumentationPart not found'}, status=status.HTTP_404_NOT_FOUND) order = serializer.validated_data.get('order') if order != documentation_part.order: if order <= 0: return Response(data={'detail': 'Order must be > 0'}, status=status.HTTP_400_BAD_REQUEST) existing_documentations = { dp.order: dp for dp in DocumentationPart.objects.filter().all() } old_order = documentation_part.order if order < old_order: need_to_be_pushed = range(old_order, order - 1, -1) for index in need_to_be_pushed: existing_documentations[index].order += 1 existing_documentations[index].save() else: max_order = max(list(existing_documentations.keys())) order = min(order, max_order) need_to_be_pushed = range(order, old_order - 1, -1) for index in need_to_be_pushed: existing_documentations[index].order -= 1 existing_documentations[index].save() documentation_part.name = serializer.validated_data.get( 'name', documentation_part.name) documentation_part.order = order if file: documentation_part.file.delete() documentation_part.file = file documentation_part.save() return Response(data=DocumentationPartSerializer(documentation_part, context={ 'request': request }).data, status=status.HTTP_200_OK)