-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
74 lines (58 loc) · 2.23 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
import datetime
from flask.ext.sqlalchemy import SQLAlchemy
ROLE_USER = 0
ROLE_MUSIC_OWNER = 1
ROLE_ADMIN = 2
db = SQLAlchemy()
class ModelMixin(object):
def __repr__(self):
return unicode(self.to_dict)
@property
def to_dict(self):
return dict(
(col, getattr(self, col)) for col in self.__table__.columns.keys())
class User(db.Model, ModelMixin):
__tablename__ = "users"
id = db.Column(db.Integer, primary_key=True)
google_user_id = db.Column(db.String(64), unique=True, index=True)
name = db.Column(db.Unicode(64))
email = db.Column(db.String(64))
active = db.Column(db.Boolean(), default=True)
role = db.Column(db.SmallInteger, default=ROLE_USER)
profile_url = db.Column(db.String(128))
image_url = db.Column(db.String(128))
created_on = db.Column(db.DateTime, default=datetime.datetime.utcnow)
#TODO(lukaszma) too short -> 67
refresh_token = db.Column(db.String(64))
videos = db.relationship("Video", backref="user", lazy="dynamic")
def __unicode__(self):
return self.email
@property
def is_admin(self):
return self.role == ROLE_ADMIN
@property
def is_music_owner(self):
return self.role == ROLE_MUSIC_OWNER
class Music(db.Model, ModelMixin):
__tablename__ = "music"
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.Unicode(64))
url = db.Column(db.String(128))
video_id = db.Column(db.Integer, db.ForeignKey("videos.id"))
artist_id = db.Column(db.Integer, db.ForeignKey("artists.id"))
category = db.Column(db.Unicode(64))
tag = db.Column(db.Unicode(20))
class Artist(db.Model, ModelMixin):
__tablename__ = "artists"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.Unicode(64))
music = db.relationship("Music", backref="artist")
class Video(db.Model, ModelMixin):
__tablename__ = "videos"
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.Unicode(128))
url = db.Column(db.String(64))
views = db.Column(db.Integer, default=0)
user_id = db.Column(db.Integer, db.ForeignKey("users.id"))
music = db.relationship("Music", backref="video")
created_on = db.Column(db.DateTime, default=datetime.datetime.utcnow)