-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
63 lines (41 loc) · 1.91 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
# -*- coding: utf-8 -*-
import datetime
from cotufa import db
class Movie(db.Model):
__tablename__ = 'movies'
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.Unicode(255), nullable=False)
year = db.Column(db.Integer, nullable=False)
cover = db.Column(db.Binary, nullable=True, default=None)
created_on = db.Column(db.DateTime, default=datetime.datetime.now, nullable=False)
seen_on = db.Column(db.Date, nullable=True, default=datetime.datetime.now)
rating = db.Column(db.Integer, nullable=False, default=0) # 1 to 3
notes = db.Column(db.UnicodeText, nullable=True, default=None)
@property
def directors(self):
return [x.person for x in self.people if x.role_type == 'director']
@property
def cast(self):
return [x for x in self.people if x.role_type == 'cast']
class Person(db.Model):
__tablename__ = 'persons'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.Unicode(255), nullable=False)
@classmethod
def get_or_create(cls, id, name):
id_ = int(id)
p = db.session.query(Person).filter_by(id=id_).first()
if p is None:
p = cls(id=id_, name=name)
db.session.add(p)
db.session.commit()
return p
class MovieRole(db.Model):
__tablename__ = 'roles'
id = db.Column(db.Integer, autoincrement=True, primary_key=True)
person_id = db.Column(db.Integer, db.ForeignKey(Person.id), nullable=False)
movie_id = db.Column(db.Integer, db.ForeignKey(Movie.id), nullable=False)
role_type = db.Column(db.Unicode(10), nullable=False, default=u'cast') # writer, director, cast
character = db.Column(db.Unicode(255), nullable=True, default=None)
movie = db.relationship('Movie', backref=db.backref('people'))
person = db.relationship('Person', backref=db.backref('roles'))