From bb038fc8de3a77e49add8f6e79e673703e5de589 Mon Sep 17 00:00:00 2001 From: Goose Date: Sun, 27 Sep 2026 04:17:57 +0000 Subject: [PATCH] feat(schema): add model introspection API and integrate unified Fields & Relationships in Object Manager --- crm_core/introspection.py | 91 ++++++++++++++++++++++++ crm_core/schema_views.py | 38 ++++++++++ frontend/src/stores/crm.js | 59 +++++++++++++++ frontend/src/views/CustomObjectsView.vue | 34 ++++++--- openforce/urls.py | 2 + tests/test_introspection.py | 82 +++++++++++++++++++++ 6 files changed, 296 insertions(+), 10 deletions(-) create mode 100644 crm_core/introspection.py create mode 100644 crm_core/schema_views.py create mode 100644 tests/test_introspection.py diff --git a/crm_core/introspection.py b/crm_core/introspection.py new file mode 100644 index 0000000..272cc71 --- /dev/null +++ b/crm_core/introspection.py @@ -0,0 +1,91 @@ +from django.apps import apps +from django.db import models + +STANDARD_OBJECT_REGISTRY = { + "Account": ("crm_core", "Account"), + "Contact": ("crm_core", "Contact"), + "Lead": ("crm_core", "Lead"), + "Opportunity": ("crm_core", "Opportunity"), + "Activity": ("crm_core", "Activity"), + "Campaign": ("outreach", "Campaign"), +} + +EXCLUDED_FIELDS = { + "custom_fields", + "password", + "is_superuser", + "is_staff", +} + +DJANGO_TYPE_MAP = { + "CharField": "Text", + "TextField": "Text Area (Long)", + "IntegerField": "Number", + "PositiveIntegerField": "Number", + "BigAutoField": "AutoNumber", + "AutoField": "AutoNumber", + "DecimalField": "Currency", + "BooleanField": "Checkbox", + "DateField": "Date", + "DateTimeField": "Date/Time", + "ForeignKey": "Lookup", + "OneToOneField": "Lookup", + "GenericForeignKey": "Generic Lookup", + "EmailField": "Email", + "URLField": "URL", +} + + +def map_field_type(field): + if getattr(field, "choices", None): + return "Picklist" + internal_type = field.get_internal_type() if hasattr(field, "get_internal_type") else type(field).__name__ + return DJANGO_TYPE_MAP.get(internal_type, internal_type) + + +def introspect_standard_fields(model_name: str): + # Normalize model_name case + normalized_name = None + for reg_name in STANDARD_OBJECT_REGISTRY: + if reg_name.lower() == model_name.lower(): + normalized_name = reg_name + break + + if not normalized_name: + raise ValueError(f"Unknown standard object: {model_name}") + + app_label, cls_name = STANDARD_OBJECT_REGISTRY[normalized_name] + model = apps.get_model(app_label, cls_name) + + fields = [] + for field in model._meta.get_fields(): + # Exclude reverse relations + if field.auto_created and not field.concrete: + continue + if field.name in EXCLUDED_FIELDS: + continue + + data_type = map_field_type(field) + choices = [c[0] for c in field.choices] if getattr(field, "choices", None) else [] + related_to = None + if getattr(field, "is_relation", False) and getattr(field, "related_model", None): + related_to = field.related_model.__name__ + + verbose_name = getattr(field, "verbose_name", field.name) + label = str(verbose_name).replace("_", " ").title() if verbose_name else field.name.title() + + fields.append({ + "name": field.name, + "label": label, + "data_type": data_type, + "is_custom": False, + "is_required": not getattr(field, "blank", True) and not getattr(field, "null", True), + "is_unique": getattr(field, "unique", False), + "is_indexed": getattr(field, "db_index", False) or getattr(field, "primary_key", False) or getattr(field, "is_relation", False), + "help_text": str(getattr(field, "help_text", "")), + "choices": choices, + "related_to": related_to, + "max_length": getattr(field, "max_length", None), + "editable": field.name not in ["id", "created_at", "updated_at"], + }) + return fields diff --git a/crm_core/schema_views.py b/crm_core/schema_views.py new file mode 100644 index 0000000..f6f3638 --- /dev/null +++ b/crm_core/schema_views.py @@ -0,0 +1,38 @@ +from rest_framework.views import APIView +from rest_framework.response import Response +from rest_framework import status +from rest_framework.permissions import IsAuthenticated + +from .introspection import introspect_standard_fields, STANDARD_OBJECT_REGISTRY + + +class StandardObjectFieldsView(APIView): + permission_classes = [IsAuthenticated] + + def get(self, request, entity_name=None): + if not entity_name: + return Response({"error": "entity_name parameter is required"}, status=status.HTTP_400_BAD_REQUEST) + + # Case-insensitive lookup against registry + matched_name = None + for key in STANDARD_OBJECT_REGISTRY: + if key.lower() == entity_name.lower(): + matched_name = key + break + + if not matched_name: + return Response( + {"error": f"Standard object '{entity_name}' not found. Supported objects: {list(STANDARD_OBJECT_REGISTRY.keys())}"}, + status=status.HTTP_404_NOT_FOUND + ) + + try: + fields = introspect_standard_fields(matched_name) + return Response({ + "entity_name": matched_name, + "label": matched_name, + "is_custom": False, + "standard_fields": fields + }, status=status.HTTP_200_OK) + except Exception as e: + return Response({"error": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) diff --git a/frontend/src/stores/crm.js b/frontend/src/stores/crm.js index a6b80b6..c3a865e 100644 --- a/frontend/src/stores/crm.js +++ b/frontend/src/stores/crm.js @@ -12,6 +12,8 @@ export const useCrmStore = defineStore('crm', { activities: [], customObjects: [], customFields: [], + standardFields: {}, + allObjectFields: [], customRecords: {}, flows: [], campaigns: [], @@ -123,6 +125,63 @@ export const useCrmStore = defineStore('crm', { return [] } }, + async fetchObjectSchema(entityNameOrId, isCustom = false) { + try { + let standard = [] + let custom = [] + + if (!isCustom) { + const entityName = typeof entityNameOrId === 'string' ? entityNameOrId : entityNameOrId.name + try { + const schemaRes = await api.get(`/schema/objects/${entityName}/fields/`) + standard = (schemaRes.data.standard_fields || []).map(f => ({ + ...f, + is_custom: false, + origin: 'Standard' + })) + this.standardFields[entityName] = standard + } catch (e) { + console.warn(`Could not load standard schema for ${entityName}:`, e) + } + + custom = await this.fetchCustomFields(entityName) + } else { + const customObjId = typeof entityNameOrId === 'number' ? entityNameOrId : entityNameOrId.id + custom = await this.fetchCustomFields(customObjId) + + // Standard audit fields for custom objects + standard = [ + { name: 'id', label: 'Record ID', data_type: 'AutoNumber', is_custom: false, is_required: true, is_unique: true, is_indexed: true, origin: 'Standard', editable: false }, + { name: 'name', label: 'Record Name', data_type: 'Text', is_custom: false, is_required: true, is_unique: false, is_indexed: true, origin: 'Standard', editable: true }, + { name: 'owner', label: 'Owner', data_type: 'Lookup', related_to: 'User', is_custom: false, is_required: false, is_unique: false, is_indexed: true, origin: 'Standard', editable: false }, + { name: 'created_at', label: 'Created Date', data_type: 'Date/Time', is_custom: false, is_required: false, is_unique: false, is_indexed: false, origin: 'Standard', editable: false }, + { name: 'updated_at', label: 'Modified Date', data_type: 'Date/Time', is_custom: false, is_required: false, is_unique: false, is_indexed: false, origin: 'Standard', editable: false }, + ] + } + + const normalizedCustom = (custom || []).map(f => ({ + name: f.name, + label: f.label || f.name, + data_type: (f.field_type || 'Text').charAt(0).toUpperCase() + (f.field_type || 'Text').slice(1), + is_custom: true, + is_required: !!f.is_required, + is_unique: !!f.is_unique, + is_indexed: !!f.is_unique, + choices: f.options || f.choices || [], + origin: 'Custom', + editable: true, + id: f.id, + raw: f + })) + + this.allObjectFields = [...standard, ...normalizedCustom] + return this.allObjectFields + } catch (err) { + console.error('Error in fetchObjectSchema:', err) + this.allObjectFields = [] + return [] + } + }, async createCustomField(fieldData) { const res = await api.post('/custom-fields/', fieldData) await this.fetchCustomObjects() diff --git a/frontend/src/views/CustomObjectsView.vue b/frontend/src/views/CustomObjectsView.vue index 10ffd7e..06b8505 100644 --- a/frontend/src/views/CustomObjectsView.vue +++ b/frontend/src/views/CustomObjectsView.vue @@ -78,7 +78,7 @@ @click="activeSubTab = 'fields'" :class="['pb-2 text-xs font-bold uppercase tracking-wider transition border-b-2', activeSubTab === 'fields' ? 'border-brand-600 text-brand-600' : 'border-transparent text-slate-400 hover:text-slate-600']"> - Custom Fields ({{ crmStore.customFields.length }}) + Fields & Relationships ({{ crmStore.allObjectFields.length }})