feat(crm, custom_objects): implement Sprint 4.1 dynamic custom fields on standard objects with GIN indexes and strict validation

This commit is contained in:
Goose 2026-09-26 17:32:45 +00:00
parent eaaf3f2791
commit d2771a2368
7 changed files with 178 additions and 13 deletions

View file

@ -0,0 +1,55 @@
# Generated by Django 4.2.30 on 2026-09-26 17:32
import django.contrib.postgres.indexes
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('crm_core', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='account',
name='custom_fields',
field=models.JSONField(blank=True, default=dict),
),
migrations.AddField(
model_name='contact',
name='custom_fields',
field=models.JSONField(blank=True, default=dict),
),
migrations.AddField(
model_name='lead',
name='custom_fields',
field=models.JSONField(blank=True, default=dict),
),
migrations.AddField(
model_name='opportunity',
name='custom_fields',
field=models.JSONField(blank=True, default=dict),
),
migrations.AlterField(
model_name='opportunity',
name='stage',
field=models.CharField(choices=[('Prospecting', 'Prospecting'), ('Qualification', 'Qualification'), ('Needs Analysis', 'Needs Analysis'), ('Value Proposition', 'Value Proposition'), ('Id. Decision Makers', 'Id. Decision Makers'), ('Decision Makers', 'Decision Makers'), ('Perception Analysis', 'Perception Analysis'), ('Proposal/Price Quote', 'Proposal/Price Quote'), ('Negotiation/Review', 'Negotiation/Review'), ('Closed Won', 'Closed Won'), ('Closed Lost', 'Closed Lost')], default='Prospecting', max_length=50),
),
migrations.AddIndex(
model_name='account',
index=django.contrib.postgres.indexes.GinIndex(fields=['custom_fields'], name='idx_account_custom_fields_gin'),
),
migrations.AddIndex(
model_name='contact',
index=django.contrib.postgres.indexes.GinIndex(fields=['custom_fields'], name='idx_contact_custom_fields_gin'),
),
migrations.AddIndex(
model_name='lead',
index=django.contrib.postgres.indexes.GinIndex(fields=['custom_fields'], name='idx_lead_custom_fields_gin'),
),
migrations.AddIndex(
model_name='opportunity',
index=django.contrib.postgres.indexes.GinIndex(fields=['custom_fields'], name='idx_opp_custom_fields_gin'),
),
]

View file

@ -3,6 +3,7 @@ from django.db import models
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from django.contrib.contenttypes.fields import GenericForeignKey from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.contrib.postgres.indexes import GinIndex
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
User = get_user_model() User = get_user_model()
@ -66,11 +67,15 @@ class Account(models.Model):
) )
owner = models.ForeignKey(User, null=True, blank=True, on_delete=models.SET_NULL, related_name='accounts') owner = models.ForeignKey(User, null=True, blank=True, on_delete=models.SET_NULL, related_name='accounts')
custom_fields = models.JSONField(default=dict, blank=True)
created_at = models.DateTimeField(auto_now_add=True) created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True) updated_at = models.DateTimeField(auto_now=True)
class Meta: class Meta:
ordering = ['-created_at'] ordering = ['-created_at']
indexes = [
GinIndex(fields=['custom_fields'], name='idx_account_custom_fields_gin'),
]
def __str__(self): def __str__(self):
return self.name return self.name
@ -99,11 +104,15 @@ class Contact(models.Model):
) )
owner = models.ForeignKey(User, null=True, blank=True, on_delete=models.SET_NULL, related_name='contacts') owner = models.ForeignKey(User, null=True, blank=True, on_delete=models.SET_NULL, related_name='contacts')
custom_fields = models.JSONField(default=dict, blank=True)
created_at = models.DateTimeField(auto_now_add=True) created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True) updated_at = models.DateTimeField(auto_now=True)
class Meta: class Meta:
ordering = ['-created_at'] ordering = ['-created_at']
indexes = [
GinIndex(fields=['custom_fields'], name='idx_contact_custom_fields_gin'),
]
def __str__(self): def __str__(self):
return f"{self.first_name} {self.last_name} ({self.email})".strip() return f"{self.first_name} {self.last_name} ({self.email})".strip()
@ -147,11 +156,15 @@ class Lead(models.Model):
'Opportunity', null=True, blank=True, on_delete=models.SET_NULL, related_name='converted_leads' 'Opportunity', null=True, blank=True, on_delete=models.SET_NULL, related_name='converted_leads'
) )
custom_fields = models.JSONField(default=dict, blank=True)
created_at = models.DateTimeField(auto_now_add=True) created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True) updated_at = models.DateTimeField(auto_now=True)
class Meta: class Meta:
ordering = ['-created_at'] ordering = ['-created_at']
indexes = [
GinIndex(fields=['custom_fields'], name='idx_lead_custom_fields_gin'),
]
def __str__(self): def __str__(self):
return f"{self.first_name} {self.last_name} - {self.company}".strip() return f"{self.first_name} {self.last_name} - {self.company}".strip()
@ -206,11 +219,15 @@ class Opportunity(models.Model):
) )
owner = models.ForeignKey(User, null=True, blank=True, on_delete=models.SET_NULL, related_name='opportunities') owner = models.ForeignKey(User, null=True, blank=True, on_delete=models.SET_NULL, related_name='opportunities')
custom_fields = models.JSONField(default=dict, blank=True)
created_at = models.DateTimeField(auto_now_add=True) created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True) updated_at = models.DateTimeField(auto_now=True)
class Meta: class Meta:
ordering = ['-close_date'] ordering = ['-close_date']
indexes = [
GinIndex(fields=['custom_fields'], name='idx_opp_custom_fields_gin'),
]
def clean(self): def clean(self):
super().clean() super().clean()

