-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmodels.py
More file actions
54 lines (40 loc) · 1.57 KB
/
models.py
File metadata and controls
54 lines (40 loc) · 1.57 KB
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
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class Base(db.Model):
__abstract__ = True
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
date_created = db.Column(db.DateTime, default=db.func.current_timestamp())
date_modified = db.Column(db.DateTime, default=db.func.current_timestamp(),
onupdate=db.func.current_timestamp())
# Model to store user details
class User(Base):
email = db.Column(db.String(100), unique=True)
username = db.Column(db.String(50), unique=True)
password = db.Column(db.String(200))
favorites = db.relationship('Favorite', backref='favorite',
lazy='dynamic')
def __repr__(self):
return self.username
# Model for favorite photos
class Favorite(Base):
user_id = db.Column(db.ForeignKey('user.id'))
title = db.Column(db.String(500))
link = db.Column(db.String(500))
date_taken = db.Column(db.DateTime)
published = db.Column(db.DateTime)
author = db.Column(db.String(100))
author_id = db.Column(db.String(50))
tags = db.Column(db.String(50))
def __repr__(self):
return self.title
# returns dictionary that can easily be jsonified
def to_json(self):
return {
'title': self.title,
'link': self.link,
'date_taken': self.date_taken,
'published': self.published,
'author': self.author,
'author_id': self.author_id,
'tags': ', '.join(self.tags.split(' '))
}