DB model structure

This commit is contained in:
Felix Albrigtsen 2022-08-16 19:52:45 +02:00
parent 766c9af62d
commit 78ccd1b097
7 changed files with 127 additions and 2 deletions

View File

@ -6,7 +6,7 @@ from flask_admin.contrib.sqla import ModelView
from os import environ, path from os import environ, path
from dotenv import load_dotenv from dotenv import load_dotenv
from worblehat.database import db_session, init_db from worblehat.database import db_session, init_db, drop_db
def create_app(): def create_app():
app = Flask(__name__, instance_relative_config=True) app = Flask(__name__, instance_relative_config=True)
@ -27,6 +27,54 @@ def configure_database(app):
init_db() init_db()
print("Initialized the database.") print("Initialized the database.")
@app.cli.command("resetdb")
def resetdb_command():
drop_db()
print("Cleared the database.")
init_db()
from worblehat.models.MediaType import MediaType
from worblehat.models.Bookcase import Bookcase
from worblehat.models.Location import Location
from worblehat.models.Language import Language
media_types = [
MediaType(name='Book', description='A physical book'),
MediaType(name='Comic', description='A comic book'),
MediaType(name='Video Game', description='A digital game for computers or games consoles'),
MediaType(name='Tabletop Game', description='A physical game with cards, boards or similar')
]
bookcases = [
Bookcase(name='A', description='The first bookcase'),
Bookcase(name='B', description='The second bookcase'),
]
locations = [
Location(name='1-1', description='The first location', bookcase=bookcases[0]),
Location(name='1-2', description='The second location', bookcase=bookcases[0]),
Location(name='1-1', description='The first location', bookcase=bookcases[1]),
Location(name='1-2', description='The second location', bookcase=bookcases[1]),
]
languages = [
Language(name='English', shortname='en'),
Language(name='Norwegian', shortname='no'),
Language(name='Japanese', shortname='ja'),
Language(name='Swedish', shortname='sv'),
Language(name='German', shortname='de'),
Language(name='Russian', shortname='ru'),
Language(name='Danish', shortname='da')
]
db_session.add_all(media_types)
db_session.add_all(bookcases)
db_session.add_all(locations)
db_session.add_all(languages)
db_session.commit()
print("Added media types, bookcases and locations.")
@app.teardown_appcontext @app.teardown_appcontext
def shutdown_session(exception=None): def shutdown_session(exception=None):
db_session.remove() db_session.remove()
@ -37,8 +85,12 @@ def configure_admin(app):
from worblehat.models.Category import Category from worblehat.models.Category import Category
from worblehat.models.Item import Item from worblehat.models.Item import Item
from worblehat.models.MediaType import MediaType from worblehat.models.MediaType import MediaType
from worblehat.models.Location import Location
from worblehat.models.Bookcase import Bookcase
admin.add_view(ModelView(Category, db_session)) admin.add_view(ModelView(Category, db_session))
admin.add_view(ModelView(Item, db_session)) admin.add_view(ModelView(Item, db_session))
admin.add_view(ModelView(MediaType, db_session)) admin.add_view(ModelView(MediaType, db_session))
admin.add_view(ModelView(Location, db_session))
admin.add_view(ModelView(Bookcase, db_session))

View File

@ -18,5 +18,12 @@ def init_db():
from .models.Category import Category from .models.Category import Category
from .models.Item import Item from .models.Item import Item
from .models.MediaType import MediaType from .models.MediaType import MediaType
from .models.Location import Location
from .models.Bookcase import Bookcase
from .models.Language import Language
Base.metadata.create_all(bind=engine) Base.metadata.create_all(bind=engine)
def drop_db():
Base.metadata.drop_all(bind=engine)

View File

@ -0,0 +1,16 @@
from sqlalchemy import Column, Integer, String, ForeignKey, Boolean
from sqlalchemy.orm import relationship
from worblehat.database import Base
class Bookcase(Base):
__tablename__ = 'bookcases'
id = Column(Integer, primary_key=True)
name = Column(String(10), nullable=False)
description = Column(String(255))
locations = relationship('Location', back_populates='bookcase')
def __repr__(self):
return '<Bookcase %r>' % self.name

View File

@ -1,12 +1,18 @@
from sqlalchemy import Column, Integer, String, ForeignKey, Boolean from sqlalchemy import Column, Integer, String, ForeignKey, Boolean, Table
from sqlalchemy.orm import relationship from sqlalchemy.orm import relationship
from worblehat.database import Base from worblehat.database import Base
category_association = Table('category_association', Base.metadata,
Column('category_id', Integer, ForeignKey('categories.id')),
Column('item_id', Integer, ForeignKey('items.id'))
)
class Category(Base): class Category(Base):
__tablename__ = 'categories' __tablename__ = 'categories'
id = Column(Integer, primary_key=True) id = Column(Integer, primary_key=True)
name = Column(String(80), unique=True) name = Column(String(80), unique=True)
description = Column(String(255)) description = Column(String(255))
items = relationship('Item', secondary=category_association, back_populates='categories')
def __repr__(self): def __repr__(self):
return '<Category %r>' % self.name return '<Category %r>' % self.name

View File

@ -1,6 +1,7 @@
from sqlalchemy import Column, Integer, String, ForeignKey, Boolean from sqlalchemy import Column, Integer, String, ForeignKey, Boolean
from sqlalchemy.orm import relationship from sqlalchemy.orm import relationship
from worblehat.database import Base from worblehat.database import Base
from .Language import Language
class Item(Base): class Item(Base):
__tablename__ = 'items' __tablename__ = 'items'
@ -13,6 +14,14 @@ class Item(Base):
media_id = Column(Integer, ForeignKey('media_types.id'), nullable=False) media_id = Column(Integer, ForeignKey('media_types.id'), nullable=False)
media = relationship('MediaType', back_populates='items') media = relationship('MediaType', back_populates='items')
location_id = Column(Integer, ForeignKey('locations.id'), nullable=False)
location = relationship('Location', back_populates='items')
language_id = Column(Integer, ForeignKey('languages.id'), nullable=False)
language = relationship('Language', back_populates='items')
categories = relationship('Category', secondary='category_association', back_populates='items')
def __repr__(self): def __repr__(self):
return '<Item %r / %r>' % (self.media.name, self.title) return '<Item %r / %r>' % (self.media.name, self.title)

View File

@ -0,0 +1,16 @@
from sqlalchemy import Column, Integer, String, ForeignKey, Boolean
from sqlalchemy.orm import relationship
from worblehat.database import Base
class Language(Base):
__tablename__ = 'languages'
id = Column(Integer, primary_key=True)
name = Column(String(32), nullable=False)
shortname = Column(String(2), nullable=False)
flag = Column(String(2))
items = relationship('Item', back_populates='language')
def __repr__(self):
return '<Language %r>' % self.name

View File

@ -0,0 +1,19 @@
from sqlalchemy import Column, Integer, String, ForeignKey, Boolean
from sqlalchemy.orm import relationship
from worblehat.database import Base
class Location(Base):
__tablename__ = 'locations'
id = Column(Integer, primary_key=True)
name = Column(String(100), nullable=False)
description = Column(String(255))
bookcase_id = Column(Integer, ForeignKey('bookcases.id'), nullable=False)
bookcase = relationship('Bookcase', back_populates='locations')
items = relationship('Item', back_populates='location')
def __repr__(self):
return '<Location %s %s>' % (self.bookcase.name, self.name)