diff --git a/.gitignore b/.gitignore index b6e4761..9df3e29 100644 --- a/.gitignore +++ b/.gitignore @@ -64,6 +64,7 @@ db.sqlite3-journal # Flask stuff: instance/ .webassets-cache +db.sqlite # Scrapy stuff: .scrapy diff --git a/config.py b/config.py new file mode 100644 index 0000000..3d4bd11 --- /dev/null +++ b/config.py @@ -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') diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..0f60fcb --- /dev/null +++ b/run.sh @@ -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 diff --git a/worblehat/__init__.py b/worblehat/__init__.py new file mode 100644 index 0000000..53ece25 --- /dev/null +++ b/worblehat/__init__.py @@ -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)) + diff --git a/worblehat/database.py b/worblehat/database.py new file mode 100644 index 0000000..bf9827e --- /dev/null +++ b/worblehat/database.py @@ -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) diff --git a/worblehat/models/Category.py b/worblehat/models/Category.py new file mode 100644 index 0000000..72d7b93 --- /dev/null +++ b/worblehat/models/Category.py @@ -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 '' % self.name diff --git a/worblehat/models/Item.py b/worblehat/models/Item.py new file mode 100644 index 0000000..fe1bc2d --- /dev/null +++ b/worblehat/models/Item.py @@ -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 '' % (self.media.name, self.title) + diff --git a/worblehat/models/MediaType.py b/worblehat/models/MediaType.py new file mode 100644 index 0000000..a59c054 --- /dev/null +++ b/worblehat/models/MediaType.py @@ -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 '' % self.name +