View file

@ -1,22 +1,45 @@
from rest_framework import serializers from rest_framework import serializers
from .models import Account, Contact, Lead, Opportunity, Activity from .models import Account, Contact, Lead, Opportunity, Activity
from custom_objects.schema_engine import validate_entity_custom_fields
class AccountSerializer(serializers.ModelSerializer): class CustomFieldsValidationMixin:
entity_name = None
def validate_custom_fields(self, value):
if value:
validate_entity_custom_fields(self.entity_name, value, partial=self.partial)
return value
def validate(self, attrs):
attrs = super().validate(attrs)
custom_fields = attrs.get('custom_fields')
if custom_fields is not None:
validate_entity_custom_fields(self.entity_name, custom_fields, partial=self.partial)
return attrs
class AccountSerializer(CustomFieldsValidationMixin, serializers.ModelSerializer):
entity_name = 'Account'
class Meta: class Meta:
model = Account model = Account
fields = '__all__' fields = '__all__'
read_only_fields = ['id', 'created_at', 'updated_at'] read_only_fields = ['id', 'created_at', 'updated_at']
class ContactSerializer(serializers.ModelSerializer): class ContactSerializer(CustomFieldsValidationMixin, serializers.ModelSerializer):
entity_name = 'Contact'
class Meta: class Meta:
model = Contact model = Contact
fields = '__all__' fields = '__all__'
read_only_fields = ['id', 'created_at', 'updated_at'] read_only_fields = ['id', 'created_at', 'updated_at']
class LeadSerializer(serializers.ModelSerializer): class LeadSerializer(CustomFieldsValidationMixin, serializers.ModelSerializer):
entity_name = 'Lead'
class Meta: class Meta:
model = Lead model = Lead
fields = '__all__' fields = '__all__'
@ -27,7 +50,9 @@ class LeadSerializer(serializers.ModelSerializer):
] ]
class OpportunitySerializer(serializers.ModelSerializer): class OpportunitySerializer(CustomFieldsValidationMixin, serializers.ModelSerializer):
entity_name = 'Opportunity'
class Meta: class Meta:
model = Opportunity model = Opportunity
fields = '__all__' fields = '__all__'

View file

@ -46,6 +46,7 @@ def convert_lead(
contact = Contact.objects.filter(email=lead.email).first() contact = Contact.objects.filter(email=lead.email).first()
if not contact: if not contact:
contact_custom_fields = dict(lead.custom_fields) if lead.custom_fields else {}
contact = Contact.objects.create( contact = Contact.objects.create(
first_name=lead.first_name, first_name=lead.first_name,
last_name=lead.last_name, last_name=lead.last_name,
@ -54,7 +55,14 @@ def convert_lead(
email=lead.email, email=lead.email,
phone=lead.phone, phone=lead.phone,
owner=owner, owner=owner,
custom_fields=contact_custom_fields,
) )
elif lead.custom_fields:
# Merge lead custom fields into contact
merged = dict(contact.custom_fields or {})
merged.update(lead.custom_fields)
contact.custom_fields = merged
contact.save(update_fields=['custom_fields'])
# 3. Optionally Create Opportunity # 3. Optionally Create Opportunity
opportunity = None opportunity = None

View file

@ -0,0 +1,24 @@
# Generated by Django 4.2.30 on 2026-09-26 17:32
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('custom_objects', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='customfielddefinition',
name='entity_name',
field=models.CharField(blank=True, max_length=100, null=True),
),
migrations.AlterField(
model_name='customfielddefinition',
name='custom_object',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='fields', to='custom_objects.customobjectdefinition'),
),
]

