2026-02-26 01:32:04 +00:00
|
|
|
from django.db.models import Q
|
2026-03-08 12:58:12 +00:00
|
|
|
from rest_framework.exceptions import NotFound, PermissionDenied, ValidationError
|
2026-03-08 13:10:49 +00:00
|
|
|
from rest_framework.parsers import FormParser, MultiPartParser
|
2026-02-26 01:32:04 +00:00
|
|
|
from rest_framework.permissions import IsAuthenticated
|
2026-03-22 09:09:14 +00:00
|
|
|
from rest_framework.decorators import action
|
|
|
|
|
from rest_framework.response import Response
|
2026-02-26 01:32:04 +00:00
|
|
|
from rest_framework.viewsets import ModelViewSet, ReadOnlyModelViewSet
|
|
|
|
|
|
2026-03-15 22:19:12 +00:00
|
|
|
from apps.accounts.models import Organization, Role
|
2026-03-08 12:58:12 +00:00
|
|
|
from apps.accounts.permissions import can_manage_organization
|
2026-03-22 15:35:21 +00:00
|
|
|
from apps.knowledge.models import KnowledgeChunk, TrainingFile
|
|
|
|
|
from apps.knowledge.serializers import KnowledgeChunkSerializer, TrainingFileSerializer
|
2026-03-22 09:09:14 +00:00
|
|
|
from apps.knowledge.tasks import ingest_training_file_task, update_agent_prompts_from_file_task
|
2026-02-26 01:32:04 +00:00
|
|
|
|
|
|
|
|
class TrainingFileViewSet(ModelViewSet):
|
|
|
|
|
queryset = TrainingFile.objects.all()
|
|
|
|
|
serializer_class = TrainingFileSerializer
|
|
|
|
|
permission_classes = [IsAuthenticated]
|
|
|
|
|
parser_classes = [MultiPartParser, FormParser]
|
|
|
|
|
lookup_field = 'uuid'
|
2026-02-27 14:23:26 +00:00
|
|
|
|
2026-02-26 01:32:04 +00:00
|
|
|
def get_queryset(self):
|
|
|
|
|
user = self.request.user
|
2026-03-08 12:58:12 +00:00
|
|
|
queryset = TrainingFile.objects.filter(
|
2026-03-15 22:19:12 +00:00
|
|
|
Q(organization__owner=user) |
|
|
|
|
|
Q(organization__members=user)
|
2026-02-26 01:32:04 +00:00
|
|
|
).distinct()
|
|
|
|
|
|
2026-03-08 12:58:12 +00:00
|
|
|
organization_uuid = self.request.query_params.get('organization_uuid')
|
|
|
|
|
if organization_uuid in (None, ''):
|
|
|
|
|
organization_uuid = self.request.data.get('organization_uuid')
|
|
|
|
|
if organization_uuid:
|
2026-03-15 22:19:12 +00:00
|
|
|
queryset = queryset.filter(organization__uuid=organization_uuid)
|
2026-03-08 12:58:12 +00:00
|
|
|
|
|
|
|
|
role_uuid = self.request.query_params.get('role_uuid')
|
|
|
|
|
if role_uuid in (None, ''):
|
|
|
|
|
role_uuid = self.request.data.get('role_uuid')
|
|
|
|
|
if role_uuid:
|
2026-03-15 22:19:12 +00:00
|
|
|
queryset = queryset.filter(Q(role__uuid=role_uuid) | Q(role__isnull=True))
|
2026-03-08 12:58:12 +00:00
|
|
|
|
|
|
|
|
return queryset
|
|
|
|
|
|
2026-02-26 01:32:04 +00:00
|
|
|
def perform_create(self, serializer):
|
2026-03-08 12:58:12 +00:00
|
|
|
role_uuid = self.request.data.get('role_uuid')
|
2026-03-15 22:19:12 +00:00
|
|
|
organization_uuid = self.request.data.get('organization_uuid')
|
|
|
|
|
|
|
|
|
|
role = None
|
|
|
|
|
organization = None
|
|
|
|
|
|
|
|
|
|
if role_uuid:
|
|
|
|
|
try:
|
|
|
|
|
role = Role.objects.select_related('organization').get(uuid=role_uuid)
|
|
|
|
|
except Role.DoesNotExist:
|
|
|
|
|
raise NotFound('Role not found')
|
|
|
|
|
|
|
|
|
|
organization = role.organization
|
|
|
|
|
|
|
|
|
|
if organization_uuid and str(organization.uuid) != str(organization_uuid):
|
|
|
|
|
raise ValidationError({'organization_uuid': 'organization_uuid does not match role organization.'})
|
|
|
|
|
else:
|
|
|
|
|
if not organization_uuid:
|
|
|
|
|
raise ValidationError({'organization_uuid': 'organization_uuid is required when role_uuid is not provided.'})
|
2026-02-27 13:58:00 +00:00
|
|
|
|
2026-03-15 22:19:12 +00:00
|
|
|
try:
|
|
|
|
|
organization = Organization.objects.get(uuid=organization_uuid)
|
|
|
|
|
except Organization.DoesNotExist:
|
|
|
|
|
raise NotFound('Organization not found')
|
2026-02-27 13:58:00 +00:00
|
|
|
|
2026-03-15 22:19:12 +00:00
|
|
|
if not can_manage_organization(self.request.user, organization):
|
2026-03-08 12:58:12 +00:00
|
|
|
raise PermissionDenied('Permission denied')
|
2026-02-26 01:32:04 +00:00
|
|
|
|
2026-03-08 12:58:12 +00:00
|
|
|
uploaded_file = self.request.FILES.get('file')
|
|
|
|
|
if uploaded_file is None:
|
|
|
|
|
raise ValidationError({'file': 'File is required.'})
|
2026-02-27 13:58:00 +00:00
|
|
|
|
2026-02-26 01:32:04 +00:00
|
|
|
serializer.save(
|
|
|
|
|
uploaded_by=self.request.user,
|
2026-03-15 22:19:12 +00:00
|
|
|
organization=organization,
|
2026-02-26 01:32:04 +00:00
|
|
|
role=role,
|
2026-03-08 12:58:12 +00:00
|
|
|
file_name=uploaded_file.name,
|
|
|
|
|
file_size=uploaded_file.size,
|
|
|
|
|
file_type=uploaded_file.content_type,
|
2026-02-26 01:32:04 +00:00
|
|
|
)
|
|
|
|
|
|
2026-03-22 09:09:14 +00:00
|
|
|
@action(detail=True, methods=['post'], url_path='retry')
|
|
|
|
|
def retry(self, request, *args, **kwargs):
|
|
|
|
|
instance: TrainingFile = self.get_object()
|
|
|
|
|
|
|
|
|
|
if not can_manage_organization(request.user, instance.organization):
|
|
|
|
|
raise PermissionDenied('Permission denied')
|
|
|
|
|
|
|
|
|
|
if instance.status != 'failed':
|
|
|
|
|
raise ValidationError({'status': 'Only failed files can be retried.'})
|
2026-02-27 13:58:00 +00:00
|
|
|
|
2026-03-22 09:09:14 +00:00
|
|
|
instance.status = 'ingesting'
|
2026-03-22 15:35:21 +00:00
|
|
|
instance.save(update_fields=['status'])
|
2026-03-22 09:09:14 +00:00
|
|
|
ingest_training_file_task.delay(str(instance.uuid))
|
|
|
|
|
|
|
|
|
|
serializer = self.get_serializer(instance)
|
|
|
|
|
return Response(serializer.data)
|
|
|
|
|
|
|
|
|
|
def destroy(self, request, *args, **kwargs):
|
|
|
|
|
instance: TrainingFile = self.get_object()
|
2026-02-27 13:58:00 +00:00
|
|
|
|
2026-03-22 09:09:14 +00:00
|
|
|
if not can_manage_organization(request.user, instance.organization):
|
2026-03-08 12:58:12 +00:00
|
|
|
raise PermissionDenied('Permission denied')
|
2026-02-27 13:58:00 +00:00
|
|
|
|
2026-03-18 22:04:07 +00:00
|
|
|
role_uuid = str(instance.role.uuid) if instance.role_id else None
|
|
|
|
|
response = super().destroy(request, *args, **kwargs)
|
|
|
|
|
if role_uuid:
|
|
|
|
|
update_agent_prompts_from_file_task.delay(role_uuid)
|
|
|
|
|
return response
|
2026-02-26 01:32:04 +00:00
|
|
|
|
2026-03-22 15:35:21 +00:00
|
|
|
class KnowledgeChunkViewSet(ReadOnlyModelViewSet):
|
|
|
|
|
queryset = KnowledgeChunk.objects.all()
|
|
|
|
|
serializer_class = KnowledgeChunkSerializer
|
2026-02-26 01:32:04 +00:00
|
|
|
permission_classes = [IsAuthenticated]
|
|
|
|
|
lookup_field = 'uuid'
|
2026-03-22 15:35:21 +00:00
|
|
|
|
2026-02-26 01:32:04 +00:00
|
|
|
def get_queryset(self):
|
|
|
|
|
user = self.request.user
|
2026-03-22 15:35:21 +00:00
|
|
|
queryset = KnowledgeChunk.objects.filter(
|
2026-03-15 22:19:12 +00:00
|
|
|
Q(organization__owner=user) |
|
|
|
|
|
Q(organization__members=user)
|
2026-02-26 01:32:04 +00:00
|
|
|
).distinct()
|
2026-03-08 12:58:12 +00:00
|
|
|
|
|
|
|
|
organization_uuid = self.request.query_params.get('organization_uuid')
|
|
|
|
|
if organization_uuid in (None, ''):
|
|
|
|
|
organization_uuid = self.request.data.get('organization_uuid')
|
|
|
|
|
if organization_uuid:
|
2026-03-15 22:19:12 +00:00
|
|
|
queryset = queryset.filter(organization__uuid=organization_uuid)
|
2026-03-08 12:58:12 +00:00
|
|
|
|
|
|
|
|
role_uuid = self.request.query_params.get('role_uuid')
|
|
|
|
|
if role_uuid in (None, ''):
|
|
|
|
|
role_uuid = self.request.data.get('role_uuid')
|
|
|
|
|
if role_uuid:
|
|
|
|
|
queryset = queryset.filter(role__uuid=role_uuid)
|
|
|
|
|
|
|
|
|
|
return queryset
|