1
Fork 0
mirror of https://github.com/pds-nest/nest.git synced 2024-11-25 14:34:19 +00:00

🧹 Cleanup repository_conditions

This commit is contained in:
Stefano Pigozzi 2021-04-30 16:04:48 +02:00
parent 1daa35f9d7
commit 08c086ba4d
Signed by untrusted user who does not match committer: steffo
GPG key ID: 6965406171929D01

View file

@ -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