Some checks failed
Build, Test, and Deploy / build-and-deploy (push) Failing after 29s
- Add zero-trust AST-based formula engine in custom_objects/formula_engine.py supporting arithmetic, logical, string, and date operations. - Enforce strict AST whitelisting and attribute/function safety checks. - Add cycle detection and topological sorting of formula dependencies. - Extend CustomFieldDefinition model with formula_expression and formula_return_type fields. - Wire pre-save signals to evaluate formula fields across standard models and CustomRecord. - Expose AST syntax validator endpoint at POST /api/custom-fields/validate-formula/. - Add comprehensive test suite in tests/test_formula_engine.py with 100% pass rate.
113 lines
4.4 KiB
Python
113 lines
4.4 KiB
Python
from django.db import models, transaction
|
|
from rest_framework import viewsets, status
|
|
from rest_framework.decorators import action
|
|
from rest_framework.response import Response
|
|
from rest_framework.permissions import IsAuthenticated
|
|
from rest_framework.exceptions import NotFound, PermissionDenied
|
|
|
|
from .models import CustomObjectDefinition, CustomFieldDefinition, CustomRecord
|
|
from .serializers import (
|
|
CustomObjectDefinitionSerializer,
|
|
CustomFieldDefinitionSerializer,
|
|
CustomRecordSerializer
|
|
)
|
|
from openforce.permissions import ScopedOwnerAccessPermission
|
|
|
|
|
|
class CustomObjectDefinitionViewSet(viewsets.ModelViewSet):
|
|
permission_classes = [ScopedOwnerAccessPermission]
|
|
queryset = CustomObjectDefinition.objects.all()
|
|
serializer_class = CustomObjectDefinitionSerializer
|
|
|
|
def get_queryset(self):
|
|
qs = super().get_queryset()
|
|
user = self.request.user
|
|
if not user or not user.is_authenticated:
|
|
return qs.none()
|
|
if user.is_superuser or user.is_staff:
|
|
return qs
|
|
return qs.filter(created_by=user)
|
|
|
|
def perform_create(self, serializer):
|
|
with transaction.atomic():
|
|
serializer.save(created_by=self.request.user)
|
|
|
|
|
|
class CustomFieldDefinitionViewSet(viewsets.ModelViewSet):
|
|
permission_classes = [ScopedOwnerAccessPermission]
|
|
queryset = CustomFieldDefinition.objects.all()
|
|
serializer_class = CustomFieldDefinitionSerializer
|
|
|
|
def get_queryset(self):
|
|
qs = super().get_queryset()
|
|
user = self.request.user
|
|
if not user or not user.is_authenticated:
|
|
return qs.none()
|
|
if not (user.is_superuser or user.is_staff):
|
|
qs = qs.filter(
|
|
models.Q(custom_object__created_by=user) | models.Q(entity_name__isnull=False)
|
|
)
|
|
|
|
custom_object_id = self.request.query_params.get('custom_object')
|
|
if custom_object_id:
|
|
qs = qs.filter(custom_object_id=custom_object_id)
|
|
|
|
entity_name = self.request.query_params.get('entity_name')
|
|
if entity_name:
|
|
qs = qs.filter(entity_name=entity_name)
|
|
|
|
return qs
|
|
|
|
def perform_create(self, serializer):
|
|
with transaction.atomic():
|
|
# Validate that if custom_object is supplied, user owns it
|
|
obj = serializer.validated_data.get('custom_object')
|
|
user = self.request.user
|
|
if obj and not (user.is_superuser or user.is_staff):
|
|
if obj.created_by and obj.created_by != user:
|
|
raise PermissionDenied("Cannot add fields to another user's custom object.")
|
|
serializer.save()
|
|
|
|
@action(detail=False, methods=['post'], url_path='validate-formula')
|
|
def validate_formula(self, request):
|
|
expr = request.data.get('formula_expression') or request.data.get('expression', '')
|
|
if not expr:
|
|
return Response({'valid': False, 'error': 'Expression is required.'}, status=status.HTTP_400_BAD_REQUEST)
|
|
from custom_objects.formula_engine import validate_formula_syntax, extract_formula_identifiers
|
|
try:
|
|
validate_formula_syntax(expr)
|
|
identifiers = extract_formula_identifiers(expr)
|
|
return Response({'valid': True, 'identifiers': list(identifiers)}, status=status.HTTP_200_OK)
|
|
except Exception as e:
|
|
return Response({'valid': False, 'error': str(e)}, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
|
|
class CustomRecordViewSet(viewsets.ModelViewSet):
|
|
permission_classes = [IsAuthenticated, ScopedOwnerAccessPermission]
|
|
serializer_class = CustomRecordSerializer
|
|
|
|
def get_custom_object(self):
|
|
obj_name = self.kwargs.get('object_name')
|
|
obj = CustomObjectDefinition.objects.filter(
|
|
models.Q(name=obj_name) | models.Q(api_name=obj_name)
|
|
).first()
|
|
if not obj:
|
|
raise NotFound(f"Custom object '{obj_name}' not found.")
|
|
return obj
|
|
|
|
def get_queryset(self):
|
|
custom_object = self.get_custom_object()
|
|
user = self.request.user
|
|
qs = CustomRecord.objects.filter(custom_object=custom_object)
|
|
if user.is_superuser or user.is_staff:
|
|
return qs
|
|
return qs.filter(owner=user)
|
|
|
|
def perform_create(self, serializer):
|
|
with transaction.atomic():
|
|
custom_object = self.get_custom_object()
|
|
serializer.save(custom_object=custom_object, owner=self.request.user)
|
|
|
|
def perform_update(self, serializer):
|
|
with transaction.atomic():
|
|
serializer.save()
|