Initialize flask configs

This commit is contained in:
Felix Albrigtsen 2022-08-16 15:07:31 +02:00
parent 4397ef573b
commit 766c9af62d
8 changed files with 134 additions and 0 deletions

1
.gitignore vendored
View File

@ -64,6 +64,7 @@ db.sqlite3-journal
# Flask stuff: # Flask stuff:
instance/ instance/
.webassets-cache .webassets-cache
db.sqlite
# Scrapy stuff: # Scrapy stuff:
.scrapy .scrapy

13
config.py Normal file
View File

@ -0,0 +1,13 @@
from os import environ, path
from dotenv import load_dotenv
basedir = path.abspath(path.dirname(__file__))
load_dotenv(path.join(basedir, '.env'))
class Config:
TESTING = True
DEBUG = True
FLASK_ENV = 'development'
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + path.join(basedir, 'worblehat.sqlite')
SQLALCHEMY_TRACK_MODIFICATIONS = False
SECRET_KEY = environ.get('SECRET_KEY')

5
run.sh Executable file
View File

@ -0,0 +1,5 @@
#!/bin/bash
# FLASK_APP=app.py FLASK_DEBUG=1 FLASK_ENV=development python3 -m flask run --host=localhost --port=5000 --debugger --reload
flask --app worblehat --debug run --host=localhost --port=5000 --debugger --reload

44
worblehat/__init__.py Normal file
View File

@ -0,0 +1,44 @@
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_admin import Admin
from flask_admin.contrib.sqla import ModelView
from os import environ, path
from dotenv import load_dotenv
from worblehat.database import db_session, init_db
def create_app():
app = Flask(__name__, instance_relative_config=True)
app.config.from_object('config.Config')
configure_database(app)
configure_admin(app)
@app.route('/')
def index():
return 'Hello World!'
return app
def configure_database(app):
@app.cli.command("initdb")
def initdb_command():
init_db()
print("Initialized the database.")
@app.teardown_appcontext
def shutdown_session(exception=None):
db_session.remove()
def configure_admin(app):
admin = Admin(app, name='Worblehat', template_mode='bootstrap3')
from worblehat.models.Category import Category
from worblehat.models.Item import Item
from worblehat.models.MediaType import MediaType
admin.add_view(ModelView(Category, db_session))
admin.add_view(ModelView(Item, db_session))
admin.add_view(ModelView(MediaType, db_session))

22
worblehat/database.py Normal file
View File

@ -0,0 +1,22 @@
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
engine = create_engine('sqlite:///db.sqlite', convert_unicode=True)
db_session = scoped_session(sessionmaker(autocommit=False,
autoflush=False,
bind=engine))
Base = declarative_base()
Base.query = db_session.query_property()
def init_db():
# import all modules here that might define models so that
# they will be registered properly on the metadata. Otherwise
# you will have to import them first before calling init_db()
from .models.Category import Category
from .models.Item import Item
from .models.MediaType import MediaType
Base.metadata.create_all(bind=engine)

View File

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

18
worblehat/models/Item.py Normal file
View File

@ -0,0 +1,18 @@
from sqlalchemy import Column, Integer, String, ForeignKey, Boolean
from sqlalchemy.orm import relationship
from worblehat.database import Base
class Item(Base):
__tablename__ = 'items'
id = Column(Integer, primary_key=True)
title = Column(String(255), nullable=False)
owner = Column(String(255))
isbn = Column(String(255))
media_id = Column(Integer, ForeignKey('media_types.id'), nullable=False)
media = relationship('MediaType', back_populates='items')
def __repr__(self):
return '<Item %r / %r>' % (self.media.name, self.title)

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 MediaType(Base):
__tablename__ = 'media_types'
id = Column(Integer, primary_key=True)
name = Column(String(80), nullable=False)
description = Column(String(255))
items = relationship('Item', back_populates='media', lazy=True)
def __init__(self, name, description):
self.name = name
self.description = description
def __repr__(self):
return '<MediaType %r>' % self.name