-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
executable file
·316 lines (227 loc) · 8.38 KB
/
model.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
from datetime import datetime
from flask_sqlalchemy import SQLAlchemy
from dataclasses import dataclass
db = SQLAlchemy()
@dataclass
class User(db.Model):
"""A user."""
user_id: int
fname: str
lname: str
email: str
pw: str
img: str
__tablename__ = "users"
user_id = db.Column(db.Integer,
autoincrement=True,
primary_key=True)
fname = db.Column(db.String(50), nullable=False)
lname = db.Column(db.String(50), nullable=False)
email = db.Column(db.String(50), unique=True, nullable=False)
pw = db.Column(db.String(30), nullable=False)
img = db.Column(db.String)
notes = db.relationship("Note")
likes = db.relationship("Like")
def __repr__(self):
return f'<User user_id={self.user_id} email={self.email}>'
@dataclass
class Doc(db.Model):
"""A document/article."""
doc_id: int
url: str
title: str
publish_date: datetime
owner: int #user?
created_at: datetime
body: str
# authors: Author
# img_urls: Img_Url
# tags: Tag
# followers: User
__tablename__ = "docs"
doc_id = db.Column(db.Integer,
autoincrement=True,
primary_key=True)
url = db.Column(db.String, nullable=False)
title = db.Column(db.String, nullable=False)
publish_date = db.Column(db.DateTime)
# body = db.Column(db.Text)
body = db.Column(db.String)
owner = db.Column(db.Integer, db.ForeignKey('users.user_id'))
created_at = db.Column(db.DateTime) #date added to Jot
authors = db.relationship("Author",
secondary="doc_authors",
backref="docs")
img_urls = db.relationship("Img_Url", backref="doc")
tags = db.relationship("Tag",
secondary="doc_tags",
backref="docs")
# followers = db.relationship("User",
# secondary="doc_followers",
# backref="followed_docs")
# followers = db.relationship("Doc_Follower", backref="doc")
notes = db.relationship("Note")
def __repr__(self):
return f'<Doc doc_id={self.doc_id} title={self.title}>'
@dataclass
class Author(db.Model):
"""An author"""
author_id: int
name: str
__tablename__ = "authors"
author_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
name = db.Column(db.String(50))
def __repr__(self):
return f'<Author author_id={self.author_id} name={self.name}>'
@dataclass
class Doc_Author(db.Model):
"""A document's author"""
doc_author_id: int
doc_id: int
author_id: int
__tablename__ = "doc_authors"
doc_author_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
doc_id = db.Column(db.Integer, db.ForeignKey('docs.doc_id'))
author_id = db.Column(db.Integer, db.ForeignKey('authors.author_id'))
@dataclass
class Img_Url(db.Model):
"""A document's image url."""
img_url_id: int
doc_id: int
url: str
__tablename__ = "img_urls"
img_url_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
doc_id = db.Column(db.Integer, db.ForeignKey('docs.doc_id'))
url = db.Column(db.String)
def __repr__(self):
return f'<Img_URL image_url_id={self.image_url_id} doc_id={self.doc_id}>'
@dataclass
class Tag(db.Model):
"""A tag"""
tag_id: int
tag: str
__tablename__ = "tags"
tag_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
tag = db.Column(db.String)
def __repr__(self):
return f'<Tag tag_id={self.tag_id} tag={self.tag}>'
@dataclass
class Doc_Tag(db.Model):
"""A document's tag"""
doc_tag_id: int
doc_id: int
tag_id: int
__tablename__ = "doc_tags"
doc_tag_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
doc_id = db.Column(db.Integer, db.ForeignKey('docs.doc_id'))
tag_id = db.Column(db.Integer, db.ForeignKey('tags.tag_id'))
@dataclass
class Doc_Follower(db.Model):
"""A document's follower."""
doc_follower_id: int
user_id: int
doc_id: int
created_at: datetime
accepted: bool
invite_msg: str
__tablename__ = "doc_followers"
doc_follower_id = db.Column(db.Integer,
autoincrement=True,
primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('users.user_id'))
doc_id = db.Column(db.Integer, db.ForeignKey('docs.doc_id'))
accepted = db.Column(db.Boolean, default=False)
created_at = db.Column(db.DateTime)
invite_msg = db.Column(db.String)
doc = db.relationship("Doc")
def __repr__(self):
return (f'<Doc_Follower doc_follower_id={self.doc_follower_id} '
f'user_id={self.user_id} '
f'doc_id={self.doc_id}>')
@dataclass
class Note(db.Model):
"""A note."""
note_id: int
user_id: int
doc_id: int
created_at: datetime
body: str
x_pos: float
y_pos: float
fname: str
lname: str
color: str
__tablename__ = "notes"
note_id = db.Column(db.Integer,
autoincrement=True,
primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('users.user_id'))
doc_id = db.Column(db.Integer, db.ForeignKey('docs.doc_id'))
parent_id = db.Column(db.Integer, db.ForeignKey('notes.note_id'))
created_at = db.Column(db.DateTime)
body = db.Column(db.Text)
x_pos = db.Column(db.Float)
y_pos = db.Column(db.Float)
fname = db.Column(db.String)
lname = db.Column(db.String)
color = db.Column(db.String, default='#C2D6C4')
doc = db.relationship("Doc")
user = db.relationship("User")
parent = db.relationship("Note")
likes = db.relationship("Like")
def __repr__(self):
return f'<Note note_id={self.note_id} x_pos={self.x_pos} y_pos={self.y_pos}>'
class Like(db.Model):
"""A like."""
__tablename__ = "likes"
like_id = db.Column(db.Integer,
autoincrement=True,
primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('users.user_id'))
note_id = db.Column(db.Integer, db.ForeignKey('notes.note_id'))
user = db.relationship("User")
note = db.relationship("Note")
def __repr__(self):
return f'<Like like_id={self.like_id} user_id={self.user_id} note_id={self.note_id}>'
class Relationship_Type(db.Model):
"""A type of user relationship"""
__tablename__ = "relationship_types"
relationship_type_id = db.Column(db.Integer,
autoincrement=True,
primary_key=True)
relationship_type = db.Column(db.String)
def __repr__(self):
return (f'<Relationship Type '
f'relationship_type_id={self.relationship_type_id} '
f'relationship_type={self.relationship_type}>')
class User_Relationship(db.Model):
"""A user relationship (friends, etc.)"""
__tablename__ = "user_relationships"
user_relationship_id = db.Column(db.Integer,
autoincrement=True,
primary_key=True)
inviter = db.Column(db.Integer, db.ForeignKey('users.user_id'))
acceptor = db.Column(db.Integer, db.ForeignKey('users.user_id'))
relationship_type_id = db.Column(db.Integer,
db.ForeignKey('relationship_types.relationship_type_id'))
created_at = db.Column(db.DateTime)
def __repr__(self):
return (f'<User Relationship '
f'user_relationship_id={self.user_relationship_id} '
f'user_1_id={self.inviter} '
f'user_2_id={self.acceptor}>')
def connect_to_db(flask_app, db_uri=None, echo=False):
flask_app.config['SQLALCHEMY_DATABASE_URI'] = db_uri or 'postgresql:///jot'
flask_app.config['SQLALCHEMY_ECHO'] = echo
flask_app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.app = flask_app
db.init_app(flask_app)
with flask_app.app_context():
db.create_all()
print('Connected to the db!')
if __name__ == '__main__':
from server import app
# Call connect_to_db(app, echo=False) if your program output gets
# too annoying; this will tell SQLAlchemy not to print out every
# query it executes.
connect_to_db(app)