Fixing imports
This commit is contained in:
1 parent
8d37b2ad67
commit
3ebe9a0894
6 files changed
+316
No files matched your search
@@ -0,0 +1,57 @@
|
||||
"""Irreversible bulk-delete of all domain (parts/taxonomy/fit) data.
|
||||
|
||||
Leaves users, roles/permissions, refresh tokens, the audit log, and the
|
||||
ImportedPartConfig (OData connection settings) untouched.
|
||||
"""
|
||||
|
||||
from sqlalchemy import delete
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.models.associations import augmented_part_imported_parts
|
||||
from app.models.attachment_point import AttachmentPoint
|
||||
from app.models.attachment_point_name import AttachmentPointName
|
||||
from app.models.augmented_part import AugmentedPart, AugmentedPartNote
|
||||
from app.models.clamp_connection import ClampConnection
|
||||
from app.models.fit import Fit, FitNote
|
||||
from app.models.imported_part import ImportedPart
|
||||
from app.models.iso_thread import IsoThread
|
||||
from app.models.manufacturer import Manufacturer
|
||||
from app.models.part_colour import PartColour
|
||||
from app.models.part_type import PartType
|
||||
from app.models.scope_info import ScopeInfo
|
||||
from app.models.scope_model import ScopeModel
|
||||
from app.models.static_connection import StaticConnection
|
||||
from app.models.sub_part_type import SubPartType
|
||||
from app.models.unc_thread import UncThread
|
||||
|
||||
# Children before the RESTRICT-protected reference tables they point to.
|
||||
_TARGETS = [
|
||||
("Fit notes", FitNote),
|
||||
("Fits", Fit),
|
||||
("Augmented part notes", AugmentedPartNote),
|
||||
("Scope info", ScopeInfo),
|
||||
("Attachment points", AttachmentPoint),
|
||||
("Augmented part / imported part links", augmented_part_imported_parts),
|
||||
("Augmented parts", AugmentedPart),
|
||||
("Imported parts", ImportedPart),
|
||||
("Scope models", ScopeModel),
|
||||
("Attachment point names", AttachmentPointName),
|
||||
("ISO threads", IsoThread),
|
||||
("UNC threads", UncThread),
|
||||
("Clamp connections", ClampConnection),
|
||||
("Static connections", StaticConnection),
|
||||
("Manufacturers", Manufacturer),
|
||||
]
|
||||
|
||||
|
||||
async def purge_domain_data(db: AsyncSession) -> dict[str, int]:
|
||||
"""Delete every row from every domain table, in FK-safe order.
|
||||
|
||||
Returns a label -> rows-deleted map for reporting/audit purposes.
|
||||
"""
|
||||
counts: dict[str, int] = {}
|
||||
for label, target in _TARGETS:
|
||||
result = await db.execute(delete(target))
|
||||
counts[label] = result.rowcount or 0
|
||||
await db.commit()
|
||||
return counts
|
||||
@@ -145,6 +145,9 @@
|
||||
{% if su or "audit_log:read" in p %}
|
||||
<a href="/admin/audit-log">{{ icon("list-checks") }} Audit Log</a>
|
||||
{% endif %}
|
||||
{% if su %}
|
||||
<a href="/admin/danger-zone">{{ icon("alert-circle") }} Danger Zone</a>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
{% if total %}
|
||||
<p style="color:var(--color-danger,#c00)">
|
||||
Purge complete: {{ total }} row(s) deleted.
|
||||
</p>
|
||||
<ul class="muted">
|
||||
{% for label, n in counts.items() %}
|
||||
{% if n %}
|
||||
<li>{{ label }}: {{ n }}</li>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{% else %}
|
||||
<p class="muted">Nothing to purge — the database was already clean.</p>
|
||||
{% endif %}
|
||||
@@ -0,0 +1,32 @@
|
||||
{% extends "base.html" %}
|
||||
{% block title %}Danger Zone · {{ app_name }}{% endblock %}
|
||||
{% block body %}
|
||||
{% include "_nav.html" %}
|
||||
<main class="container">
|
||||
<h1>Danger Zone</h1>
|
||||
|
||||
<div class="card" style="border: 1px solid var(--color-danger, #c00)">
|
||||
<h2>Purge database</h2>
|
||||
<p>
|
||||
Permanently deletes every part, taxonomy entry, attachment point, fit,
|
||||
and imported part in the database — manufacturers, part colours, part
|
||||
types, sub part types, scope models, ISO/UNC threads, clamp/static
|
||||
connections, attachment point names, augmented parts, scope info,
|
||||
attachment points, fits, fit notes, and imported parts.
|
||||
</p>
|
||||
<p>
|
||||
<strong>Not</strong> deleted: users, roles & permissions, refresh
|
||||
tokens, the audit log, and the OData connection configuration.
|
||||
</p>
|
||||
<p class="muted">This cannot be undone. Take a database backup first if you might need this data again.</p>
|
||||
<button class="btn btn-danger"
|
||||
hx-post="/admin/danger-zone/purge"
|
||||
hx-target="#purge-result"
|
||||
hx-swap="innerHTML"
|
||||
hx-confirm="This will PERMANENTLY delete all parts, taxonomy, attachment points, fits, and imported parts. Users and the OData configuration are kept. This cannot be undone. Continue?">
|
||||
Purge all part/fit data
|
||||
</button>
|
||||
<div id="purge-result" style="margin-top:1rem"></div>
|
||||
</div>
|
||||
</main>
|
||||
{% endblock %}
|
||||
@@ -43,6 +43,7 @@ from app.models.fit import FitLabel
|
||||
from app.models.unc_thread import UncSeries
|
||||
from app.models.user import User
|
||||
from app.services import augmented_part_sync as augmented_part_sync_service
|
||||
from app.services import danger_zone as danger_zone_service
|
||||
from app.services import fit_matching as fit_matching_service
|
||||
from app.services import jobs
|
||||
from app.services import odata as odata_service
|
||||
@@ -131,6 +132,16 @@ def web_require(*perms: str):
|
||||
return checker
|
||||
|
||||
|
||||
async def web_require_superuser(user: User = Depends(get_current_web_user)) -> User:
|
||||
"""Web dependency: superusers only, no permission-string bypass."""
|
||||
if not user.is_superuser:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="You do not have access to this page.",
|
||||
)
|
||||
return user
|
||||
|
||||
|
||||
async def _role_perm_ctx(db: AsyncSession) -> dict:
|
||||
all_perms = await role_crud.list_permissions(db)
|
||||
scopes = sorted({p.name.split(":")[0] for p in all_perms})
|
||||
@@ -3598,3 +3609,45 @@ async def audit_log_page(
|
||||
"admin/audit_log.html",
|
||||
{"user": user, "entries": await audit_crud.list_recent(db)},
|
||||
)
|
||||
|
||||
|
||||
# --- Danger Zone ---------------------------------------------------------------
|
||||
|
||||
|
||||
@router.get("/danger-zone", response_class=HTMLResponse)
|
||||
async def danger_zone_page(
|
||||
request: Request,
|
||||
user: User = Depends(web_require_superuser),
|
||||
):
|
||||
return templates.TemplateResponse(
|
||||
request,
|
||||
"admin/danger_zone.html",
|
||||
{"user": user},
|
||||
)
|
||||
|
||||
|
||||
@router.post("/danger-zone/purge", response_class=HTMLResponse)
|
||||
async def purge_database(
|
||||
request: Request,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
actor: User = Depends(web_require_superuser),
|
||||
):
|
||||
counts = await danger_zone_service.purge_domain_data(db)
|
||||
total = sum(counts.values())
|
||||
breakdown = ", ".join(f"{label} {n}" for label, n in counts.items() if n)
|
||||
await audit_crud.log(
|
||||
db,
|
||||
actor=actor,
|
||||
action="delete",
|
||||
resource_type="Database",
|
||||
resource_id=None,
|
||||
description=(
|
||||
f"Purged all domain data via Danger Zone ({total} row(s) deleted"
|
||||
+ (f": {breakdown})" if breakdown else ")")
|
||||
),
|
||||
)
|
||||
return templates.TemplateResponse(
|
||||
request,
|
||||
"admin/_danger_zone_result.html",
|
||||
{"total": total, "counts": counts},
|
||||
)
|
||||
Reference in new issue
Block a user