flask-api-example/models.py

106 lines
2.5 KiB
Python
Raw Normal View History

2018-01-31 00:42:22 +01:00
# -*- coding: utf-8 -*-
# Librarys
2018-01-31 23:17:52 +01:00
import os
2018-02-11 18:02:41 +01:00
from os.path import join, dirname
2018-01-31 00:42:22 +01:00
from flask import Flask
from datetime import datetime
from flask_sqlalchemy import SQLAlchemy
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
2018-02-11 18:02:41 +01:00
from dotenv import load_dotenv
dotenv_path = join(dirname(__file__), 'env')
load_dotenv(dotenv_path)
2018-01-31 23:17:52 +01:00
2018-01-31 00:42:22 +01:00
app = Flask(__name__)
# Settings
2018-01-31 23:17:52 +01:00
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URI')
2018-01-31 00:42:22 +01:00
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))
2018-01-31 23:17:52 +01:00
mail = db.Column(db.String(200))
2018-01-31 00:42:22 +01:00
password = db.Column(db.String(106))
created_at = db.Column(
db.DateTime, nullable=False, default=datetime.utcnow)
def __repr__(self):
return '<User Table {0}>'.format(self.username)
2018-02-05 23:59:51 +01:00
class Notice(db.Model):
2018-01-31 00:42:22 +01:00
'''
2018-02-05 23:59:51 +01:00
Table Notice
2018-01-31 00:42:22 +01:00
'''
2018-02-05 23:00:30 +01:00
2018-01-31 00:42:22 +01:00
__tablename__ = 'news'
2018-02-05 23:00:30 +01:00
2018-01-31 00:42:22 +01:00
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100))
2018-02-06 21:29:09 +01:00
url = db.Column(db.String(500))
2018-01-31 23:17:52 +01:00
user_id = db.Column(
db.Integer, db.ForeignKey('users.id'), nullable=False)
2018-01-31 00:42:22 +01:00
created_at = db.Column(
db.DateTime, nullable=False, default=datetime.utcnow)
2018-01-31 23:17:52 +01:00
# Relations
2018-01-31 00:42:22 +01:00
user = db.relationship(
2018-02-11 18:02:41 +01:00
'User',
backref=db.backref(
'Notice',
lazy=True,
cascade="all, delete-orphan"))
2018-01-31 00:42:22 +01:00
def __repr__(self):
2018-02-05 23:59:51 +01:00
return '<Notice Table {0}>'.format(self.title)
2018-01-31 00:42:22 +01:00
class Comment(db.Model):
'''
Table Comment
'''
2018-02-05 23:00:30 +01:00
2018-01-31 00:42:22 +01:00
__tablename__ = 'comments'
2018-02-05 23:00:30 +01:00
2018-01-31 00:42:22 +01:00
id = db.Column(db.Integer, primary_key=True)
text = db.Column(db.String(1000))
2018-02-05 23:59:51 +01:00
notice_id = db.Column(
2018-01-31 23:17:52 +01:00
db.Integer, db.ForeignKey('news.id'), nullable=False)
user_id = db.Column(
db.Integer, db.ForeignKey('users.id'), nullable=False)
2018-01-31 00:42:22 +01:00
created_at = db.Column(
db.DateTime, nullable=False, default=datetime.utcnow)
2018-01-31 23:17:52 +01:00
# Relations
user = db.relationship(
2018-02-11 18:02:41 +01:00
'User',
backref=db.backref(
'Comment',
lazy=True,
cascade="all, delete-orphan"))
notice = db.relationship('Notice', backref=db.backref(
'Comment', lazy=True, cascade="all, delete-orphan"))
2018-01-31 00:42:22 +01:00
def __repr__(self):
return '<Comment Table {0}>'.format(self.id)
if __name__ == "__main__":
manager.run()