infrasynth-backend-kit/infrasynth/files/models.py
jcv-dev 551b42eab5 feat: production-hardening pass across the kit
Close the gaps between the documented contract (API-STANDARD, TENANCY,
ENTITLEMENTS) and the implementation, and remove committed build artifacts.

Security:
- verify + process inbound webhooks (HMAC/handler verify, size limit,
  timestamp tolerance, idempotency via InboundEvent.external_id)
- real 2FA login flow (pre-auth challenge; tokens only after verify/recovery)
- wire HybridPermission into security/audit views; add API-key rotate and
  users/<id>/permissions|roles endpoints
- tenant-scoped throttling on by default; webhook replay protection
- verify MercadoPago webhook signatures
- login brute-force guard, configurable password policy, real ALTCHA PoW

Correctness:
- apply verified billing webhooks idempotently (subscription/entitlement/
  invoice/PaymentTransaction); scheduled payment lifecycle jobs
- capture audit update diffs automatically; add audit retention purge
- working notification retries, per-channel rate limits, log retention
- pluggable virus scanner, upload-size limit, pipeline toggle
- feature rollout %/environment targeting; settings-driven registrations
- workflow guards (instance cap, route depth, self-assignment, clone on re-entry)
- wire every previously-dead INFRASYNTH_* setting; drop truly dead ones

Delivery:
- README + CHANGELOG; CI format check + coverage gate
- keep test media out of the tree; untrack .coverage, __pycache__,
  egg-info, docs/ and invoice artifacts
2026-09-24 10:41:21 -05:00

102 lines
3.4 KiB
Python

from django.conf import settings
from django.db import models
from django.db.models import Q
from infrasynth.tenancy.mixins import GlobalOrTenantModel, TenantOwnedModel
class StoredFile(TenantOwnedModel):
storage_backend = models.CharField(max_length=50)
storage_key = models.CharField(max_length=500)
original_filename = models.CharField(max_length=500)
mime_type = models.CharField(max_length=255)
size_bytes = models.BigIntegerField()
checksum_sha256 = models.CharField(max_length=64)
is_public = models.BooleanField(default=False)
category = models.ForeignKey(
"FileCategory",
on_delete=models.SET_NULL,
null=True,
blank=True,
related_name="+",
)
metadata = models.JSONField(default=dict)
uploaded_by = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.SET_NULL,
null=True,
blank=True,
related_name="+",
)
created_at = models.DateTimeField(auto_now_add=True)
class Meta:
db_table = "files_stored_file"
indexes = [models.Index(fields=["tenant_id", "created_at"])]
class FileCategory(TenantOwnedModel):
slug = models.SlugField(max_length=100)
name = models.CharField(max_length=200)
description = models.TextField(blank=True)
storage_path = models.CharField(max_length=500)
allowed_extensions = models.TextField(blank=True)
max_size_bytes = models.BigIntegerField(null=True, blank=True)
is_active = models.BooleanField(default=True)
storage_backend_override = models.CharField(max_length=50, blank=True)
class Meta:
db_table = "files_category"
constraints = [
models.UniqueConstraint(fields=["tenant", "slug"], name="uniq_file_category_slug_per_tenant"),
]
class ProcessingPipeline(GlobalOrTenantModel):
name = models.CharField(max_length=200)
slug = models.SlugField(max_length=100)
steps = models.JSONField()
is_active = models.BooleanField(default=True)
class Meta:
db_table = "files_pipeline"
constraints = [
models.UniqueConstraint(fields=["tenant", "slug"], name="uniq_pipeline_slug_per_tenant"),
models.UniqueConstraint(
fields=["slug"],
condition=Q(tenant__isnull=True),
name="uniq_global_pipeline_slug",
),
]
class PipelineExecution(TenantOwnedModel):
class Status(models.TextChoices):
PENDING = "pending", "Pending"
RUNNING = "running", "Running"
COMPLETED = "completed", "Completed"
FAILED = "failed", "Failed"
file = models.ForeignKey(StoredFile, on_delete=models.CASCADE, related_name="pipeline_executions")
pipeline = models.ForeignKey(
ProcessingPipeline,
on_delete=models.SET_NULL,
null=True,
blank=True,
related_name="+",
)
status = models.CharField(max_length=20, choices=Status.choices, default=Status.PENDING)
started_at = models.DateTimeField(null=True, blank=True)
completed_at = models.DateTimeField(null=True, blank=True)
output_file = models.ForeignKey(
StoredFile,
on_delete=models.SET_NULL,
null=True,
blank=True,
related_name="+",
)
error = models.TextField(blank=True)
class Meta:
db_table = "files_pipeline_execution"
indexes = [models.Index(fields=["tenant_id", "status"])]