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.
153 lines
6.4 KiB
Python
153 lines
6.4 KiB
Python
import re
|
|
from django.db import models
|
|
from django.contrib.auth import get_user_model
|
|
from django.contrib.postgres.indexes import GinIndex
|
|
from django.core.exceptions import ValidationError
|
|
|
|
User = get_user_model()
|
|
|
|
RESERVED_NAMES = {
|
|
'id', 'pk', 'user', 'created_at', 'updated_at', 'account', 'contact',
|
|
'lead', 'opportunity', 'activity', 'select', 'where', 'from', 'table',
|
|
'drop', 'delete', 'insert', 'update'
|
|
}
|
|
|
|
|
|
def validate_api_name(value):
|
|
if not re.match(r'^[a-z][a-z0-9_]*$', value):
|
|
raise ValidationError(
|
|
f"'{value}' is not a valid API name. Must start with lowercase letter and contain only alphanumeric and underscores."
|
|
)
|
|
clean_val = value[:-3] if value.endswith('__c') else value
|
|
if clean_val.lower() in RESERVED_NAMES:
|
|
raise ValidationError(f"'{value}' is a reserved system keyword.")
|
|
|
|
|
|
class CustomObjectDefinition(models.Model):
|
|
name = models.CharField(max_length=100, unique=True, validators=[validate_api_name])
|
|
api_name = models.CharField(max_length=100, unique=True, blank=True, validators=[validate_api_name])
|
|
label = models.CharField(max_length=100)
|
|
plural_label = models.CharField(max_length=100, blank=True, default='')
|
|
description = models.TextField(blank=True, default='')
|
|
|
|
created_by = models.ForeignKey(User, null=True, blank=True, on_delete=models.SET_NULL)
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
|
updated_at = models.DateTimeField(auto_now=True)
|
|
|
|
def clean(self):
|
|
super().clean()
|
|
if self.name and not self.name.endswith('__c'):
|
|
self.name = f"{self.name}__c"
|
|
if self.api_name and not self.api_name.endswith('__c'):
|
|
self.api_name = f"{self.api_name}__c"
|
|
if not self.api_name and self.name:
|
|
self.api_name = self.name
|
|
if not self.name and self.api_name:
|
|
self.name = self.api_name
|
|
|
|
def save(self, *args, **kwargs):
|
|
if not self.api_name and self.name:
|
|
self.api_name = self.name
|
|
if not self.name and self.api_name:
|
|
self.name = self.api_name
|
|
super().save(*args, **kwargs)
|
|
|
|
def __str__(self):
|
|
return f"{self.label} ({self.name})"
|
|
|
|
|
|
class CustomFieldDefinition(models.Model):
|
|
FIELD_TYPE_CHOICES = [
|
|
('text', 'Text'),
|
|
('number', 'Number'),
|
|
('boolean', 'Boolean'),
|
|
('date', 'Date'),
|
|
('datetime', 'DateTime'),
|
|
('email', 'Email'),
|
|
('url', 'URL'),
|
|
('select', 'Picklist (Select)'),
|
|
('picklist', 'Picklist'),
|
|
('lookup', 'Lookup (Relationship)'),
|
|
('formula', 'Formula (Calculated)'),
|
|
('rollup', 'Roll-Up Summary'),
|
|
]
|
|
|
|
custom_object = models.ForeignKey(
|
|
CustomObjectDefinition, on_delete=models.CASCADE, related_name='fields',
|
|
null=True, blank=True
|
|
)
|
|
entity_name = models.CharField(max_length=100, null=True, blank=True)
|
|
name = models.CharField(max_length=100, validators=[validate_api_name])
|
|
label = models.CharField(max_length=100)
|
|
field_type = models.CharField(max_length=20, choices=FIELD_TYPE_CHOICES)
|
|
is_required = models.BooleanField(default=False)
|
|
is_unique = models.BooleanField(default=False)
|
|
default_value = models.JSONField(null=True, blank=True)
|
|
choices = models.JSONField(null=True, blank=True, help_text="List of choices for 'select' type")
|
|
options = models.JSONField(null=True, blank=True, help_text="List of choices for picklist")
|
|
target_object = models.ForeignKey(
|
|
CustomObjectDefinition, null=True, blank=True, on_delete=models.SET_NULL,
|
|
related_name='referencing_fields', help_text="Target for 'lookup' type"
|
|
)
|
|
|
|
# Formula & Roll-up summary configuration
|
|
formula_expression = models.TextField(null=True, blank=True, help_text="AST expression for formula calculations")
|
|
formula_return_type = models.CharField(max_length=20, default='text', null=True, blank=True)
|
|
rollup_summary_object = models.CharField(max_length=100, null=True, blank=True)
|
|
rollup_field = models.CharField(max_length=100, null=True, blank=True)
|
|
rollup_type = models.CharField(max_length=20, null=True, blank=True)
|
|
rollup_filter = models.JSONField(null=True, blank=True)
|
|
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
|
updated_at = models.DateTimeField(auto_now=True)
|
|
|
|
class Meta:
|
|
unique_together = [['custom_object', 'name']]
|
|
|
|
def clean(self):
|
|
super().clean()
|
|
if (self.custom_object and self.entity_name) or (not self.custom_object and not self.entity_name):
|
|
raise ValidationError(
|
|
"CustomFieldDefinition must have EITHER custom_object OR entity_name, not both, not neither."
|
|
)
|
|
if self.name and not self.name.endswith('__c'):
|
|
self.name = f"{self.name}__c"
|
|
if self.field_type == 'select' and not self.choices:
|
|
raise ValidationError({'choices': 'Select fields require a list of choices.'})
|
|
if self.field_type == 'lookup' and not self.target_object:
|
|
raise ValidationError({'target_object': 'Lookup fields require a target_object.'})
|
|
if self.field_type == 'formula':
|
|
if not self.formula_expression:
|
|
raise ValidationError({'formula_expression': 'Formula fields require a formula_expression.'})
|
|
from custom_objects.formula_engine import validate_formula_syntax
|
|
validate_formula_syntax(self.formula_expression)
|
|
|
|
def __str__(self):
|
|
parent_name = self.custom_object.name if self.custom_object else self.entity_name
|
|
return f"{parent_name}.{self.name} ({self.field_type})"
|
|
|
|
|
|
class CustomRecord(models.Model):
|
|
custom_object = models.ForeignKey(
|
|
CustomObjectDefinition, on_delete=models.CASCADE, related_name='records'
|
|
)
|
|
data = models.JSONField(default=dict, blank=True)
|
|
owner = models.ForeignKey(User, null=True, blank=True, on_delete=models.SET_NULL)
|
|
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
|
updated_at = models.DateTimeField(auto_now=True)
|
|
|
|
class Meta:
|
|
db_table = 'custom_record'
|
|
indexes = [
|
|
GinIndex(fields=['data'], name='idx_custom_record_data_gin'),
|
|
]
|
|
ordering = ['-created_at']
|
|
|
|
def clean(self):
|
|
super().clean()
|
|
from .schema_engine import validate_record_data
|
|
validate_record_data(self.custom_object, self.data)
|
|
|
|
def __str__(self):
|
|
return f"{self.custom_object.name} #{self.id}"
|