feat(schema): add model introspection API and integrate unified Fields & Relationships in Object Manager
Some checks failed
Build, Test, and Deploy / build-and-deploy (push) Failing after 36s

This commit is contained in:
Goose 2026-09-27 04:17:57 +00:00
parent d73b435ec6
commit bb038fc8de
6 changed files with 296 additions and 10 deletions

91
crm_core/introspection.py Normal file
View file

@ -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

38
crm_core/schema_views.py Normal file
View file

@ -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)

View file

@ -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()

View file

@ -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 }})
</button>
<button
v-if="!selectedObj.is_standard"
@ -97,26 +97,40 @@
<th class="py-2.5 px-4">Field Label</th>
<th class="py-2.5 px-4">API Field Name</th>
<th class="py-2.5 px-4">Data Type</th>
<th class="py-2.5 px-4">Origin</th>
<th class="py-2.5 px-4">Required</th>
</tr>
</thead>
<tbody class="divide-y divide-slate-100">
<tr v-for="field in crmStore.customFields" :key="field.id" class="hover:bg-slate-50/50">
<tr v-for="field in crmStore.allObjectFields" :key="field.id || field.name" class="hover:bg-slate-50/50">
<td class="py-2.5 px-4 font-medium text-slate-800">{{ field.label }}</td>
<td class="py-2.5 px-4 font-mono text-slate-500">{{ field.name }}</td>
<td class="py-2.5 px-4">
<span class="bg-slate-100 text-slate-600 px-2 py-0.5 rounded font-mono text-[10px] uppercase">
{{ field.field_type }}
<span :class="[
'px-2 py-0.5 rounded font-mono text-[10px] uppercase font-medium',
field.data_type === 'Currency' ? 'bg-emerald-50 text-emerald-700 border border-emerald-200' :
field.data_type === 'Lookup' ? 'bg-purple-50 text-purple-700 border border-purple-200' :
field.data_type === 'Picklist' ? 'bg-blue-50 text-blue-700 border border-blue-200' :
field.data_type === 'Checkbox' ? 'bg-amber-50 text-amber-700 border border-amber-200' :
field.data_type === 'AutoNumber' ? 'bg-slate-200 text-slate-700 font-bold' :
'bg-slate-100 text-slate-600'
]">
{{ field.data_type }}
<span v-if="field.related_to" class="normal-case font-sans text-purple-600">({{ field.related_to }})</span>
</span>
</td>
<td class="py-2.5 px-4">
<span v-if="field.is_custom" class="bg-indigo-100 text-indigo-700 px-1.5 py-0.5 rounded text-[10px] font-semibold">Custom</span>
<span v-else class="bg-slate-100 text-slate-500 px-1.5 py-0.5 rounded text-[10px] font-medium">Standard</span>
</td>
<td class="py-2.5 px-4">
<span :class="field.is_required ? 'text-amber-600 font-bold' : 'text-slate-400'">
{{ field.is_required ? 'Required' : 'Optional' }}
</span>
</td>
</tr>
<tr v-if="crmStore.customFields.length === 0">
<td colspan="4" class="py-8 text-center text-slate-400 italic">No custom fields defined on this object yet.</td>
<tr v-if="crmStore.allObjectFields.length === 0">
<td colspan="5" class="py-8 text-center text-slate-400 italic">No fields defined on this object yet.</td>
</tr>
</tbody>
</table>
@ -331,9 +345,9 @@ async function selectObject(obj) {
selectedObj.value = obj
activeSubTab.value = 'fields'
if (obj.is_standard) {
await crmStore.fetchCustomFields(obj.id)
await crmStore.fetchObjectSchema(obj.id, true)
} else {
await crmStore.fetchCustomFields(obj.id)
await crmStore.fetchObjectSchema(obj.id, false)
await crmStore.fetchCustomRecords(obj.api_name)
}
}
@ -367,9 +381,9 @@ async function createCustomField() {
newField.value = { label: '', name: '', field_type: 'text', is_required: false }
if (selectedObj.value.is_standard) {
await crmStore.fetchCustomFields(selectedObj.value.id)
await crmStore.fetchObjectSchema(selectedObj.value.id, true)
} else {
await crmStore.fetchCustomFields(selectedObj.value.id)
await crmStore.fetchObjectSchema(selectedObj.value.id, false)
}
}

View file

