infrasynth-backend-kit/infrasynth/features/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

55 lines
2 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 FeatureFlag(GlobalOrTenantModel):
"""An operational toggle. ``tenant IS NULL`` is the global default; a tenant row overrides it."""
slug = models.SlugField(max_length=100)
name = models.CharField(max_length=200)
description = models.TextField(blank=True)
is_active = models.BooleanField(default=False)
rollout_percentage = models.PositiveSmallIntegerField(default=100, help_text="0-100. 100 = all users")
environments = models.JSONField(default=list, help_text='["production", "staging"] or [] = all')
category = models.CharField(max_length=50, blank=True, help_text="Grouping for UI")
metadata = models.JSONField(default=dict)
class Meta:
db_table = "features_flag"
constraints = [
models.UniqueConstraint(fields=["tenant", "slug"], name="uniq_feature_flag_slug_per_tenant"),
models.UniqueConstraint(
fields=["slug"],
condition=Q(tenant__isnull=True),
name="uniq_global_feature_flag_slug",
),
]
class FeatureFlagOverride(TenantOwnedModel):
flag = models.ForeignKey(FeatureFlag, on_delete=models.CASCADE, related_name="overrides")
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
null=True,
blank=True,
related_name="+",
)
group = models.ForeignKey(
"auth.Group",
on_delete=models.CASCADE,
null=True,
blank=True,
related_name="+",
)
is_enabled = models.BooleanField()
class Meta:
db_table = "features_override"
constraints = [
models.UniqueConstraint(fields=["flag", "user"], name="uniq_override_flag_user"),
models.UniqueConstraint(fields=["flag", "group"], name="uniq_override_flag_group"),
]