42 lines
1.6 KiB
Python
42 lines
1.6 KiB
Python
from django.contrib.contenttypes.fields import GenericForeignKey
|
|
from django.contrib.contenttypes.models import ContentType
|
|
from django.db import models
|
|
|
|
|
|
class ExternalMapping(models.Model):
|
|
"""Maps internal entities to provider-specific IDs across namespaces."""
|
|
|
|
provider_namespace = models.CharField(max_length=80)
|
|
external_id = models.CharField(max_length=160)
|
|
external_secondary_id = models.CharField(max_length=160, blank=True)
|
|
|
|
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
|
|
object_id = models.PositiveBigIntegerField()
|
|
content_object = GenericForeignKey("content_type", "object_id")
|
|
|
|
# Raw payload is kept only for troubleshooting and parsing diagnostics.
|
|
raw_payload = models.JSONField(default=dict, blank=True)
|
|
last_seen_at = models.DateTimeField(auto_now=True)
|
|
is_active = models.BooleanField(default=True)
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
|
|
|
class Meta:
|
|
constraints = [
|
|
models.UniqueConstraint(
|
|
fields=["provider_namespace", "external_id"],
|
|
name="uq_mapping_provider_external_id",
|
|
),
|
|
models.UniqueConstraint(
|
|
fields=["provider_namespace", "content_type", "object_id"],
|
|
name="uq_mapping_provider_entity",
|
|
),
|
|
]
|
|
indexes = [
|
|
models.Index(fields=["provider_namespace", "external_id"]),
|
|
models.Index(fields=["content_type", "object_id"]),
|
|
models.Index(fields=["is_active"]),
|
|
]
|
|
|
|
def __str__(self) -> str:
|
|
return f"{self.provider_namespace}:{self.external_id}"
|