Skip to content

Commit 0232a98

Browse files
Finally figured out how to add files to database and save them off.
1 parent 81d83d7 commit 0232a98

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
.vscode
66
*.pycache
77
*.pyc
8+
*.pdf

lib/app/FileDelegate.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
from app.recipe import Recipe
22
import hashlib
33
import os
4+
import shutil
45

5-
UPLOAD_FOLDER = os.getcwd() + "pdfs/"
6+
UPLOAD_FOLDER = os.getcwd() + "/pdfs/"
67

78
class FileDelegate:
89

910

1011
def addNew(self,properties,file):
1112
print("Adding New")
13+
ingredients=properties['ingredients'].split(',')
14+
tags=properties['tags'].split(',')
1215
hasher = hashlib.md5()
1316
buf=file.read()
1417
hasher.update(buf)
1518
new_name=hasher.hexdigest()+".pdf"
16-
print("Saving File")
17-
file.save(os.path.join(UPLOAD_FOLDER,new_name))
19+
print("Saving File to "+os.path.join(UPLOAD_FOLDER,new_name))
20+
with open(os.path.join(UPLOAD_FOLDER,new_name),'wb') as f:
21+
f.write(buf)
22+
f.close()
1823
print("File Saved")
19-
recipe=Recipe(title=properties["title"],filename=new_name,tags=properties["tags"],ingredients=properties["ingredients"])
24+
recipe=Recipe(title=properties["title"],filename=new_name,tags=tags,ingredients=ingredients)
2025
print("Saving to DB")
2126
recipe.save()
2227
return recipe.toJson()

lib/app/recipe.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ class Recipe(db.Document):
1111
tags=db.ListField(db.StringField())
1212

1313
def toJson(self):
14-
json.dumps(self.__dict__)
14+
json={}
15+
json['title']=self.title
16+
json['filename']=self.filename
17+
json['ingredients']=self.ingredients
18+
json['tags']=self.tags
19+
return json
1520

1621

0 commit comments

Comments
 (0)