Source code for ferrosoft.apps.ferrobase.tables.models

#  Copyright (c) 2025 Ferrosoft GmbH. All rights reserved.
from django.urls import reverse

from ferrosoft.apps.ferrobase.models import (
    User,
    IdSequenceConfig,
    Organization,
    Tenant,
    SubscriptionPlan,
    StoredFile,
    FeatureAssignment,
    APIToken,
)
from ferrosoft.apps.ferrobase.tables import EmptyExpandColumn, NumberColumn
from ferrosoft.apps.ferrobase.tables.crud import CRUDTable
from ferrosoft.apps.ferrobase.tables.columns import Column
from ferrosoft.apps.ferrobase.tables.table import Table
from ferrosoft.decimal import zero


[docs] class APITokenTable(Table): """Read-only listing of ``APIToken`` records (name, tenant, status, expiry)."""
[docs] class Meta: model = APIToken fields = [ "name", "tenant", "status", "expiration", ]
[docs] class FeatureAssignmentTable(CRUDTable): """CRUD table for ``FeatureAssignment``; renders ``0`` limits as an em-dash."""
[docs] class Meta(CRUDTable.Meta): model = FeatureAssignment fields = [ "actions", "feature", "limit", ]
empty = EmptyExpandColumn() limit = NumberColumn(transform=lambda value: "—" if value == zero else value)
[docs] def get_delete_action_url(self, *args, **kwargs) -> str | None: """Resolve the delete URL via the ``featureassignment_delete`` route.""" return reverse( "ferrobase:featureassignment_delete", args=[kwargs.get("record").pk] )
[docs] class IDSequenceTable(Table): """Listing of ID-sequence configurations linkified on the ``name`` column."""
[docs] class Meta: model = IdSequenceConfig fields = [ "name", "prefix", "padding_amount", ]
name = Column(linkify=True)
[docs] class OrganizationTable(Table): """Listing of ``Organization`` records linkified on the ``name`` column."""
[docs] class Meta: model = Organization fields = [ "name", "contact_email", "contact_first_name", "contact_last_name", ]
name = Column(linkify=True)
[docs] class StoredFileTable(Table): """Listing of stored files linking each name to its update view."""
[docs] class Meta: model = StoredFile fields = [ "name", "owner", "media_type", "creation_time", "expire_on", ]
name = Column( linkify=lambda record: reverse("ferrobase:storedfile_update", args=[record.pk]) )
[docs] class SubscriptionPlanTable(Table): """Listing of subscription plans linkified on the ``name`` column."""
[docs] class Meta: model = SubscriptionPlan fields = [ "name", "max_tonnage", "enable_all_features", ]
name = Column(linkify=True)
[docs] class TenantTable(CRUDTable): """CRUD table for tenants nested under an organisation."""
[docs] class Meta(CRUDTable.Meta): model = Tenant fields = [ "actions", "name", "active", "expiry_date", ]
empty = EmptyExpandColumn()
[docs] def get_delete_action_url(self, *args, **kwargs) -> str | None: """Resolve the delete URL via the ``tenant_delete`` route.""" return reverse("ferrobase:tenant_delete", args=[kwargs.get("record").pk])
[docs] class UserTable(Table): """Listing of users linkified on the ``username`` column."""
[docs] class Meta: model = User fields = [ "username", "is_active", "first_name", "last_name", "email", "organization", "date_joined", ]
username = Column(linkify=True)