-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
175 lines (127 loc) · 4.78 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
168
169
170
171
172
173
174
175
from enum import Enum
from socket import gethostname
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import (
Boolean,
Column,
DateTime,
Float,
ForeignKey,
Integer,
String,
Table,
UniqueConstraint
)
from sqlalchemy.orm import DeclarativeBase, registry, relationship
from configs import get_current_datetime
mapper_registry = registry()
class UserRole(Enum):
admin = 1
user = 2
class Base(DeclarativeBase):
pass
db = SQLAlchemy(model_class=Base)
class User(db.Model):
__tablename__ = "user"
id = Column(Integer, primary_key=True, index=True)
created_datetime = Column(DateTime(timezone=True), default=get_current_datetime)
last_modified_datetime = Column(DateTime(timezone=True), default=get_current_datetime)
first_name = Column(String)
last_name = Column(String)
username = Column(String, unique=True, nullable=False, index=True)
description = Column(String)
email = Column(String)
password = Column(String)
role = Column(Integer, default=UserRole.user.value)
posts = relationship("Post", back_populates="user", cascade="all, delete")
def __unicode__(self):
return self.username
class Contact(db.Model):
__tablename__ = "contact"
id = Column(Integer, primary_key=True, index=True)
name = Column(String)
email = Column(String)
message = Column(String, nullable=False)
created_datetime = Column(DateTime(timezone=True), nullable=False, default=get_current_datetime)
class Log(db.Model):
__tablename__ = "log"
id = Column(Integer, primary_key=True, index=True)
x_forwarded_for = Column(String, index=True)
remote_addr = Column(String, index=True)
referrer = Column(String, index=True)
content_md5 = Column(String)
origin = Column(String)
scheme = Column(String)
method = Column(String)
path = Column(String, index=True)
query_string = Column(String)
duration = Column(Float)
start_datetime_utc = Column(DateTime(timezone=True))
end_datetime_utc = Column(DateTime(timezone=True))
user_agent = Column(String)
accept = Column(String)
accept_language = Column(String)
accept_encoding = Column(String)
content_length = Column(Integer)
bridge_tag = Table(
"bridge_tag",
db.metadata,
Column("post_id", ForeignKey("post.id"), primary_key=True),
Column("tag_id", ForeignKey("tag.id"), primary_key=True),
UniqueConstraint("post_id", "tag_id"),
)
class BridgeTag:
pass
mapper_registry.map_imperatively(BridgeTag, bridge_tag)
class File(db.Model):
__tablename__ = "file"
id = Column(Integer, primary_key=True, index=True)
server = Column(String, server_default=gethostname())
relative_path = Column(String)
server_file_name = Column(String, unique=True)
file_name = Column(String)
file_type = Column(String)
upload_date = Column(DateTime(), default=get_current_datetime)
post_id = Column(Integer, ForeignKey("post.id"), nullable=False)
post = relationship("Post", back_populates="files")
def __unicode__(self):
return self.file_name
class Tag(db.Model):
__tablename__ = "tag"
id = Column(Integer, primary_key=True, index=True)
text = Column(String, unique=True)
posts = relationship("Post", back_populates="tags", secondary=bridge_tag)
def __unicode__(self):
return self.text
class Post(db.Model):
"""
- The Post-User relationship is many-to-one.
- The Post-File relationship is one-to-many.
- The Post-Tag relationship is many-to-many.
- The Post-Comment relationship is one-to-many.
"""
__tablename__ = "post"
id = Column(Integer, primary_key=True, index=True)
title = Column(String)
text_html = Column(String)
text_markdown = Column(String)
published_date = Column(DateTime(), default=get_current_datetime)
last_modified_date = Column(DateTime(), default=get_current_datetime)
is_published = Column(Boolean, default=True)
user_id = Column(Integer, ForeignKey("user.id"), nullable=False)
user = relationship("User", back_populates="posts")
comments = relationship("Comment", back_populates="post", cascade="all, delete")
files = relationship("File", back_populates="post", cascade="all, delete")
tags = relationship("Tag", back_populates="posts", secondary=bridge_tag)
def __unicode__(self):
return self.title
class Comment(db.Model):
__tablename__ = "comment"
id = Column(Integer, primary_key=True, index=True)
title = Column(String)
text = Column(String)
published_date = Column(DateTime(timezone=True), default=get_current_datetime)
post_id = Column(Integer, ForeignKey("post.id"), nullable=False)
post = relationship("Post", back_populates="comments")
def __unicode__(self):
return self.title