summaryrefslogtreecommitdiffstats
path: root/victoria/models.py
blob: a37724ae982bd1e84497d52ff916d1526f95a190 (plain) (blame)
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
from victoria import db, login_manager
from flask_login import UserMixin
from datetime import datetime

@login_manager.user_loader
def load_user(artist_id):
    return Artist.query.get(int(artist_id))

# helper tables
artist_work = db.Table('artist_work',
    db.Column('artist_id', db.Integer, db.ForeignKey('artist.id'), primary_key=True),
    db.Column('work_id', db.Integer, db.ForeignKey('work.id'), primary_key=True)
)
artist_series = db.Table('artist_series',
    db.Column('artist_id', db.Integer, db.ForeignKey('artist.id'), primary_key=True),
    db.Column('series_id', db.Integer, db.ForeignKey('series.id'), primary_key=True)
)
series_work = db.Table('series_work',
    db.Column('series_id', db.Integer, db.ForeignKey('series.id'), primary_key=True),
    db.Column('work_id', db.Integer, db.ForeignKey('work.id'), primary_key=True)
)
tags = db.Table('tags',
    db.Column('tag', db.String(20), db.ForeignKey('tag.tag'), primary_key=True),
    db.Column('work_id', db.Integer, db.ForeignKey('work.id'), primary_key=True)
)

# models
class Artist(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(20), unique=True, nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)
    name = db.Column(db.String(120))
    profile_picture = db.Column(db.String(20))
    bio = db.Column(db.Text)

    works = db.relationship('Work', secondary=tags, lazy='subquery',
        backref=db.backref('artist', lazy=True))
    series = db.relationship('Series', secondary=tags, lazy='subquery',
        backref=db.backref('artist', lazy=True))

    def __repr__(self):
        return f"Artist('{self.username}', '{self.email}', '{self.name}')"

class Work(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    image_file = db.Column(db.String(20), unique=True, nullable=False)
    title = db.Column(db.String(120), nullable=False, default='Untitled')
    date = db.Column(db.DateTime),
    date_added = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
    description = db.Column(db.Text)
    alt_text = db.Column(db.Text)

    # tags
    tags = db.relationship('Tag', secondary=tags, lazy='subquery',
        backref=db.backref('works', lazy=True))
    # license
    license = db.Column(db.String(20), db.ForeignKey('license.license'))

    def __repr__(self):
        return f"Work('{self.title}', '{self.image_file}', '{self.alt_text}')"

class Series(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(120), nullable=False, default='Untitled')
    date = db.Column(db.DateTime),
    date_added = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
    location = db.Column(db.String(120))
    description = db.Column(db.Text)

    # tree self-relationship
    children = db.relationship('Series', backref='parent', lazy=True)
    parent_id = db.Column(db.Integer, db.ForeignKey('parent.id'), nullable=False)

    # many-to-many relationships
    # works
    works = db.relationship('Work', secondary=tags, lazy='subquery',
        backref=db.backref('series', lazy=True))


class Tag(db.Model):
    tag = db.Column(db.String(20), primary_key=True)

class License(db.Model):
    license = db.Column(db.String(20), primary_key=True)
    text = db.Column(db.Text)
    works = db.relationship('Work', backref='license', lazy=True)