Database memes

This commit is contained in:
Pünkösd Marcell 2020-11-27 21:13:00 +01:00
parent e25c28b04c
commit 6eb2fe1879
13 changed files with 52 additions and 18 deletions

View File

@ -11,3 +11,6 @@ flask-cors
requests
psycopg2-binary
email_validator
minio
flask-minio
bleach

View File

@ -7,11 +7,12 @@ from sentry_sdk.integrations.sqlalchemy import SqlalchemyIntegration
from healthcheck import HealthCheck
from flask_cors import CORS
from utils.config import SENTRY_DSN, RELEASE_ID, RELEASEMODE, POSTGRES_DB, PORT, POSTGRES_HOSTNAME, POSTGRES_PASSWORD, \
POSTGRES_USERNAME, DEBUG, SECRET_KEY, ALLOWED_ORIGINS
from utils import db, health_database_status, security, user_datastore
from utils.config import SENTRY_DSN, RELEASE_ID, RELEASEMODE, PORT, DEBUG, SECRET_KEY, ALLOWED_ORIGINS, SQLALCHEMY_URI
from utils import health_database_status, security, user_datastore
from views import ItemView, ProfileView, UploadView, IndexView
from models import db
"""
Main Flask entrypoint
"""
@ -33,8 +34,7 @@ if SENTRY_DSN:
)
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = \
f"postgresql://{POSTGRES_USERNAME}:{POSTGRES_PASSWORD}@{POSTGRES_HOSTNAME}:5432/{POSTGRES_DB}"
app.config['SQLALCHEMY_DATABASE_URI'] = SQLALCHEMY_URI
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SECRET_KEY'] = SECRET_KEY
app.config['SECURITY_REGISTERABLE'] = True

View File

@ -1,3 +1,4 @@
from .db import db
from .user import User
from .role import Role
from .item import Item

17
src/models/comment.py Normal file
View File

@ -0,0 +1,17 @@
#!/usr/bin/env python3
from sqlalchemy import func
from . import db
class Comment(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
timestamp = db.Column(db.TIMESTAMP, nullable=False, server_default=func.now())
commenter_id = db.Column(db.Integer, db.ForeignKey("user.id", ondelete="CASCADE"), nullable=False)
commenter = db.relationship("User", backref=db.backref("comments", lazy=True))
item_id = db.Column(db.Integer, db.ForeignKey("item.id", ondelete="CASCADE"), nullable=False)
item = db.relationship("Item", backref=db.backref("comments", lazy=True))
text = db.Text(4096, nullable=False)

View File

@ -1,7 +1,7 @@
#!/usr/bin/env python3
from sqlalchemy import func
from utils import db
from . import db
"""
Item model
@ -16,4 +16,7 @@ __version__text__ = "1"
class Item(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.String, nullable=False)
creation_date = db.Column(db.TIMESTAMP, nullable=False, server_default=func.now())
upload_date = db.Column(db.TIMESTAMP, nullable=False, server_default=func.now())
uploader_id = db.Column(db.Integer, db.ForeignKey("user.id", ondelete="CASCADE"), nullable=False)
uploader = db.relationship("User", backref=db.backref("uploads", lazy=True))

15
src/models/purchase.py Normal file
View File

@ -0,0 +1,15 @@
#!/usr/bin/env python3
from sqlalchemy import func
from . import db
class Purchase(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
timestamp = db.Column(db.TIMESTAMP, nullable=False, server_default=func.now())
purchaser_id = db.Column(db.Integer, db.ForeignKey("user.id", ondelete="CASCADE"), nullable=False)
purchaser = db.relationship("User", backref=db.backref("purchases", lazy=True))
item_id = db.Column(db.Integer, db.ForeignKey("item.id", ondelete="CASCADE"), nullable=False)
item = db.relationship("Item", backref=db.backref("purchases", lazy=True))

View File

@ -1,7 +1,7 @@
#!/usr/bin/env python3
from flask_security import RoleMixin
from utils import db
from . import db
"""
Role model

View File

@ -2,7 +2,7 @@
from flask_security import UserMixin
from .role import roles_users
from utils import db
from . import db
"""
User model

View File

@ -1,3 +1,2 @@
from .db import db
from .healthchecks import health_database_status
from .security import security, user_datastore

View File

@ -19,10 +19,7 @@ SENTRY_DSN = os.environ.get("SENTRY_DSN")
RELEASE_ID = os.environ.get("RELEASE_ID", "test")
RELEASEMODE = os.environ.get("RELEASEMODE", "dev")
POSTGRES_HOSTNAME = os.getenv("POSTGRES_HOSTNAME", "localhost")
POSTGRES_USERNAME = os.getenv("POSTGRES_USERNAME", "webshop")
POSTGRES_PASSWORD = os.getenv("POSTGRES_PASSWORD", "webshop")
POSTGRES_DB = os.getenv("POSTGRES_DB", "webshop")
SQLALCHEMY_URI = os.environ.get("SQLALCHEMY_URI", "sqlite://")
SECRET_KEY = os.getenv("SECRET_KEY")
ALLOWED_ORIGINS = os.environ.get('ALLOWED_ORIGINS', '*')

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python3
from .db import db
from models.db import db
"""
Healthchek functions

View File

@ -11,8 +11,7 @@ __version__text__ = "1"
from flask_security import Security, SQLAlchemyUserDatastore
from models import User, Role
from utils import db
from models import db, User, Role
user_datastore = SQLAlchemyUserDatastore(db, User, Role)
security = Security()