@ -7,6 +7,7 @@ from crm_core.views import (
AccountViewSet, ContactViewSet, LeadViewSet,
OpportunityViewSet, ActivityViewSet
)
from crm_core.schema_views import StandardObjectFieldsView
from custom_objects.views import (
CustomObjectDefinitionViewSet, CustomFieldDefinitionViewSet,
CustomRecordViewSet
@ -33,6 +34,7 @@ router.register(r'campaigns', CampaignViewSet, basename='campaign')
urlpatterns = [
path('admin/', admin.site.urls),
path('api/schema/objects/<str:entity_name>/fields/', StandardObjectFieldsView.as_view(), name='standard_object_fields'),
path('api/', include(router.urls)),
path('api/track/<uuid:token>/', TrackEmailOpenView.as_view(), name='track_email_open'),
path('api/outreach/track/open/<uuid:token>/', TrackEmailOpenView.as_view(), name='track_email_open_alt'),

View file

@ -0,0 +1,82 @@
import pytest
from django.contrib.auth import get_user_model
from rest_framework.test import APIClient
from crm_core.introspection import introspect_standard_fields, STANDARD_OBJECT_REGISTRY
User = get_user_model()
@pytest.mark.django_db
class TestSchemaIntrospection:
def setup_method(self):
self.user = User.objects.create_user(username="introspect_user", password="password")
self.client = APIClient()
self.client.force_authenticate(user=self.user)
def test_introspect_standard_fields_service(self):
"""Test introspect_standard_fields returns valid DTO for all standard models."""
for model_name in STANDARD_OBJECT_REGISTRY:
fields = introspect_standard_fields(model_name)
assert isinstance(fields, list)
assert len(fields) > 0
field_names = [f["name"] for f in fields]
assert "id" in field_names
# Internal/forbidden fields must be excluded
assert "password" not in field_names
assert "custom_fields" not in field_names
assert "is_superuser" not in field_names
for field in fields:
assert "name" in field
assert "label" in field
assert "data_type" in field
assert "is_custom" in field
assert field["is_custom"] is False
assert "is_required" in field
assert "is_unique" in field
assert "is_indexed" in field
assert "choices" in field
assert "editable" in field
def test_introspect_type_mapping(self):
"""Test proper mapping to Salesforce-style data types."""
account_fields = {f["name"]: f for f in introspect_standard_fields("Account")}
assert account_fields["id"]["data_type"] == "AutoNumber"
assert account_fields["name"]["data_type"] == "Text"
assert account_fields["type"]["data_type"] == "Picklist"
assert len(account_fields["type"]["choices"]) > 0
assert account_fields["annual_revenue"]["data_type"] == "Currency"
opp_fields = {f["name"]: f for f in introspect_standard_fields("Opportunity")}
assert opp_fields["account"]["data_type"] == "Lookup"
assert opp_fields["account"]["related_to"] == "Account"
assert opp_fields["stage"]["data_type"] == "Picklist"
assert "Closed Won" in opp_fields["stage"]["choices"]
def test_schema_endpoint_authenticated(self):
"""Test GET /api/schema/objects/{entity}/fields/ endpoint returns 200 with standard fields."""
for entity in ["Account", "Contact", "Lead", "Opportunity", "Activity", "Campaign"]:
res = self.client.get(f"/api/schema/objects/{entity}/fields/", HTTP_HOST="localhost")
assert res.status_code == 200
assert res.data["entity_name"] == entity
assert res.data["is_custom"] is False
assert len(res.data["standard_fields"]) > 0
def test_schema_endpoint_case_insensitive(self):
"""Test endpoint handles lowercase or mixed case entity names."""
res = self.client.get("/api/schema/objects/account/fields/", HTTP_HOST="localhost")
assert res.status_code == 200
assert res.data["entity_name"] == "Account"
def test_schema_endpoint_unsupported_object(self):
"""Test endpoint returns 404 for unknown/unsupported object."""
res = self.client.get("/api/schema/objects/NonExistent/fields/", HTTP_HOST="localhost")
assert res.status_code == 404
assert "error" in res.data
def test_schema_endpoint_unauthenticated(self):
"""Test endpoint requires authentication."""
anon_client = APIClient()
res = anon_client.get("/api/schema/objects/Account/fields/", HTTP_HOST="localhost")
assert res.status_code == 403