You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
1.7 KiB
Python
45 lines
1.7 KiB
Python
2 years ago
|
from werkzeug.security import generate_password_hash, check_password_hash
|
||
|
from nanoid import generate
|
||
|
from . import db
|
||
|
|
||
|
class User(db.Model):
|
||
|
def __init__(self, username, blog_url):
|
||
|
self.username = username
|
||
|
self.blog_url = blog_url
|
||
|
__tablename__ = 'users'
|
||
|
id = db.Column(db.Integer, primary_key=True)
|
||
|
username = db.Column(db.Text, unique=True)
|
||
|
password = db.Column(db.Text)
|
||
|
blog_url = db.Column(db.Text, db.ForeignKey('blogs.url', ondelete='CASCADE'), unique=True)
|
||
|
blog = db.relationship('Blog', backref='blog_user', cascade="all,delete")
|
||
|
def set_password(self, password):
|
||
|
self.password = generate_password_hash(password)
|
||
|
|
||
|
def check_password(self, password):
|
||
|
return check_password_hash(self.password, password)
|
||
|
|
||
|
class Blog(db.Model):
|
||
|
def __init__(self, url, is_private=False):
|
||
|
self.url = url
|
||
|
self.is_private = is_private
|
||
|
__tablename__ = "blogs"
|
||
|
id = db.Column(db.Integer, primary_key=True)
|
||
|
url = db.Column(db.Text, unique=True)
|
||
|
is_private = db.Column(db.Boolean, default=False) #TODO this is must be private (VULN)
|
||
|
user = db.relationship('User', backref='blog_user', cascade="all,delete", lazy='dynamic')
|
||
|
posts = db.relationship("Post", cascade="all,delete")
|
||
|
|
||
|
|
||
|
#TODO дописать стуруктуру БД
|
||
|
|
||
|
class Post(db.Model):
|
||
|
def __init__(self, title, body, blog_url):
|
||
|
self.title = title
|
||
|
self.body = body
|
||
|
self.blog_url = blog_url
|
||
|
__tablename__ = "posts"
|
||
|
id = db.Column(db.Integer, primary_key=True)
|
||
|
title = db.Column(db.String)
|
||
|
body = db.Column(db.String)
|
||
|
blog_url = db.Column(db.String, db.ForeignKey("blogs.url"))
|
||
|
blog = db.relationship("Blog", cascade="all,delete")
|