-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
187 lines (151 loc) · 3.98 KB
/
models.py
File metadata and controls
187 lines (151 loc) · 3.98 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
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
""" models for pixly """
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
def connect_db(app):
""" connects this database to the provided flask app"""
app.app_context().push()
db.app = app
db.init_app(app)
class Image(db.Model):
""" model for images table"""
__tablename__ = "images"
###### TABLE COLUMNS ######
id = db.Column(
db.Integer,
primary_key=True,
autoincrement=True
)
file_name = db.Column(
db.Text,
nullable=False,
unique=True
)
title = db.Column(
db.String(50),
nullable=False
)
caption = db.Column(
db.String(100),
nullable=True,
)
upload_at = db.Column(
db.DateTime,
nullable=False,
default=db.func.now()
)
views = db.Column(
db.Integer,
default=0,
nullable=False
)
photographer = db.Column(
db.String(50),
nullable=True
)
###### RELATIONSHIPS ######
exif_data = db.relationship('EXIFData', backref='image')
tags = db.relationship('Tag', secondary='images_tags', backref='images')
###### INSTANCE METHOD ######
def __repr__(self):
return f'<Image {self.id} {self.title} {self.file_name}>'
def serialize(self):
""" serialize to dictionary """
return {
"id":self.id,
"file_name":self.file_name,
"title":self.title,
"caption":self.caption,
"upload_at":self.upload_at,
"views":self.views,
"photographer":self.photographer
}
class EXIFData(db.Model):
""" Model for exif_data table """
__tablename__ = 'exif_data'
###### TABLE COLUMNS ######
image_id = db.Column(
db.Integer,
db.ForeignKey('images.id', ondelete='CASCADE'),
primary_key=True
)
height_px = db.Column(
db.Integer,
nullable=False
)
width_px = db.Column(
db.Integer,
nullable=False
)
device_manufacturer = db.Column(
db.String(100),
nullable=True
)
device_model = db.Column(
db.String(100),
nullable=True
)
focal_length = db.Column(
db.Integer,
nullable=True
)
f_stop = db.Column(
db.Float,
nullable=True
)
exposure = db.Column(
db.Integer,
nullable=True
)
location = db.Column(
db.String(100),
nullable=True
)
taken_at = db.Column(
db.String(100),
nullable=True
)
###### INSTANCE METHODS ######
def __repr__(self):
return f'<EXIFData Image {self.image_id}: {self.image.title}>'
def serialize(self):
""" serialize to dictionary """
return {
"image_id":self.image_id,
"height_px":self.height_px,
"width_px":self.width_px,
"device_manufacturer":self.device_manufacturer,
"device_model":self.device_model,
"focal_length":self.focal_length,
"f_stop":self.f_stop,
"exposure":self.exposure,
"location":self.location,
"taken_at":self.taken_at
}
class Tag(db.Model):
""" Model for tags table """
__tablename__ = 'tags'
###### TABLE COLUMNS ######
name = db.Column(
db.String(30),
primary_key = True
)
###### INSTANCE METHODS ######
def __repr__(self):
return f'<Tag {self.name}>'
class ImageTag(db.Model):
""" Model for images - tags association table """
__tablename__ = 'images_tags'
###### TABLE COLUMNS ######
image_id = db.Column(
db.Integer,
db.ForeignKey('images.id', ondelete='CASCADE'),
primary_key=True
)
tag_name = db.Column(
db.String(30),
db.ForeignKey('tags.name', ondelete='CASCADE'),
primary_key=True
)
###### INSTANCE METHODS ######
def __repr__(self):
return f'<ImageTag Image {self.image_id}: Tag {self.tag_name}>'