From d2771a2368b1362c7aede7288ccac3783b7f2380 Mon Sep 17 00:00:00 2001 From: Goose Date: Sat, 26 Sep 2026 17:32:45 +0000 Subject: [PATCH] feat(crm, custom_objects): implement Sprint 4.1 dynamic custom fields on standard objects with GIN indexes and strict validation --- ...m_fields_contact_custom_fields_and_more.py | 55 +++++++++++++++++++ crm_core/models.py | 17 ++++++ crm_core/serializers.py | 33 +++++++++-- crm_core/services.py | 8 +++ ...tomfielddefinition_entity_name_and_more.py | 24 ++++++++ custom_objects/models.py | 11 +++- custom_objects/schema_engine.py | 43 ++++++++++++--- 7 files changed, 178 insertions(+), 13 deletions(-) create mode 100644 crm_core/migrations/0002_account_custom_fields_contact_custom_fields_and_more.py create mode 100644 custom_objects/migrations/0002_customfielddefinition_entity_name_and_more.py diff --git a/crm_core/migrations/0002_account_custom_fields_contact_custom_fields_and_more.py b/crm_core/migrations/0002_account_custom_fields_contact_custom_fields_and_more.py new file mode 100644 index 0000000..7c7e2d4 --- /dev/null +++ b/crm_core/migrations/0002_account_custom_fields_contact_custom_fields_and_more.py @@ -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'), + ), + ] diff --git a/crm_core/models.py b/crm_core/models.py index 9e4c3b8..6d70043 100644 --- a/crm_core/models.py +++ b/crm_core/models.py @@ -3,6 +3,7 @@ from django.db import models from django.contrib.auth import get_user_model from django.contrib.contenttypes.fields import GenericForeignKey from django.contrib.contenttypes.models import ContentType +from django.contrib.postgres.indexes import GinIndex from django.core.exceptions import ValidationError 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') + custom_fields = models.JSONField(default=dict, blank=True) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) class Meta: ordering = ['-created_at'] + indexes = [ + GinIndex(fields=['custom_fields'], name='idx_account_custom_fields_gin'), + ] def __str__(self): 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') + custom_fields = models.JSONField(default=dict, blank=True) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) class Meta: ordering = ['-created_at'] + indexes = [ + GinIndex(fields=['custom_fields'], name='idx_contact_custom_fields_gin'), + ] def __str__(self): 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' ) + custom_fields = models.JSONField(default=dict, blank=True) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) class Meta: ordering = ['-created_at'] + indexes = [ + GinIndex(fields=['custom_fields'], name='idx_lead_custom_fields_gin'), + ] def __str__(self): 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') + custom_fields = models.JSONField(default=dict, blank=True) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) class Meta: ordering = ['-close_date'] + indexes = [ + GinIndex(fields=['custom_fields'], name='idx_opp_custom_fields_gin'), + ] def clean(self): super().clean() diff --git a/crm_core/serializers.py b/crm_core/serializers.py index d4a45e6..c4ec783 100644 --- a/crm_core/serializers.py +++ b/crm_core/serializers.py @@ -1,22 +1,45 @@ from rest_framework import serializers 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: model = Account fields = '__all__' read_only_fields = ['id', 'created_at', 'updated_at'] -class ContactSerializer(serializers.ModelSerializer): +class ContactSerializer(CustomFieldsValidationMixin, serializers.ModelSerializer): + entity_name = 'Contact' + class Meta: model = Contact fields = '__all__' read_only_fields = ['id', 'created_at', 'updated_at'] -class LeadSerializer(serializers.ModelSerializer): +class LeadSerializer(CustomFieldsValidationMixin, serializers.ModelSerializer): + entity_name = 'Lead' + class Meta: model = Lead fields = '__all__' @@ -27,7 +50,9 @@ class LeadSerializer(serializers.ModelSerializer): ] -class OpportunitySerializer(serializers.ModelSerializer): +class OpportunitySerializer(CustomFieldsValidationMixin, serializers.ModelSerializer): + entity_name = 'Opportunity' + class Meta: model = Opportunity fields = '__all__' diff --git a/crm_core/services.py b/crm_core/services.py index a5ea488..deb76ce 100644 --- a/crm_core/services.py +++ b/crm_core/services.py @@ -46,6 +46,7 @@ def convert_lead( contact = Contact.objects.filter(email=lead.email).first() if not contact: + contact_custom_fields = dict(lead.custom_fields) if lead.custom_fields else {} contact = Contact.objects.create( first_name=lead.first_name, last_name=lead.last_name, @@ -54,7 +55,14 @@ def convert_lead( email=lead.email, phone=lead.phone, 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 opportunity = None diff --git a/custom_objects/migrations/0002_customfielddefinition_entity_name_and_more.py b/custom_objects/migrations/0002_customfielddefinition_entity_name_and_more.py new file mode 100644 index 0000000..c3ce237 --- /dev/null +++ b/custom_objects/migrations/0002_customfielddefinition_entity_name_and_more.py @@ -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'), + ), + ] diff --git a/custom_objects/models.py b/custom_objects/models.py index c888d5a..e02554e 100644 --- a/custom_objects/models.py +++ b/custom_objects/models.py @@ -71,8 +71,10 @@ class CustomFieldDefinition(models.Model): ] 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]) label = models.CharField(max_length=100) field_type = models.CharField(max_length=20, choices=FIELD_TYPE_CHOICES) @@ -94,6 +96,10 @@ class CustomFieldDefinition(models.Model): 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: @@ -102,7 +108,8 @@ class CustomFieldDefinition(models.Model): raise ValidationError({'target_object': 'Lookup fields require a target_object.'}) 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): diff --git a/custom_objects/schema_engine.py b/custom_objects/schema_engine.py index 8c681ac..5810bdd 100644 --- a/custom_objects/schema_engine.py +++ b/custom_objects/schema_engine.py @@ -13,15 +13,11 @@ FIELD_TYPE_TO_JSONSCHEMA = { } -def build_schema_for_object(custom_object, partial=False): - """ - Dynamically generates JSONSchema Draft 7 representation of CustomObjectDefinition fields. - """ +def build_schema_for_fields(field_queryset, title="Object", partial=False): properties = {} required = [] - fields = custom_object.fields.all() - for field in fields: + for field in field_queryset: f_type = field.field_type if f_type in FIELD_TYPE_TO_JSONSCHEMA: field_def = dict(FIELD_TYPE_TO_JSONSCHEMA[f_type]) @@ -40,7 +36,7 @@ def build_schema_for_object(custom_object, partial=False): schema = { '$schema': 'http://json-schema.org/draft-07/schema#', - 'title': custom_object.label, + 'title': title, 'type': 'object', 'properties': properties, 'additionalProperties': False, @@ -51,6 +47,39 @@ def build_schema_for_object(custom_object, partial=False): 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): """ Validates record payload against the dynamic schema