1
Fork 0
mirror of https://github.com/pds-nest/nest.git synced 2024-11-22 13:04: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. + GET: Returns the conditions of the specified repo.
+ POST: type, content -> Adds a condition and returns it. + POST: type, content -> Adds a condition and returns it.
""" """
repository = Repository.query.filter_by(rid=rid).first() repository = Repository.query.filter_by(rid=rid).first()
user = find_user(get_jwt_identity()) user = find_user(get_jwt_identity())
if user.email != repository.owner_id: if user.email != repository.owner_id:
return json_error("You are not authorized."), 403 return json_error("You are not authorized."), 403
if request.method == "GET": if request.method == "GET":
return json_success([u.condition.to_json() for u in repository.uses]) return json_success([u.condition.to_json() for u in repository.uses])
if request.method == "POST": if request.method == "POST":
type = request.json.get("type") if not (type_ := request.json.get("type")):
if not type or type not in dir(ConditionType): return json_error("Missing `type` parameter."), 400
return json_error("Could not understand the type of the condition."), 400
content = request.json.get("content") try:
if not content: type_ = ConditionType(type_)
return json_error("Could not find the content"), 400 except KeyError:
condition = Condition.query.filter(Condition.content.ilike(str(content))).filter_by(type=ConditionType.__getattr__(str(type)).value).first() return json_error("Unknown `type` specified."), 400
if not condition:
condition = Condition(content=content, type=ConditionType.__getattr__(str(type)).value) if not (content := request.json.get("content")):
Base.session.add(condition) return json_error("Missing `content` parameter."), 400
repository = Repository.query.filter_by(request.json.get("id"))
if Uses.query.filter_by(cid=condition.id, rid=repository.id): if not (repo_id := request.json.get("id")):
return json_error("This condition is already connected to the repository."), 406 return json_error("Missing `id` parameter."), 400
Base.session.add(Uses(cid=condition.id, rid=repository.id))
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() Base.session.commit()
return json_success(condition.to_json()), 200 return json_success(condition.to_json()), 200