def deleteRecipe(obj): recipe_object = models.Recipe.query.get(obj['id']) if recipe_object is None: return errorhelper.generateError("recipe doesn't exist", 400) recipe_author = getUser(recipe_object.author) if recipe_author is None: return errorhelper.generateError("recipe author doesn't exist", 400) recipe_author.deletes_recipe(recipe_object.rating) models.db.session.delete(recipe_object) return commitResponse(models.db.session, {})
def refreshAuthToken(): obj = request.get_json(silent=True) if (obj['token'] is None): err = errorhelper.generateError("no token", 400) return handleResponse(err) user = dbapi.getUserByToken(obj['token']) #try token:unused auth if user is None: #try user:pass auth err = errorhelper.generateError( "invalid token {}".format(obj['token']), 400) return handleResponse(err) #For now generate a new token token = dbapi.generateAuthToken(user) res = {} res['data'] = {'token': token.decode('ascii')} return handleResponse(res, 200)
def deleteComment(): obj = request.get_json(silent=True) if g.user.id is not obj['author']: err = errorhelper.generateError("incorrect user id", 400) return handleResponse(err) res = dbapi.deleteComment(obj) return handleResponse(res, 204)
def getRecipe(rid): res = {} recipe = dbapi.getRecipe(rid) if recipe is None: err = errorhelper.generateError("recipe doesn't exist", 400) return handleResponse(err) res['data'] = recipe return handleResponse(res)
def voteComment(obj): comment_object = getCommentById(obj['comment']) if comment_object is None: return errorhelper.generateError("comment doesn't exist", 400) comment_author = getUser(comment_object.author) if comment_author is None: return errorhelper.generateError("comment author doesn't exist", 400) user = getUser(obj['voter']) if user is None: return errorhelper.generateError("user doesn't exist", 400) vote_object = models.Vote.query.filter_by(voter=obj['voter'], comment=obj['comment']).first() old_vote = 'new' if vote_object is None: vote_object = models.Vote(voter=obj['voter'], comment=obj['comment'], good=obj['good']) user.gives_vote(vote_object.good) comment_object.gets_vote(vote_object) comment_author.comment_get_vote(vote_object.good) else: old_vote = 'changed' if vote_object.good != obj['good'] else 'unchanged' user.changes_vote(vote_object.good, obj['good']) comment_author.comment_votes_change(vote_object.good, obj['good']) comment_object.vote_changes(vote_object, obj['good']) res = {} try: models.db.session.commit() res['data'] = vote_object.to_json() res['data']['old_vote'] = old_vote except: models.db.session.rollback() finally: return res return commitResponse(models.db.session, {}, vote_object)
def handleResponse(res, success=200): response = jsonify(res) if 'data' in res: response.status_code = success elif 'error' in res: response.status_code = res['error']['code'] else: response = jsonify( errorhelper.generateError(500, 'internal server error')) response.status_code = 500 return response
def createUser(obj): if userExists(obj['username'], obj['email']): return errorhelper.generateError('username or password already exist', 400) user_object = models.User(email=obj['email'], username=obj['username'], cooking_points=0) hashPassword(user_object, obj['password']) addRoleToUser(user_object, getRole(1)) models.db.session.add(user_object) return commitResponse(models.db.session, {}, user_object)
def deleteComment(obj): comment_object = getCommentById(obj['id']) if comment_object is None: return errorhelper.generateError("comment doesn't exist", 400) recipe_object = getRecipe(comment_object.recipe) if recipe_object is None: return errorhelper.generateError("recipe doesn't exist", 400) recipe_author = getUser(recipe_object.author) if recipe_author is None: return errorhelper.generateError("recipe author doesn't exist", 400) user = getUser(obj['author']) if user is None: return errorhelper.generateError("user doesn't exist", 400) recipe_author.loses_comment() user.deletes_comment(comment_object.popularity) models.db.session.delete(comment_object) return commitResponse(models.db.session, {})
def rateRecipe(obj): rating = obj['rating'] if 'rating' in obj else -1 if rating < 0.5 or rating > 5: return errorhelper.generateError("rating must be 1-5 integer", 400) recipe_object = models.Recipe.query.get(obj['recipe']) if recipe_object is None: return errorhelper.generateError("recipe doesn't exist", 400) user = getUser(obj['rater']) if user is None: return errorhelper.generateError("user doesn't exist", 400) rating_object = models.Rating.query\ .filter_by(rater=obj['rater'], recipe=obj['recipe'])\ .first() old_rating = -1 if rating_object is None: rating_object = models.Rating(rater=obj['rater'], recipe=obj['recipe'], rating=obj['rating']) recipe_object.gets_rating(rating_object) user.gives_rating() else: old_rating = rating_object.rating recipe_object.rating_changes(rating_object, obj['rating']) res = {} try: models.db.session.commit() res['data'] = rating_object.to_json() res['data']['old_rating'] = old_rating except: models.db.session.rollback() finally: return res
def createComment(obj): recipe_object = models.Recipe.query.get(obj['recipe']) if recipe_object is None: return errorhelper.generateError("recipe doesn't exist", 400) user = getUser(obj['author']) if user is None: return errorhelper.generateError("user doesn't exist", 400) recipe_author = getUser(recipe_object.author) if recipe_author is None: return errorhelper.generateError("recipe author doesn't exist", 400) comment_object = models.Comment(author=obj['author'], recipe=obj['recipe'], text=obj['text'], popularity=0, voters=0, published=currentTime()) recipe_object.comments.append(comment_object) recipe_author.gets_comment() return commitResponse(models.db.session, {}, comment_object)
def createRecipe(obj): # ingredients = '' #for i in obj['ingredients']: # name += ('name' in i) ? + # "{}, {}, {}".format(i['name'], i['amount'], i['unit']) recipe_object = models.Recipe( author=obj['author'], name=obj['name'], description=obj['description'], #A bit hacky. Should store ingredients in database models. ingredients=' | '.join(["{}, {}, {}".format(i['name'], i['amount'], i['unit'])\ for i in obj['ingredients']]), guide=obj['guide'], picture_url=obj['picture_url'], rating=0, raters=0, published=currentTime() ) user = getUser(obj['author']) if user is None: return errorhelper.generateError("user doesn't exist", 400) user.recipes.append(recipe_object) for category_name in obj['categories']: category_name = category_name.lower() exist_category = getCategoryByName(category_name) cr = models.CategoryRecipe() if exist_category is not None: exist_category.category_recipes.append(cr) recipe_object.categories.append(cr) else: new_category = models.Category(name=category_name) new_category.category_recipes.append(cr) recipe_object.categories.append(cr) res = commitResponse(models.db.session, {}, recipe_object) user = user.to_json() res['data']['categories'] = [ getCategoryById(c).to_json() for c in res['data']['categories'] ] res['data']['author'] = {'id': user['id'], 'name': user['username']} return res