Starting to recreate models.
This commit is contained in:
parent
e420110fb3
commit
388472742e
11 changed files with 132 additions and 4 deletions
|
|
@ -23,4 +23,4 @@ class Version(Base):
|
||||||
timestamp = Column(DateTime, default=func.now())
|
timestamp = Column(DateTime, default=func.now())
|
||||||
|
|
||||||
actor = Column(String, nullable=True)
|
actor = Column(String, nullable=True)
|
||||||
metadata = Column(JSON, nullable=True)
|
meta = Column('metadata', JSON, nullable=True)
|
||||||
|
|
|
||||||
|
|
@ -98,7 +98,7 @@ class CRUDService(Generic[T]):
|
||||||
change_type=change_type,
|
change_type=change_type,
|
||||||
data=data,
|
data=data,
|
||||||
actor=str(actor) if actor else None,
|
actor=str(actor) if actor else None,
|
||||||
metadata=metadata
|
meta=metadata
|
||||||
)
|
)
|
||||||
self.session.add(version)
|
self.session.add(version)
|
||||||
self.session.commit()
|
self.session.commit()
|
||||||
|
|
|
||||||
23
inventory/__init__.py
Normal file
23
inventory/__init__.py
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
import os
|
||||||
|
|
||||||
|
from flask import Flask
|
||||||
|
|
||||||
|
from .db import init_db, create_all_tables
|
||||||
|
|
||||||
|
def create_app() -> Flask:
|
||||||
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
app.config["DATABASE_URL"] = os.getenv("DATABASE_URL", "sqlite:///inventory.db")
|
||||||
|
|
||||||
|
init_db(app.config["DATABASE_URL"])
|
||||||
|
|
||||||
|
from . import models as _models
|
||||||
|
|
||||||
|
create_all_tables()
|
||||||
|
|
||||||
|
@app.get("/")
|
||||||
|
def index():
|
||||||
|
return {"status": "ok"}
|
||||||
|
|
||||||
|
return app
|
||||||
28
inventory/db.py
Normal file
28
inventory/db.py
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from sqlalchemy import create_engine
|
||||||
|
from sqlalchemy.orm import sessionmaker, scoped_session
|
||||||
|
|
||||||
|
from crudkit.core.base import Base
|
||||||
|
|
||||||
|
_engine = None
|
||||||
|
SessionLocal = None
|
||||||
|
|
||||||
|
def init_db(database_url: str) -> None:
|
||||||
|
global _engine, SessionLocal
|
||||||
|
connect_args = {}
|
||||||
|
if database_url.startswith("sqlite:///"):
|
||||||
|
connect_args["check_same_thread"] = False
|
||||||
|
|
||||||
|
_engine = create_engine(database_url, future=True, echo=False, connect_args=connect_args)
|
||||||
|
SessionLocal = scoped_session(sessionmaker(bind=_engine, autoflush=False, autocommit=False, future=True))
|
||||||
|
|
||||||
|
def get_session():
|
||||||
|
if SessionLocal is None:
|
||||||
|
raise RuntimeError("DB not initialized. Call init_db() first.")
|
||||||
|
return SessionLocal()
|
||||||
|
|
||||||
|
def create_all_tables() -> None:
|
||||||
|
if _engine is None:
|
||||||
|
raise RuntimeError("DB engine not initialized.")
|
||||||
|
Base.metadata.create_all(bind=_engine)
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
from sqlalchemy import Integer, Unicode
|
||||||
|
from sqlalchemy.orm import Mapped, mapped_column
|
||||||
|
|
||||||
|
from crudkit.core.base import Base, CRUDMixin
|
||||||
|
|
@ -1,8 +1,18 @@
|
||||||
from sqlalchemy import Column, Unicode
|
from typing import List, Optional
|
||||||
|
|
||||||
|
from sqlalchemy import Boolean, Unicode
|
||||||
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||||
|
|
||||||
from crudkit.core.base import Base, CRUDMixin
|
from crudkit.core.base import Base, CRUDMixin
|
||||||
|
|
||||||
class Area(Base, CRUDMixin):
|
class Area(Base, CRUDMixin):
|
||||||
__tablename__ = "area"
|
__tablename__ = "area"
|
||||||
|
|
||||||
name = Column(Unicode(255), nullable=True)
|
name: Mapped[Optional[str]] = mapped_column(Unicode(255), nullable=True)
|
||||||
|
|
||||||
|
rooms: Mapped[List['Room']] = relationship('Room', back_populates='area')
|
||||||
|
|
||||||
|
is_deleted: Mapped[Boolean] = mapped_column(Boolean, nullable=False, default=False)
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f"<Area(id={self.id}, name={repr(self.name)})>"
|
||||||
18
inventory/models/brand.py
Normal file
18
inventory/models/brand.py
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
from sqlalchemy import Boolean, Unicode
|
||||||
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||||
|
|
||||||
|
from crudkit.core.base import Base, CRUDMixin
|
||||||
|
|
||||||
|
class Brand(Base, CRUDMixin):
|
||||||
|
__tablename__ = "brand"
|
||||||
|
|
||||||
|
name: Mapped[str] = mapped_column(Unicode(255), nullable=False)
|
||||||
|
|
||||||
|
inventory: Mapped[List['Inventory']] = relationship('Inventory', back_populates='brand')
|
||||||
|
|
||||||
|
is_deleted: Mapped[Boolean] = mapped_column(Boolean, nullable=False, default=False)
|
||||||
|
|
||||||
|
def __repr__(self) -> str:
|
||||||
|
return f"<Brand(id={self.id}, name={repr(self.name)})>"4
|
||||||
20
inventory/models/image.py
Normal file
20
inventory/models/image.py
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
from typing import List, Optional
|
||||||
|
|
||||||
|
from sqlalchemy import DateTime, Unicode, func
|
||||||
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||||
|
|
||||||
|
from crudkit.core.base import Base, CRUDMixin
|
||||||
|
|
||||||
|
class Image(Base, CRUDMixin):
|
||||||
|
__tablename__ = "images"
|
||||||
|
|
||||||
|
filename: Mapped[str] = mapped_column(Unicode(512))
|
||||||
|
caption: Mapped[str] = mapped_column(Unicode(255), default="")
|
||||||
|
timestamp: Mapped[DateTime] = mapped_column(DateTime, default=func.now(), server_default=func.now())
|
||||||
|
|
||||||
|
inventory: Mapped[Optional['Inventory']] = relationship('Inventory', back_populates='images')
|
||||||
|
user: Mapped[Optional['User']] = relationship('User', back_populates='image')
|
||||||
|
worklogs: Mapped[List['WorkLog']] = relationship('WorkLog', secondary=worklog_images, back_populates='images')
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f"<Image(id={self.id}, filename={self.filename})>"
|
||||||
18
inventory/models/inventory.py
Normal file
18
inventory/models/inventory.py
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
from sqlalchemy import DateTime, ForeignKey, Index, Integer, Unicode
|
||||||
|
from sqlalchemy.orm import Mapped, mapped_column
|
||||||
|
|
||||||
|
from crudkit.core.base import Base, CRUDMixin
|
||||||
|
|
||||||
|
class Inventory(Base, CRUDMixin):
|
||||||
|
__tablename__ = "inventory"
|
||||||
|
|
||||||
|
name: Mapped[Optional[str]] = mapped_column(Unicode(255))
|
||||||
|
serial: Mapped[Optional[str]] = mapped_column(Unicode(255))
|
||||||
|
|
||||||
|
timestamp: Mapped[DateTime] = mapped_column(DateTime)
|
||||||
|
condition: Mapped[str] = mapped_column(Unicode(255))
|
||||||
|
|
||||||
|
device_type_id: Mapped[Optional[int]] = mapped_column('type_id', Integer, ForeignKey("item.id"), nullable=True, index=True)
|
||||||
|
device_type: Mapped[Optional['DeviceType']]
|
||||||
2
inventory/wsgi.py
Normal file
2
inventory/wsgi.py
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
from . import create_app
|
||||||
|
app = create_app
|
||||||
|
|
@ -20,5 +20,8 @@ dependencies = [
|
||||||
requires = ["setuptools"]
|
requires = ["setuptools"]
|
||||||
build-backend = "setuptools.build_meta"
|
build-backend = "setuptools.build_meta"
|
||||||
|
|
||||||
|
[tool.pip.dependencies]
|
||||||
|
crudkit = { path = "../crudkit", editable = true }
|
||||||
|
|
||||||
[tool.setuptools]
|
[tool.setuptools]
|
||||||
packages = ["inventory"]
|
packages = ["inventory"]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue