-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
77 lines (52 loc) · 1.96 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""Models for Blogly."""
from flask_sqlalchemy import SQLAlchemy
import datetime
db = SQLAlchemy()
DEFAULT_PROFILE_PIC = "https://www.freeiconspng.com/uploads/name-people-person-user-icon--icon-search-engine-1.png"
class User(db.Model):
"""all users table"""
__tablename__ = "users"
id = db.Column(db.Integer, primary_key=True)
first_name = db.Column(db.Text, nullable=False)
last_name = db.Column(db.Text, nullable=False)
image_url = db.Column(db.Text, nullable=False, default=DEFAULT_PROFILE_PIC)
posts = db.relationship("Post", backref="user", cascade="all, delete-orphan")
def get_name(self):
"""Return full name of user."""
return f"{self.first_name} {self.last_name}"
def get_pic(self):
"""Return full name of user."""
return self.image_url
class Post(db.Model):
"""User posts"""
__tablename__ = "posts"
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.Text, nullable=False)
content = db.Column(db.Text, nullable=False)
created_at = db.Column(
db.DateTime,
nullable=False,
default=datetime.datetime.now)
user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
@property
def friendly_date(self):
"""Return nicely-formatted date."""
return self.created_at.strftime("%a %b %-d %Y, %-I:%M %p")
class Tag(db.Model):
"""Tag that can be added to posts."""
__tablename__ = 'tags'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.Text, nullable=False, unique=True)
posts = db.relationship(
'Post',
secondary="posts_tags",
backref="tags",
)
class PostTag(db.Model):
"""Tag on a post."""
__tablename__ = "posts_tags"
post_id = db.Column(db.Integer, db.ForeignKey('posts.id'), primary_key=True)
tag_id = db.Column(db.Integer, db.ForeignKey('tags.id'), primary_key=True)
def connect_db(app):
db.app = app
db.init_app(app)