From 3df5f74ec396de72c5f6c6c9c402c487985ce5ff Mon Sep 17 00:00:00 2001 From: Domeniko Gentner Date: Mon, 7 Dec 2020 21:45:20 +0100 Subject: [PATCH] Added project table --- labertasche/database/__init__.py | 13 ++++++++++++- labertasche/models/__init__.py | 1 + labertasche/models/t_comments.py | 3 ++- labertasche/models/t_emails.py | 2 ++ labertasche/models/t_location.py | 2 ++ labertasche/models/t_projects.py | 21 +++++++++++++++++++++ server.py | 12 +++++++----- 7 files changed, 47 insertions(+), 7 deletions(-) create mode 100644 labertasche/models/t_projects.py diff --git a/labertasche/database/__init__.py b/labertasche/database/__init__.py index 929b1da..c257476 100644 --- a/labertasche/database/__init__.py +++ b/labertasche/database/__init__.py @@ -7,6 +7,17 @@ # * _license : This project is under MIT License # *********************************************************************************/ from flask_sqlalchemy import SQLAlchemy +from sqlalchemy import MetaData + +# naming conventions +convention = { + "ix": 'ix_%(column_0_label)s', + "uq": "uq_%(table_name)s_%(column_0_name)s", + "ck": "ck_%(table_name)s_%(constraint_name)s", + "fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s", + "pk": "pk_%(table_name)s" +} +metadata = MetaData(naming_convention=convention) # Create SQLAlchemy -labertasche_db = SQLAlchemy() +labertasche_db = SQLAlchemy(metadata=metadata) diff --git a/labertasche/models/__init__.py b/labertasche/models/__init__.py index 6614d81..7e9bc2c 100644 --- a/labertasche/models/__init__.py +++ b/labertasche/models/__init__.py @@ -9,3 +9,4 @@ from .t_comments import TComments from .t_location import TLocation from .t_emails import TEmail +from .t_projects import TProjects diff --git a/labertasche/models/t_comments.py b/labertasche/models/t_comments.py index e2a23a1..d97f86e 100644 --- a/labertasche/models/t_comments.py +++ b/labertasche/models/t_comments.py @@ -28,7 +28,8 @@ class TComments(db.Model): is_published = db.Column(db.Boolean, nullable=False) is_spam = db.Column(db.Boolean, nullable=False) spam_score = db.Column(db.Float, nullable=False) - replied_to = db.Column(db.Integer, ForeignKey('t_comments.comments_id'), nullable=True) + replied_to = db.Column(db.Integer, ForeignKey('t_comments.comments_id'), nullable=True, default=None) confirmation = db.Column(db.Text, nullable=True) deletion = db.Column(db.Text, nullable=True) gravatar = db.Column(db.Text, nullable=True) + project_id = db.Column(db.Integer, ForeignKey('t_projects.id_project'), nullable=False) diff --git a/labertasche/models/t_emails.py b/labertasche/models/t_emails.py index 49e90c3..91746dc 100644 --- a/labertasche/models/t_emails.py +++ b/labertasche/models/t_emails.py @@ -7,6 +7,7 @@ # * _license : This project is under MIT License # *********************************************************************************/ from labertasche.database import labertasche_db as db +from sqlalchemy import ForeignKey class TEmail(db.Model): @@ -21,3 +22,4 @@ class TEmail(db.Model): email = db.Column(db.Integer, unique=True) is_blocked = db.Column(db.Boolean) is_allowed = db.Column(db.Boolean) + project_id = db.Column(db.Integer, ForeignKey('t_projects.id_project'), nullable=False) diff --git a/labertasche/models/t_location.py b/labertasche/models/t_location.py index 9203156..8e469c4 100644 --- a/labertasche/models/t_location.py +++ b/labertasche/models/t_location.py @@ -7,6 +7,7 @@ # * _license : This project is under MIT License # *********************************************************************************/ from labertasche.database import labertasche_db as db +from sqlalchemy import ForeignKey class TLocation(db.Model): @@ -19,3 +20,4 @@ class TLocation(db.Model): # data location = db.Column(db.Text, nullable=False, unique=True) + project_id = db.Column(db.Integer, ForeignKey('t_projects.id_project'), nullable=False) diff --git a/labertasche/models/t_projects.py b/labertasche/models/t_projects.py new file mode 100644 index 0000000..b61c300 --- /dev/null +++ b/labertasche/models/t_projects.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# /********************************************************************************** +# * _author : Domeniko Gentner +# * _mail : code@tuxstash.de +# * _repo : https://git.tuxstash.de/gothseidank/labertasche +# * _license : This project is under MIT License +# *********************************************************************************/ +from labertasche.database import labertasche_db as db + + +class TProjects(db.Model): + # table name + __tablename__ = "t_projects" + __table_args__ = {'useexisting': True} + + # primary key + id_project = db.Column(db.Integer, primary_key=True) + + # data + name = db.Column(db.Text, nullable=True, unique=True) diff --git a/server.py b/server.py index 09c9d79..812d867 100644 --- a/server.py +++ b/server.py @@ -34,9 +34,15 @@ laberflask.config.update(dict( SQLALCHEMY_TRACK_MODIFICATIONS=False )) -# flask migrate +# Flask migrate + migrate = Migrate(laberflask, labertasche_db, render_as_batch=True) +# Initialize ORM +labertasche_db.init_app(laberflask) +with laberflask.app_context(): + labertasche_db.create_all() + # CORS CORS(laberflask, resources={r"/comments": {"origins": settings.system['blog_url']}}) @@ -49,10 +55,6 @@ laberflask.register_blueprint(bp_login) log = logging.getLogger('werkzeug') log.setLevel(logging.ERROR) -# Initialize ORM -labertasche_db.init_app(laberflask) -with laberflask.app_context(): - labertasche_db.create_all() # Set up login manager loginmgr = LoginManager(laberflask)