Driver rename and icons: `Driver.display_name` plus a `DriverIcon` table, both global/shared like the `Driver` row they hang off, so a rename or an icon is what every Owner sees. The rename/icon dialog keeps its forms as siblings (nested forms are invalid HTML) and the icon routes return an `hx-swap-oob` thumbnail refresh rather than re-rendering the table, which would tear the open `<dialog>` out of the DOM. Web image picker: `GET /web/images` renders a pickable grid for a printer or a driver icon, with the search term prefilled from the entity name and editable. Picking one downloads it server-side and normalizes it. Driver download search: `GET /web/drivers` searches for a vendor-wide driver (the term is rewritten into the vendor's real product name for 15 brands) or for the exact model as typed. Links only — nothing is downloaded, and the fragment says the results are unvetted. Icon uploads no longer reject off-size or non-PNG files: `normalize_icon()` letterboxes any decodable raster into a 256x256 PNG. An already-exact 256x256 PNG is returned byte-identical, because icon storage is content-addressed and re-encoding would move the file on every save. `fetch_image()` makes the request from the server, so `assert_fetchable()` refuses any URL resolving to a private, loopback, or link-local address, and re-runs on every redirect. ImpTune sits on the same LAN as the printers it configures; an unguarded fetcher would be a port scanner for anyone who can reach the UI. DuckDuckGo is scraped, not called through an API — no key needed, but fragile, so both search functions swallow parse failures and return [] instead of 500ing a page. `WEB_SEARCH=false` disables every outbound request and hides the controls, for air-gapped installs. Also: one shared `Jinja2Templates` in `templating.py` instead of five per-router instances, so a template global is declared once; `_add_missing_columns()` in `database.py` adds new nullable columns to a pre-existing table, which `create_tables(safe=True)` skips; `db_env` in test_db.py now closes its connection on teardown, or the next test's ORM writes land in the previous test's DB file. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
123 lines
3.4 KiB
Python
123 lines
3.4 KiB
Python
"""Peewee ORM models — full schema for phases 1-5."""
|
|
from datetime import UTC, datetime
|
|
|
|
|
|
def _utcnow():
|
|
return datetime.now(UTC).replace(tzinfo=None)
|
|
|
|
from peewee import (
|
|
BooleanField,
|
|
CharField,
|
|
DateTimeField,
|
|
ForeignKeyField,
|
|
IntegerField,
|
|
Model,
|
|
)
|
|
|
|
from imptune.db.database import db
|
|
|
|
|
|
class BaseModel(Model):
|
|
"""Base model that binds all models to the shared db instance."""
|
|
|
|
class Meta:
|
|
database = db
|
|
|
|
|
|
class Owner(BaseModel):
|
|
"""Cookie-scoped identity — no accounts, just an opaque bearer key."""
|
|
|
|
key = CharField(unique=True, index=True)
|
|
is_permanent = BooleanField(default=False)
|
|
created_at = DateTimeField(default=_utcnow)
|
|
|
|
class Meta:
|
|
table_name = "owner"
|
|
|
|
|
|
class Client(BaseModel):
|
|
"""Represents a deployment target (AD client / OU), scoped to an Owner."""
|
|
|
|
name = CharField()
|
|
owner = ForeignKeyField(Owner, backref="clients")
|
|
created_at = DateTimeField(default=_utcnow)
|
|
|
|
class Meta:
|
|
table_name = "client"
|
|
indexes = ((("owner", "name"), True),)
|
|
|
|
|
|
class Driver(BaseModel):
|
|
"""Uploaded printer driver package (content-addressed by SHA256)."""
|
|
|
|
sha256 = CharField(unique=True, index=True)
|
|
original_filename = CharField()
|
|
size_bytes = IntegerField()
|
|
uploaded_at = DateTimeField(default=_utcnow)
|
|
driver_desc = CharField(null=True)
|
|
inf_filename = CharField(null=True)
|
|
architecture = CharField(null=True)
|
|
has_cat_file = BooleanField(default=False)
|
|
# Operator-chosen label. Drivers are global/shared, so a rename is visible
|
|
# to every Owner — same as the rest of this row.
|
|
display_name = CharField(null=True)
|
|
|
|
class Meta:
|
|
table_name = "driver"
|
|
|
|
@property
|
|
def label(self) -> str:
|
|
"""What the UI shows: the rename if there is one, else the ZIP name."""
|
|
return self.display_name or self.original_filename
|
|
|
|
|
|
class Printer(BaseModel):
|
|
"""Printer configuration record."""
|
|
|
|
name = CharField()
|
|
ip_address = CharField()
|
|
port_name = CharField()
|
|
owner = ForeignKeyField(Owner, backref="printers")
|
|
client = ForeignKeyField(Client, null=True, backref="printers")
|
|
driver = ForeignKeyField(Driver, null=True, backref="printers")
|
|
duplex_mode = CharField(default="OneSided")
|
|
color_mode = BooleanField(default=True)
|
|
paper_size = CharField(default="A4")
|
|
collate = BooleanField(default=True)
|
|
created_at = DateTimeField(default=_utcnow)
|
|
updated_at = DateTimeField(default=_utcnow)
|
|
|
|
class Meta:
|
|
table_name = "printer"
|
|
|
|
|
|
class Icon(BaseModel):
|
|
"""Printer icon image (one per printer)."""
|
|
|
|
printer = ForeignKeyField(Printer, unique=True, backref="icons")
|
|
sha256 = CharField()
|
|
original_filename = CharField()
|
|
size_bytes = IntegerField()
|
|
uploaded_at = DateTimeField(default=_utcnow)
|
|
|
|
class Meta:
|
|
table_name = "icon"
|
|
|
|
|
|
class DriverIcon(BaseModel):
|
|
"""Icon image for a driver package (one per driver).
|
|
|
|
Separate table rather than a nullable FK on `Icon`: printer icons ship in
|
|
the `.intunewin` export and driver icons are library decoration only, so
|
|
the two never share a query. Global/shared, like `Driver` itself.
|
|
"""
|
|
|
|
driver = ForeignKeyField(Driver, unique=True, backref="icons", on_delete="CASCADE")
|
|
sha256 = CharField()
|
|
original_filename = CharField()
|
|
size_bytes = IntegerField()
|
|
uploaded_at = DateTimeField(default=_utcnow)
|
|
|
|
class Meta:
|
|
table_name = "driver_icon"
|