-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.py
40 lines (32 loc) · 1.15 KB
/
schema.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
from mongoengine import connect, Document, FileField, StringField
connect("imageRepo")
class Image(Document):
"""
MongoDB Document for the image Collection.
title: string -- title of the image
description: string -- description of the image
photo: GridFS Object -- image stored in bytes, managed with GridFS
"""
title = StringField(required=True)
description = StringField(required=True)
photo = FileField(required=True)
meta = {"strict": False}
def addImage(filepath, title, description):
"""
Creates image in the MongoDB database based on parameters
filepath: string -- path to the file of image
title: string -- title of the image
description: string -- description of the image
"""
image = Image(title=title)
image.description = description
fileHandle = open(filepath, "rb")
image.photo.put(fileHandle, filename=filepath)
image.save()
if __name__ == "__main__":
# instantiate variable to test the add image function
filepath = "Dog"
title = "Dog in sweater"
description = "Dog.jpg"
# run the addImage function
addImage(filepath, title, description)