Skip to content

Commit 81d83d7

Browse files
Trying to get the file to PUT, POST, or whatever. Flask is infurtiating
1 parent df8d6f9 commit 81d83d7

File tree

7 files changed

+38
-14
lines changed

7 files changed

+38
-14
lines changed

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
/data
2-
/lib/venv/
2+
/lib/.env/
33
.idea
4+
*pycache*
5+
.vscode
6+
*.pycache
7+
*.pyc

lib/app/FileDelegate.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
1-
from recipe import Recipe
1+
from app.recipe import Recipe
22
import hashlib
33
import os
44

55
UPLOAD_FOLDER = os.getcwd() + "pdfs/"
66

7-
class CreationModificationDelegate:
7+
class FileDelegate:
88

99

1010
def addNew(self,properties,file):
11+
print("Adding New")
1112
hasher = hashlib.md5()
1213
buf=file.read()
1314
hasher.update(buf)
1415
new_name=hasher.hexdigest()+".pdf"
16+
print("Saving File")
1517
file.save(os.path.join(UPLOAD_FOLDER,new_name))
18+
print("File Saved")
1619
recipe=Recipe(title=properties["title"],filename=new_name,tags=properties["tags"],ingredients=properties["ingredients"])
20+
print("Saving to DB")
1721
recipe.save()
1822
return recipe.toJson()
1923

lib/app/RecipeQuery.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from flask.ext.mongoalchemy import BaseQuery
1+
from flask_mongoalchemy import BaseQuery
22

33
class Queries(BaseQuery):
44

@@ -16,3 +16,6 @@ def tagsIncludeAll(self,tags=[]):
1616

1717
def titleIsLike(self,match):
1818
return self.filter(match in self.type.title)
19+
20+
def fileIs(self,filename):
21+
return self.filter(filename == self.type.filename)

lib/app/TitleDelegate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ class UploadDelegate:
55
def getRecipesByTitle(self, title):
66
recipes = Recipe.query.titleIsLike(title)
77
toJson = list(map(lambda r: r.toJson()), recipes)
8-
return toJson
8+
return toJson

lib/app/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from flask import Flask
2-
from flask.ext.mongoalchemy import MongoAlchemy
2+
from flask_mongoalchemy import MongoAlchemy
33
app = Flask(__name__)
44
app.config["DEBUG"] = True
55
app.config['MONGOALCHEMY_DATABASE'] = "recipes"

lib/app/recipe.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import json
2-
from RecipeQuery import Queries
2+
from app.RecipeQuery import Queries
33
from app import db
44

55

@@ -8,7 +8,7 @@ class Recipe(db.Document):
88
title = db.StringField()
99
filename=db.StringField()
1010
ingredients=db.ListField(db.StringField())
11-
tags=db.ListField(db.StringField)
11+
tags=db.ListField(db.StringField())
1212

1313
def toJson(self):
1414
json.dumps(self.__dict__)

lib/app/routes.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from flask.json import jsonify
55
from flask import request
6+
from app.FileDelegate import FileDelegate
67

78

89
@app.route('/', methods=['GET'])
@@ -18,7 +19,7 @@ def searchByTags():
1819

1920

2021
@app.route('/search/byTags/all', methods=['GET'])
21-
def searchByTags():
22+
def searchByTagsAll():
2223
return jsonify("{}")
2324

2425

@@ -33,19 +34,31 @@ def searchByIngredients():
3334

3435

3536
@app.route("/search/ByIngredients/all", methods=['GET'])
36-
def searchByIngredients():
37+
def searchByIngredientsAll():
3738
return jsonify("{}")
3839

3940

40-
@app.route("/recipes/put", methods=['PUT'])
41+
@app.route("/recipes/put", methods=['POST'])
4142
def addRecipe():
42-
return jsonify("{}")
43+
delegate=FileDelegate()
44+
new_file=request.files['file']
45+
if new_file:
46+
json=delegate.addNew(file=request.files['file'],properties=request.args)
47+
return jsonify(json)
48+
return jsonify("{'error':'no file found'}")
4349

4450

4551
@app.route("/recipes/edit", methods=['POST'])
4652
def editRecipe():
47-
return jsonify("{}")
48-
53+
delegate=FileDelegate()
54+
json=delegate.edit(file=request.files['file'],properties=request.values)
55+
return jsonify(json)
56+
57+
@app.route("/recipes/remove",methods=['DELETE'])
58+
def removeRecipe():
59+
delegate=FileDelegate()
60+
json=delegate.remove(file=request.files['file'],properties=request.values)
61+
return jsonify(json)
4962

5063
@app.route('/recipes/tags', methods=['GET'])
5164
def getTags():

0 commit comments

Comments
 (0)