2024-07-19 19:29:27 +03:00
|
|
|
''' Endpoints for OSS. '''
|
2024-08-14 23:38:50 +03:00
|
|
|
from typing import Optional, cast
|
2024-07-19 19:29:27 +03:00
|
|
|
|
|
|
|
from django.db import transaction
|
2024-08-14 23:38:50 +03:00
|
|
|
from django.db.models import Q
|
2024-08-07 21:54:50 +03:00
|
|
|
from django.http import HttpResponse
|
2024-07-19 19:29:27 +03:00
|
|
|
from drf_spectacular.utils import extend_schema, extend_schema_view
|
2024-07-28 00:37:50 +03:00
|
|
|
from rest_framework import generics, serializers
|
2024-07-19 19:29:27 +03:00
|
|
|
from rest_framework import status as c
|
|
|
|
from rest_framework import viewsets
|
|
|
|
from rest_framework.decorators import action
|
|
|
|
from rest_framework.request import Request
|
|
|
|
from rest_framework.response import Response
|
|
|
|
|
2024-07-31 18:09:31 +03:00
|
|
|
from apps.library.models import LibraryItem, LibraryItemType
|
2024-07-25 19:12:59 +03:00
|
|
|
from apps.library.serializers import LibraryItemSerializer
|
2024-10-28 14:53:41 +03:00
|
|
|
from apps.rsform.models import Constituenta, RSForm
|
2024-08-01 21:16:40 +03:00
|
|
|
from apps.rsform.serializers import CstTargetSerializer
|
2024-07-28 00:37:50 +03:00
|
|
|
from shared import messages as msg
|
2024-07-19 19:29:27 +03:00
|
|
|
from shared import permissions
|
|
|
|
|
|
|
|
from .. import models as m
|
|
|
|
from .. import serializers as s
|
|
|
|
|
|
|
|
|
|
|
|
@extend_schema(tags=['OSS'])
|
|
|
|
@extend_schema_view()
|
|
|
|
class OssViewSet(viewsets.GenericViewSet, generics.ListAPIView, generics.RetrieveAPIView):
|
|
|
|
''' Endpoint: OperationSchema. '''
|
2024-07-25 19:12:59 +03:00
|
|
|
queryset = LibraryItem.objects.filter(item_type=LibraryItemType.OPERATION_SCHEMA)
|
|
|
|
serializer_class = LibraryItemSerializer
|
2024-07-19 19:29:27 +03:00
|
|
|
|
2024-07-25 19:12:59 +03:00
|
|
|
def _get_item(self) -> LibraryItem:
|
|
|
|
return cast(LibraryItem, self.get_object())
|
2024-07-19 19:29:27 +03:00
|
|
|
|
|
|
|
def get_permissions(self):
|
|
|
|
''' Determine permission class. '''
|
|
|
|
if self.action in [
|
|
|
|
'create_operation',
|
|
|
|
'delete_operation',
|
2024-07-28 00:37:50 +03:00
|
|
|
'update_positions',
|
2024-07-28 21:30:10 +03:00
|
|
|
'create_input',
|
2024-07-29 22:31:11 +03:00
|
|
|
'set_input',
|
|
|
|
'update_operation',
|
2024-10-28 14:53:41 +03:00
|
|
|
'execute_operation',
|
|
|
|
'relocate_constituents'
|
2024-07-19 19:29:27 +03:00
|
|
|
]:
|
|
|
|
permission_list = [permissions.ItemEditor]
|
|
|
|
elif self.action in ['details']:
|
|
|
|
permission_list = [permissions.ItemAnyone]
|
2024-08-01 21:16:40 +03:00
|
|
|
elif self.action in ['get_predecessor']:
|
|
|
|
permission_list = [permissions.Anyone]
|
2024-07-19 19:29:27 +03:00
|
|
|
else:
|
|
|
|
permission_list = [permissions.Anyone]
|
|
|
|
return [permission() for permission in permission_list]
|
|
|
|
|
|
|
|
@extend_schema(
|
|
|
|
summary='get operations data',
|
|
|
|
tags=['OSS'],
|
|
|
|
request=None,
|
|
|
|
responses={
|
|
|
|
c.HTTP_200_OK: s.OperationSchemaSerializer,
|
|
|
|
c.HTTP_404_NOT_FOUND: None
|
|
|
|
}
|
|
|
|
)
|
|
|
|
@action(detail=True, methods=['get'], url_path='details')
|
2024-08-07 21:54:50 +03:00
|
|
|
def details(self, request: Request, pk) -> HttpResponse:
|
2024-07-19 19:29:27 +03:00
|
|
|
''' Endpoint: Detailed OSS data. '''
|
2024-07-25 19:12:59 +03:00
|
|
|
serializer = s.OperationSchemaSerializer(self._get_item())
|
2024-07-19 19:29:27 +03:00
|
|
|
return Response(
|
|
|
|
status=c.HTTP_200_OK,
|
|
|
|
data=serializer.data
|
|
|
|
)
|
|
|
|
|
|
|
|
@extend_schema(
|
|
|
|
summary='update positions',
|
|
|
|
tags=['OSS'],
|
|
|
|
request=s.PositionsSerializer,
|
|
|
|
responses={
|
|
|
|
c.HTTP_200_OK: None,
|
|
|
|
c.HTTP_403_FORBIDDEN: None,
|
|
|
|
c.HTTP_404_NOT_FOUND: None
|
|
|
|
}
|
|
|
|
)
|
|
|
|
@action(detail=True, methods=['patch'], url_path='update-positions')
|
2024-08-07 21:54:50 +03:00
|
|
|
def update_positions(self, request: Request, pk) -> HttpResponse:
|
2024-07-19 19:29:27 +03:00
|
|
|
''' Endpoint: Update operations positions. '''
|
|
|
|
serializer = s.PositionsSerializer(data=request.data)
|
|
|
|
serializer.is_valid(raise_exception=True)
|
2024-07-25 19:12:59 +03:00
|
|
|
m.OperationSchema(self.get_object()).update_positions(serializer.validated_data['positions'])
|
2024-07-19 19:29:27 +03:00
|
|
|
return Response(status=c.HTTP_200_OK)
|
|
|
|
|
|
|
|
@extend_schema(
|
|
|
|
summary='create operation',
|
|
|
|
tags=['OSS'],
|
|
|
|
request=s.OperationCreateSerializer(),
|
|
|
|
responses={
|
|
|
|
c.HTTP_201_CREATED: s.NewOperationResponse,
|
|
|
|
c.HTTP_400_BAD_REQUEST: None,
|
|
|
|
c.HTTP_403_FORBIDDEN: None,
|
|
|
|
c.HTTP_404_NOT_FOUND: None
|
|
|
|
}
|
|
|
|
)
|
|
|
|
@action(detail=True, methods=['post'], url_path='create-operation')
|
2024-08-07 21:54:50 +03:00
|
|
|
def create_operation(self, request: Request, pk) -> HttpResponse:
|
2024-07-19 19:29:27 +03:00
|
|
|
''' Create new operation. '''
|
|
|
|
serializer = s.OperationCreateSerializer(data=request.data)
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
|
2024-07-25 19:12:59 +03:00
|
|
|
oss = m.OperationSchema(self.get_object())
|
2024-07-19 19:29:27 +03:00
|
|
|
with transaction.atomic():
|
2024-07-25 19:12:59 +03:00
|
|
|
oss.update_positions(serializer.validated_data['positions'])
|
2024-07-31 18:09:31 +03:00
|
|
|
new_operation = oss.create_operation(**serializer.validated_data['item_data'])
|
2024-08-14 23:38:50 +03:00
|
|
|
schema = new_operation.result
|
|
|
|
if schema is not None:
|
|
|
|
connected_operations = \
|
|
|
|
m.Operation.objects \
|
|
|
|
.filter(Q(result=schema) & ~Q(pk=new_operation.pk)) \
|
|
|
|
.only('operation_type', 'oss_id')
|
|
|
|
for operation in connected_operations:
|
|
|
|
if operation.operation_type != m.OperationType.INPUT:
|
|
|
|
raise serializers.ValidationError({
|
|
|
|
'item_data': msg.operationResultFromAnotherOSS()
|
|
|
|
})
|
|
|
|
if operation.oss_id == new_operation.oss_id:
|
|
|
|
raise serializers.ValidationError({
|
|
|
|
'item_data': msg.operationInputAlreadyConnected()
|
|
|
|
})
|
2024-07-31 18:09:31 +03:00
|
|
|
if new_operation.operation_type == m.OperationType.INPUT and serializer.validated_data['create_schema']:
|
|
|
|
oss.create_input(new_operation)
|
2024-07-21 15:19:57 +03:00
|
|
|
if new_operation.operation_type != m.OperationType.INPUT and 'arguments' in serializer.validated_data:
|
2024-07-29 22:31:11 +03:00
|
|
|
oss.set_arguments(
|
2024-08-16 20:57:37 +03:00
|
|
|
target=new_operation.pk,
|
2024-07-29 22:31:11 +03:00
|
|
|
arguments=serializer.validated_data['arguments']
|
|
|
|
)
|
2024-07-28 00:37:50 +03:00
|
|
|
return Response(
|
2024-07-19 19:29:27 +03:00
|
|
|
status=c.HTTP_201_CREATED,
|
|
|
|
data={
|
|
|
|
'new_operation': s.OperationSerializer(new_operation).data,
|
2024-07-25 19:12:59 +03:00
|
|
|
'oss': s.OperationSchemaSerializer(oss.model).data
|
2024-07-19 19:29:27 +03:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
@extend_schema(
|
|
|
|
summary='delete operation',
|
|
|
|
tags=['OSS'],
|
2024-08-15 23:23:45 +03:00
|
|
|
request=s.OperationDeleteSerializer,
|
2024-07-19 19:29:27 +03:00
|
|
|
responses={
|
|
|
|
c.HTTP_200_OK: s.OperationSchemaSerializer,
|
|
|
|
c.HTTP_400_BAD_REQUEST: None,
|
|
|
|
c.HTTP_403_FORBIDDEN: None,
|
|
|
|
c.HTTP_404_NOT_FOUND: None
|
|
|
|
}
|
|
|
|
)
|
|
|
|
@action(detail=True, methods=['patch'], url_path='delete-operation')
|
2024-08-07 21:54:50 +03:00
|
|
|
def delete_operation(self, request: Request, pk) -> HttpResponse:
|
2024-07-19 19:29:27 +03:00
|
|
|
''' Endpoint: Delete operation. '''
|
2024-08-15 23:23:45 +03:00
|
|
|
serializer = s.OperationDeleteSerializer(
|
2024-07-19 19:29:27 +03:00
|
|
|
data=request.data,
|
2024-07-25 19:12:59 +03:00
|
|
|
context={'oss': self.get_object()}
|
2024-07-19 19:29:27 +03:00
|
|
|
)
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
|
2024-07-25 19:12:59 +03:00
|
|
|
oss = m.OperationSchema(self.get_object())
|
2024-08-15 23:23:45 +03:00
|
|
|
operation = cast(m.Operation, serializer.validated_data['target'])
|
2024-09-12 20:56:56 +03:00
|
|
|
old_schema = operation.result
|
2024-07-19 19:29:27 +03:00
|
|
|
with transaction.atomic():
|
2024-07-25 19:12:59 +03:00
|
|
|
oss.update_positions(serializer.validated_data['positions'])
|
2024-08-16 20:57:37 +03:00
|
|
|
oss.delete_operation(operation.pk, serializer.validated_data['keep_constituents'])
|
2024-08-15 23:23:45 +03:00
|
|
|
if old_schema is not None:
|
|
|
|
if serializer.validated_data['delete_schema']:
|
|
|
|
m.PropagationFacade.before_delete_schema(old_schema)
|
|
|
|
old_schema.delete()
|
|
|
|
elif old_schema.is_synced(oss.model):
|
|
|
|
old_schema.visible = True
|
|
|
|
old_schema.save(update_fields=['visible'])
|
2024-07-19 19:29:27 +03:00
|
|
|
return Response(
|
|
|
|
status=c.HTTP_200_OK,
|
2024-07-25 19:12:59 +03:00
|
|
|
data=s.OperationSchemaSerializer(oss.model).data
|
2024-07-19 19:29:27 +03:00
|
|
|
)
|
2024-07-28 00:37:50 +03:00
|
|
|
|
|
|
|
@extend_schema(
|
|
|
|
summary='create input schema for target operation',
|
|
|
|
tags=['OSS'],
|
|
|
|
request=s.OperationTargetSerializer(),
|
|
|
|
responses={
|
|
|
|
c.HTTP_200_OK: s.NewSchemaResponse,
|
|
|
|
c.HTTP_400_BAD_REQUEST: None,
|
|
|
|
c.HTTP_403_FORBIDDEN: None,
|
|
|
|
c.HTTP_404_NOT_FOUND: None
|
|
|
|
}
|
|
|
|
)
|
|
|
|
@action(detail=True, methods=['patch'], url_path='create-input')
|
2024-08-07 21:54:50 +03:00
|
|
|
def create_input(self, request: Request, pk) -> HttpResponse:
|
2024-07-28 00:37:50 +03:00
|
|
|
''' Create new input RSForm. '''
|
|
|
|
serializer = s.OperationTargetSerializer(
|
|
|
|
data=request.data,
|
|
|
|
context={'oss': self.get_object()}
|
|
|
|
)
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
|
|
|
|
operation: m.Operation = cast(m.Operation, serializer.validated_data['target'])
|
|
|
|
if operation.operation_type != m.OperationType.INPUT:
|
|
|
|
raise serializers.ValidationError({
|
|
|
|
'target': msg.operationNotInput(operation.alias)
|
|
|
|
})
|
|
|
|
if operation.result is not None:
|
|
|
|
raise serializers.ValidationError({
|
|
|
|
'target': msg.operationResultNotEmpty(operation.alias)
|
|
|
|
})
|
|
|
|
|
|
|
|
oss = m.OperationSchema(self.get_object())
|
|
|
|
with transaction.atomic():
|
|
|
|
oss.update_positions(serializer.validated_data['positions'])
|
2024-07-31 18:09:31 +03:00
|
|
|
schema = oss.create_input(operation)
|
2024-07-28 00:37:50 +03:00
|
|
|
|
|
|
|
return Response(
|
|
|
|
status=c.HTTP_200_OK,
|
|
|
|
data={
|
2024-07-31 18:09:31 +03:00
|
|
|
'new_schema': LibraryItemSerializer(schema.model).data,
|
2024-07-28 00:37:50 +03:00
|
|
|
'oss': s.OperationSchemaSerializer(oss.model).data
|
|
|
|
}
|
|
|
|
)
|
2024-07-28 21:30:10 +03:00
|
|
|
|
|
|
|
@extend_schema(
|
|
|
|
summary='set input schema for target operation',
|
|
|
|
tags=['OSS'],
|
|
|
|
request=s.SetOperationInputSerializer(),
|
|
|
|
responses={
|
|
|
|
c.HTTP_200_OK: s.OperationSchemaSerializer,
|
|
|
|
c.HTTP_400_BAD_REQUEST: None,
|
|
|
|
c.HTTP_403_FORBIDDEN: None,
|
|
|
|
c.HTTP_404_NOT_FOUND: None
|
|
|
|
}
|
|
|
|
)
|
|
|
|
@action(detail=True, methods=['patch'], url_path='set-input')
|
2024-08-07 21:54:50 +03:00
|
|
|
def set_input(self, request: Request, pk) -> HttpResponse:
|
2024-07-28 21:30:10 +03:00
|
|
|
''' Set input schema for target operation. '''
|
|
|
|
serializer = s.SetOperationInputSerializer(
|
|
|
|
data=request.data,
|
|
|
|
context={'oss': self.get_object()}
|
|
|
|
)
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
|
2024-08-14 23:38:50 +03:00
|
|
|
target_operation: m.Operation = cast(m.Operation, serializer.validated_data['target'])
|
|
|
|
schema: Optional[LibraryItem] = serializer.validated_data['input']
|
|
|
|
if schema is not None:
|
|
|
|
connected_operations = m.Operation.objects.filter(result=schema).only('operation_type', 'oss_id')
|
|
|
|
for operation in connected_operations:
|
|
|
|
if operation.operation_type != m.OperationType.INPUT:
|
|
|
|
raise serializers.ValidationError({
|
|
|
|
'input': msg.operationResultFromAnotherOSS()
|
|
|
|
})
|
|
|
|
if operation != target_operation and operation.oss_id == target_operation.oss_id:
|
|
|
|
raise serializers.ValidationError({
|
|
|
|
'input': msg.operationInputAlreadyConnected()
|
|
|
|
})
|
2024-07-28 21:30:10 +03:00
|
|
|
oss = m.OperationSchema(self.get_object())
|
2024-09-12 20:56:56 +03:00
|
|
|
old_schema = target_operation.result
|
2024-07-28 21:30:10 +03:00
|
|
|
with transaction.atomic():
|
2024-08-15 23:23:45 +03:00
|
|
|
if old_schema is not None:
|
|
|
|
if old_schema.is_synced(oss.model):
|
|
|
|
old_schema.visible = True
|
|
|
|
old_schema.save(update_fields=['visible'])
|
2024-07-28 21:30:10 +03:00
|
|
|
oss.update_positions(serializer.validated_data['positions'])
|
2024-08-14 23:38:50 +03:00
|
|
|
oss.set_input(target_operation.pk, schema)
|
2024-07-28 21:30:10 +03:00
|
|
|
return Response(
|
|
|
|
status=c.HTTP_200_OK,
|
|
|
|
data=s.OperationSchemaSerializer(oss.model).data
|
|
|
|
)
|
2024-07-29 22:31:11 +03:00
|
|
|
|
|
|
|
@extend_schema(
|
|
|
|
summary='update operation',
|
|
|
|
tags=['OSS'],
|
|
|
|
request=s.OperationUpdateSerializer(),
|
|
|
|
responses={
|
|
|
|
c.HTTP_200_OK: s.OperationSchemaSerializer,
|
|
|
|
c.HTTP_400_BAD_REQUEST: None,
|
|
|
|
c.HTTP_403_FORBIDDEN: None,
|
|
|
|
c.HTTP_404_NOT_FOUND: None
|
|
|
|
}
|
|
|
|
)
|
|
|
|
@action(detail=True, methods=['patch'], url_path='update-operation')
|
2024-08-07 21:54:50 +03:00
|
|
|
def update_operation(self, request: Request, pk) -> HttpResponse:
|
2024-07-29 22:31:11 +03:00
|
|
|
''' Update operation arguments and parameters. '''
|
|
|
|
serializer = s.OperationUpdateSerializer(
|
|
|
|
data=request.data,
|
|
|
|
context={'oss': self.get_object()}
|
|
|
|
)
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
|
|
|
|
operation: m.Operation = cast(m.Operation, serializer.validated_data['target'])
|
|
|
|
oss = m.OperationSchema(self.get_object())
|
|
|
|
with transaction.atomic():
|
|
|
|
oss.update_positions(serializer.validated_data['positions'])
|
|
|
|
operation.alias = serializer.validated_data['item_data']['alias']
|
|
|
|
operation.title = serializer.validated_data['item_data']['title']
|
|
|
|
operation.comment = serializer.validated_data['item_data']['comment']
|
2024-08-10 11:41:52 +03:00
|
|
|
operation.save(update_fields=['alias', 'title', 'comment'])
|
2024-07-29 22:31:11 +03:00
|
|
|
|
2024-07-30 16:00:09 +03:00
|
|
|
if operation.result is not None:
|
2024-07-29 22:31:11 +03:00
|
|
|
can_edit = permissions.can_edit_item(request.user, operation.result)
|
2024-08-01 11:56:21 +03:00
|
|
|
if can_edit or operation.operation_type == m.OperationType.SYNTHESIS:
|
2024-07-29 22:31:11 +03:00
|
|
|
operation.result.alias = operation.alias
|
|
|
|
operation.result.title = operation.title
|
|
|
|
operation.result.comment = operation.comment
|
|
|
|
operation.result.save()
|
|
|
|
if 'arguments' in serializer.validated_data:
|
2024-08-16 20:57:37 +03:00
|
|
|
oss.set_arguments(operation.pk, serializer.validated_data['arguments'])
|
2024-07-29 22:31:11 +03:00
|
|
|
if 'substitutions' in serializer.validated_data:
|
2024-08-16 20:57:37 +03:00
|
|
|
oss.set_substitutions(operation.pk, serializer.validated_data['substitutions'])
|
2024-07-29 22:31:11 +03:00
|
|
|
return Response(
|
|
|
|
status=c.HTTP_200_OK,
|
|
|
|
data=s.OperationSchemaSerializer(oss.model).data
|
|
|
|
)
|
|
|
|
|
|
|
|
@extend_schema(
|
|
|
|
summary='execute operation',
|
|
|
|
tags=['OSS'],
|
|
|
|
request=s.OperationTargetSerializer(),
|
|
|
|
responses={
|
|
|
|
c.HTTP_200_OK: s.OperationSchemaSerializer,
|
|
|
|
c.HTTP_400_BAD_REQUEST: None,
|
|
|
|
c.HTTP_403_FORBIDDEN: None,
|
|
|
|
c.HTTP_404_NOT_FOUND: None
|
|
|
|
}
|
|
|
|
)
|
|
|
|
@action(detail=True, methods=['post'], url_path='execute-operation')
|
2024-08-07 21:54:50 +03:00
|
|
|
def execute_operation(self, request: Request, pk) -> HttpResponse:
|
2024-07-29 22:31:11 +03:00
|
|
|
''' Execute operation. '''
|
|
|
|
serializer = s.OperationTargetSerializer(
|
|
|
|
data=request.data,
|
|
|
|
context={'oss': self.get_object()}
|
|
|
|
)
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
|
|
|
|
operation: m.Operation = cast(m.Operation, serializer.validated_data['target'])
|
|
|
|
if operation.operation_type != m.OperationType.SYNTHESIS:
|
|
|
|
raise serializers.ValidationError({
|
|
|
|
'target': msg.operationNotSynthesis(operation.alias)
|
|
|
|
})
|
|
|
|
if operation.result is not None:
|
|
|
|
raise serializers.ValidationError({
|
|
|
|
'target': msg.operationResultNotEmpty(operation.alias)
|
|
|
|
})
|
|
|
|
|
|
|
|
oss = m.OperationSchema(self.get_object())
|
2024-07-31 18:09:31 +03:00
|
|
|
with transaction.atomic():
|
|
|
|
oss.update_positions(serializer.validated_data['positions'])
|
|
|
|
oss.execute_operation(operation)
|
2024-07-29 22:31:11 +03:00
|
|
|
|
|
|
|
return Response(
|
|
|
|
status=c.HTTP_200_OK,
|
|
|
|
data=s.OperationSchemaSerializer(oss.model).data
|
|
|
|
)
|
2024-08-01 21:16:40 +03:00
|
|
|
|
|
|
|
@extend_schema(
|
|
|
|
summary='get predecessor for target constituenta',
|
|
|
|
tags=['OSS'],
|
|
|
|
request=CstTargetSerializer(),
|
|
|
|
responses={
|
|
|
|
c.HTTP_200_OK: s.ConstituentaReferenceResponse,
|
|
|
|
c.HTTP_400_BAD_REQUEST: None,
|
|
|
|
c.HTTP_403_FORBIDDEN: None,
|
|
|
|
c.HTTP_404_NOT_FOUND: None
|
|
|
|
}
|
|
|
|
)
|
|
|
|
@action(detail=False, methods=['post'], url_path='get-predecessor')
|
2024-08-07 21:54:50 +03:00
|
|
|
def get_predecessor(self, request: Request) -> HttpResponse:
|
2024-08-01 21:16:40 +03:00
|
|
|
''' Get predecessor. '''
|
|
|
|
serializer = CstTargetSerializer(data=request.data)
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
cst = cast(Constituenta, serializer.validated_data['target'])
|
2024-09-12 20:56:56 +03:00
|
|
|
inheritance_query = m.Inheritance.objects.filter(child=cst)
|
|
|
|
while inheritance_query.exists():
|
|
|
|
inheritance = inheritance_query.first()
|
|
|
|
if inheritance is None:
|
|
|
|
break
|
|
|
|
cst = inheritance.parent
|
|
|
|
inheritance_query = m.Inheritance.objects.filter(child=cst)
|
2024-08-01 21:16:40 +03:00
|
|
|
|
|
|
|
return Response(
|
|
|
|
status=c.HTTP_200_OK,
|
|
|
|
data={
|
|
|
|
'id': cst.pk,
|
|
|
|
'schema': cst.schema_id
|
|
|
|
}
|
|
|
|
)
|
2024-10-28 14:53:41 +03:00
|
|
|
|
|
|
|
@extend_schema(
|
|
|
|
summary='relocate constituents from one schema to another',
|
|
|
|
tags=['OSS'],
|
|
|
|
request=s.RelocateConstituentsSerializer(),
|
|
|
|
responses={
|
|
|
|
c.HTTP_200_OK: s.OperationSchemaSerializer,
|
|
|
|
c.HTTP_400_BAD_REQUEST: None,
|
|
|
|
c.HTTP_403_FORBIDDEN: None,
|
|
|
|
c.HTTP_404_NOT_FOUND: None
|
|
|
|
}
|
|
|
|
)
|
|
|
|
@action(detail=False, methods=['post'], url_path='relocate-constituents')
|
|
|
|
def relocate_constituents(self, request: Request) -> Response:
|
|
|
|
''' Relocate constituents from one schema to another. '''
|
|
|
|
serializer = s.RelocateConstituentsSerializer(data=request.data)
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
|
|
|
|
data = serializer.validated_data
|
|
|
|
oss = m.OperationSchema(LibraryItem.objects.get(pk=data['oss']))
|
|
|
|
source = RSForm(LibraryItem.objects.get(pk=data['source']))
|
|
|
|
destination = RSForm(LibraryItem.objects.get(pk=data['destination']))
|
|
|
|
|
|
|
|
with transaction.atomic():
|
|
|
|
if data['move_down']:
|
|
|
|
oss.relocate_down(source, destination, data['items'])
|
|
|
|
m.PropagationFacade.before_delete_cst(source, data['items'])
|
|
|
|
source.delete_cst(data['items'])
|
|
|
|
else:
|
|
|
|
new_items = oss.relocate_up(source, destination, data['items'])
|
|
|
|
m.PropagationFacade.after_create_cst(destination, new_items, exclude=[oss.model.pk])
|
|
|
|
|
|
|
|
return Response(status=c.HTTP_200_OK)
|