From d5e95e25d0a8b2d7b027ed5480bb4d65c833aeb9 Mon Sep 17 00:00:00 2001 From: Andros Fenollosa Date: Wed, 31 Jan 2018 00:42:22 +0100 Subject: [PATCH] Add models and env --- Pipfile | 3 +++ app.py | 6 ++++- models.py | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 models.py diff --git a/Pipfile b/Pipfile index dc5f052..22cd21b 100644 --- a/Pipfile +++ b/Pipfile @@ -16,3 +16,6 @@ flask-restplus = "*" flask-jwt = "*" flask-sqlalchemy = "*" flask-mail = "*" +python-dotenv = "*" +flask-script = "*" +flask-migrate = "*" diff --git a/app.py b/app.py index 78f238e..da075fe 100644 --- a/app.py +++ b/app.py @@ -1,5 +1,9 @@ +import os from flask import Flask from flask_restplus import Resource, Api +from dotenv import load_dotenv, find_dotenv + +load_dotenv(find_dotenv()) app = Flask(__name__) api = Api(app) @@ -55,5 +59,5 @@ class Comments(Resource): if __name__ == '__main__': - app.run(debug=True) + app.run(debug=os.environ.get('DEBUG') == 'True' if True else False) diff --git a/models.py b/models.py new file mode 100644 index 0000000..ec3b48b --- /dev/null +++ b/models.py @@ -0,0 +1,80 @@ +# -*- coding: utf-8 -*- +# Librarys +from flask import Flask +from datetime import datetime +from flask_sqlalchemy import SQLAlchemy +from flask_script import Manager +from flask_migrate import Migrate, MigrateCommand + +app = Flask(__name__) + +# Settings +app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.sqlite' +app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False + +# Variables +db = SQLAlchemy(app) +migrate = Migrate(app, db) +manager = Manager(app) +manager.add_command('db', MigrateCommand) + + +class User(db.Model): + ''' + Table user + ''' + + __tablename__ = 'users' + + id = db.Column(db.Integer, primary_key=True) + username = db.Column(db.String(100)) + email = db.Column(db.String(200)) + password = db.Column(db.String(106)) + created_at = db.Column( + db.DateTime, nullable=False, default=datetime.utcnow) + + def __repr__(self): + return ''.format(self.username) + + +class News(db.Model): + ''' + Table News + ''' + __tablename__ = 'news' + id = db.Column(db.Integer, primary_key=True) + title = db.Column(db.String(100)) + link = db.Column(db.String(500)) + created_at = db.Column( + db.DateTime, nullable=False, default=datetime.utcnow) + + user_id = db.Column( + db.Integer, db.ForeignKey('user.id'), nullable=False) + user = db.relationship( + 'User', backref=db.backref('News', lazy=True)) + + def __repr__(self): + return ''.format(self.title) + + +class Comment(db.Model): + ''' + Table Comment + ''' + __tablename__ = 'comments' + id = db.Column(db.Integer, primary_key=True) + text = db.Column(db.String(1000)) + created_at = db.Column( + db.DateTime, nullable=False, default=datetime.utcnow) + + news_id = db.Column( + db.Integer, db.ForeignKey('news.id'), nullable=False) + news = db.relationship( + 'News', backref=db.backref('Comment', lazy=True)) + + def __repr__(self): + return ''.format(self.id) + + +if __name__ == "__main__": + manager.run()