/
models.py
167 lines (132 loc) · 4.76 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import re
from datetime import datetime
from sqlalchemy.orm import relationship, backref
from sqlalchemy import (
Table,
Column,
Integer,
String,
Text,
DateTime,
ForeignKey,
)
from db import Base
from auth.utils import password_context, get_hashed_password
class User(Base):
"""Model for storing information about users."""
__tablename__ = "users"
id = Column(Integer, primary_key=True)
username = Column(String(50), nullable=False, unique=True)
email = Column(String(255), nullable=False, unique=True)
password = Column(String(110), nullable=False)
created = Column(DateTime, default=datetime.today())
avatar = Column(String, nullable=True, default="media/avatars/default.png")
posts = relationship(
"Post",
backref=backref("user", lazy="joined"),
lazy="dynamic",
passive_deletes=True,
)
comments = relationship(
"Comment",
backref=backref("user", lazy="joined"),
lazy="dynamic",
passive_deletes=True,
)
likes = relationship(
"Like",
backref=backref("user", lazy="joined"),
lazy="dynamic",
passive_deletes=True,
)
def __init__(self, *args, **kwargs) -> None:
"""Adds hashed password to user instance."""
super(User, self).__init__(*args, **kwargs)
self.password = get_hashed_password(self.password)
def is_valid_(self, password: str) -> bool:
"""Checks if given password is valid."""
return password_context.verify(password, self.password)
class _ModelWithSlug:
"""Mixin for models with 'slug' column."""
def __str__(self) -> str:
"""Returns string representation of model instance."""
return f"<{self.title}>"
def __repr__(self) -> str:
"""Returns string representation of model instance."""
return f"<{self.title}>"
def generate_slug(self) -> None:
"""Generates slug from title."""
self.slug = self._get_correct_slug_from_(self.title)
def _get_correct_slug_from_(self, title: str) -> str:
"""Returns correct slug from given string."""
return re.sub(pattern=r"[^\w+]", repl="-", string=title).lower()
# Table for many-to-many relationship between posts and tags
post_tags = Table(
"post_tags",
Base.metadata,
Column("post_id", Integer, ForeignKey("posts.id")),
Column("tag_id", Integer, ForeignKey("tags.id")),
)
class Post(_ModelWithSlug, Base):
"""Model for storing information about posts"""
__tablename__ = "posts"
id = Column(Integer, primary_key=True)
title = Column(String(70), nullable=False, unique=True)
slug = Column(String(70), nullable=True, unique=True)
body = Column(Text, nullable=False)
created = Column(DateTime, default=datetime.today())
user_id = Column(
Integer, ForeignKey("users.id", ondelete="CASCADE"), nullable=False
)
tags = relationship(
"Tag", secondary=post_tags, backref=backref("posts", lazy="dynamic")
)
comments = relationship(
"Comment",
backref=backref("post", lazy="joined"),
lazy="dynamic",
passive_deletes=True,
)
likes = relationship(
"Like",
backref=backref("post", lazy="joined"),
lazy="dynamic",
passive_deletes=True,
)
def __init__(self, *args, **kwargs) -> None:
"""For setting correct slug during initializing"""
super(Post, self).__init__(*args, **kwargs)
self.generate_slug()
class Tag(_ModelWithSlug, Base):
"""Model for storing information about tags"""
__tablename__ = "tags"
id = Column(Integer, primary_key=True)
title = Column(String(30), nullable=False, unique=True)
slug = Column(String(30), nullable=False, unique=True)
def __init__(self, *args, **kwargs) -> None:
"""For setting correct slug during initializing"""
super(Tag, self).__init__(*args, **kwargs)
self.generate_slug()
class Comment(Base):
"""Model for storing information about comments"""
__tablename__ = "comments"
id = Column(Integer, primary_key=True)
body = Column(Text, nullable=False)
created = Column(DateTime, default=datetime.today())
user_id = Column(
Integer, ForeignKey("users.id", ondelete="CASCADE"), nullable=False
)
post_id = Column(
Integer, ForeignKey("posts.id", ondelete="CASCADE"), nullable=False
)
class Like(Base):
"""Model for storing information about likes"""
__tablename__ = "likes"
id = Column(Integer, primary_key=True)
created = Column(DateTime, default=datetime.today())
user_id = Column(
Integer, ForeignKey("users.id", ondelete="CASCADE"), nullable=False
)
post_id = Column(
Integer, ForeignKey("posts.id", ondelete="CASCADE"), nullable=False
)