View file

@ -71,8 +71,10 @@ class CustomFieldDefinition(models.Model):
] ]
custom_object = models.ForeignKey( custom_object = models.ForeignKey(
CustomObjectDefinition, on_delete=models.CASCADE, related_name='fields' 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]) name = models.CharField(max_length=100, validators=[validate_api_name])
label = models.CharField(max_length=100) label = models.CharField(max_length=100)
field_type = models.CharField(max_length=20, choices=FIELD_TYPE_CHOICES) field_type = models.CharField(max_length=20, choices=FIELD_TYPE_CHOICES)
@ -94,6 +96,10 @@ class CustomFieldDefinition(models.Model):
def clean(self): def clean(self):
super().clean() 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'): if self.name and not self.name.endswith('__c'):
self.name = f"{self.name}__c" self.name = f"{self.name}__c"
if self.field_type == 'select' and not self.choices: if self.field_type == 'select' and not self.choices:
@ -102,7 +108,8 @@ class CustomFieldDefinition(models.Model):
raise ValidationError({'target_object': 'Lookup fields require a target_object.'}) raise ValidationError({'target_object': 'Lookup fields require a target_object.'})
def __str__(self): def __str__(self):
return f"{self.custom_object.name}.{self.name} ({self.field_type})" 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): class CustomRecord(models.Model):

View file

@ -13,15 +13,11 @@ FIELD_TYPE_TO_JSONSCHEMA = {
} }
def build_schema_for_object(custom_object, partial=False): def build_schema_for_fields(field_queryset, title="Object", partial=False):
"""
Dynamically generates JSONSchema Draft 7 representation of CustomObjectDefinition fields.
"""
properties = {} properties = {}
required = [] required = []
fields = custom_object.fields.all() for field in field_queryset:
for field in fields:
f_type = field.field_type f_type = field.field_type
if f_type in FIELD_TYPE_TO_JSONSCHEMA: if f_type in FIELD_TYPE_TO_JSONSCHEMA:
field_def = dict(FIELD_TYPE_TO_JSONSCHEMA[f_type]) field_def = dict(FIELD_TYPE_TO_JSONSCHEMA[f_type])
@ -40,7 +36,7 @@ def build_schema_for_object(custom_object, partial=False):
schema = { schema = {
'$schema': 'http://json-schema.org/draft-07/schema#', '$schema': 'http://json-schema.org/draft-07/schema#',
'title': custom_object.label, 'title': title,
'type': 'object', 'type': 'object',
'properties': properties, 'properties': properties,
'additionalProperties': False, 'additionalProperties': False,
@ -51,6 +47,39 @@ def build_schema_for_object(custom_object, partial=False):
return schema return schema
def build_schema_for_object(custom_object, partial=False):
"""
Dynamically generates JSONSchema Draft 7 representation of CustomObjectDefinition fields.
"""
return build_schema_for_fields(custom_object.fields.all(), title=custom_object.label, partial=partial)
def build_schema_for_entity(entity_name, partial=False):
"""
Dynamically generates JSONSchema Draft 7 representation of custom fields for a standard entity.
"""
from custom_objects.models import CustomFieldDefinition
fields = CustomFieldDefinition.objects.filter(entity_name=entity_name)
return build_schema_for_fields(fields, title=entity_name, partial=partial)
def validate_entity_custom_fields(entity_name, data, partial=False):
"""
Validates custom_fields dictionary for standard entity (e.g. Account, Contact, Lead, Opportunity).
"""
if not isinstance(data, dict):
raise ValidationError({'custom_fields': 'Must be an object.'})
schema = build_schema_for_entity(entity_name, partial=partial)
validator = Draft7Validator(schema)
errors = {}
for err in sorted(validator.iter_errors(data), key=lambda e: e.path):
field = err.path[0] if err.path else 'non_field_errors'
errors.setdefault(field, []).append(err.message)
if errors:
raise ValidationError(errors)
return True
def validate_record_data(custom_object, data, partial=False): def validate_record_data(custom_object, data, partial=False):
""" """
Validates record payload against the dynamic schema Validates record payload against the dynamic schema