From 08c086ba4d893f742ea7f7feff0431adc2be1ef3 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi <256895@studenti.unimore.it> Date: Fri, 30 Apr 2021 16:04:48 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=B9=20Cleanup=20repository=5Fcondition?= =?UTF-8?q?s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/repository_conditions.py | 41 ++++++++++++------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/code/backend/nest_backend/routes/repository/repository_conditions.py b/code/backend/nest_backend/routes/repository/repository_conditions.py index 8672d2e..ab553bf 100644 --- a/code/backend/nest_backend/routes/repository/repository_conditions.py +++ b/code/backend/nest_backend/routes/repository/repository_conditions.py @@ -14,26 +14,37 @@ def page_repository_conditions(rid): + GET: Returns the conditions of the specified repo. + POST: type, content -> Adds a condition and returns it. """ + repository = Repository.query.filter_by(rid=rid).first() user = find_user(get_jwt_identity()) + if user.email != repository.owner_id: return json_error("You are not authorized."), 403 + if request.method == "GET": return json_success([u.condition.to_json() for u in repository.uses]) + if request.method == "POST": - type = request.json.get("type") - if not type or type not in dir(ConditionType): - return json_error("Could not understand the type of the condition."), 400 - content = request.json.get("content") - if not content: - return json_error("Could not find the content"), 400 - condition = Condition.query.filter(Condition.content.ilike(str(content))).filter_by(type=ConditionType.__getattr__(str(type)).value).first() - if not condition: - condition = Condition(content=content, type=ConditionType.__getattr__(str(type)).value) - Base.session.add(condition) - repository = Repository.query.filter_by(request.json.get("id")) - if Uses.query.filter_by(cid=condition.id, rid=repository.id): - return json_error("This condition is already connected to the repository."), 406 - Base.session.add(Uses(cid=condition.id, rid=repository.id)) + if not (type_ := request.json.get("type")): + return json_error("Missing `type` parameter."), 400 + + try: + type_ = ConditionType(type_) + except KeyError: + return json_error("Unknown `type` specified."), 400 + + if not (content := request.json.get("content")): + return json_error("Missing `content` parameter."), 400 + + if not (repo_id := request.json.get("id")): + return json_error("Missing `id` parameter."), 400 + + condition = Condition(content=content, type=type_) + Base.session.merge(condition) + + repository = Repository.query.get(repo_id) + use = Uses(cid=condition.id, rid=repository.id) + Base.session.merge(use) + Base.session.commit() - return json_success(condition.to_json()), 200 \ No newline at end of file + return json_success(condition.to_json()), 200