def mutate(root, info, question_id, reaction_data): errors = {} current_user = get_jwt_identity() user = UserModel.find_by_id(current_user["id"]) if not user: errors["user"] = "******" question = QuestionModel.find_by_id(question_id) if not question: errors["question"] = "not found" if errors: raise GraphQLError(json.dumps(errors)) reaction = ReactionModel(user=user, **reaction_data) if not question.reactions: question.reactions = [reaction] else: old_reaction = list( filter(lambda r: r.user == reaction.user, question.reactions)) if old_reaction: if old_reaction[0].reaction != reaction.reaction: question.reactions.remove(old_reaction[0]) question.reactions.append(reaction) else: question.reactions.append(reaction) question.save() return ReactToQuestion(question=question, reactions=question.reactions, ok=True)
def mutate(root, info, question_id, file_name): errors = {} current_user = get_jwt_identity() user = UserModel.find_by_id(current_user["id"]) if not user: errors["user"] = "******" question = QuestionModel.find_by_id(question_id) if not question: errors["question"] = "not found" if user != question.created_by: errors["user"] = "******" if file_name not in question.images: errors["question"] = "does not have an image called {}".format( file_name) if errors: raise GraphQLError(json.dumps(errors)) file_path = ROOT_PATH + url_for("static", filename="img/{}".format(file_name)) os.remove(file_path) question.images.remove(file_name) question.save() return DeleteImg(question=question, ok=True)
def upload_img(question_id): if 'img' not in request.files: print("'img' key not found") abort(404) # Not Found file = request.files['img'] if file.filename == '': abort(400) # Bad Request question = Question.find_by_id(question_id) if not question: print("Question not found") abort(404) # Not Found file_name = file.filename _, ext = file_name.rsplit('.', 1) if ext.lower() not in ALLOWED_FORMATS: abort(406) # Not Acceptable file_name = "{}.{}".format(token_urlsafe(16), ext) path = ROOT_PATH + url_for("static", filename="img/{}".format(file_name)) file.save(path) if question.images: question.images.append(file_name) else: question.images = [file_name] question.save() return redirect(url_for("web.img", file_name=file_name))
def mutate(root, info, question_id, answer_data): errors = {} current_user = get_jwt_identity() user = UserModel.find_by_id(current_user["id"]) if not user: errors["user"] = "******" question = QuestionModel.find_by_id(question_id) if not question: errors["question"] = "not found" if errors: raise GraphQLError(json.dumps(errors)) answer = AnswerModel(created_by=user, **answer_data) answer.save() if question.answers: question.answers.append(answer) else: question.answers = [answer] question.save() return AnswerQuestion(answer=answer, ok=True)
def resolve_question(root, info, _id): question = QuestionModel.find_by_id(_id) return question