From 5d45d0904c54de211fc2777513e320852bf4fb23 Mon Sep 17 00:00:00 2001 From: Lorenzo Balugani Date: Mon, 26 Apr 2021 18:51:32 +0200 Subject: [PATCH] Repository filtering Now the API can be asked to give only active repos or closed ones (or both). --- .../nest_backend/database/tables/Repository.py | 3 ++- .../routes/repository/repository_list.py | 16 ++++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/code/backend/nest_backend/database/tables/Repository.py b/code/backend/nest_backend/database/tables/Repository.py index 7089a73..7484202 100644 --- a/code/backend/nest_backend/database/tables/Repository.py +++ b/code/backend/nest_backend/database/tables/Repository.py @@ -22,4 +22,5 @@ class Repository(Base.Model): uses = Base.relationship("Uses", back_populates="repository") def to_json(self): - return {"id": self.id, "name": self.name, "start": self.start.isoformat(), "owner": self.owner.to_json()} + return {"id": self.id, "name": self.name, "start": self.start.isoformat(), "isActive":self.isActive, + "end":self.end.isoformat(),"owner": self.owner.to_json()} diff --git a/code/backend/nest_backend/routes/repository/repository_list.py b/code/backend/nest_backend/routes/repository/repository_list.py index a00fecd..9a8afe7 100644 --- a/code/backend/nest_backend/routes/repository/repository_list.py +++ b/code/backend/nest_backend/routes/repository/repository_list.py @@ -10,9 +10,21 @@ from flask_cors import cross_origin def page_repository_list(): """ API call that returns the list of repositories. + :parameter onlyActive: if present, only active repos are provided + :parameter onlyDead: if present, only dead repos are provided :returns: a JSON-formatted string that contains under the "content" field the list of repositories that belong to the user ("owner") and a list of repositories that he can spectate ("spectator"). """ user = find_user(get_jwt_identity()) - return json_success({"owner": [r.to_json() for r in user.owner_of], - "spectator": [r.repository.to_json() for r in user.authorizations]}) + owner = Repository.query.filter_by(owner_id=user.email) + spectator = Authorization.query.filter_by(email=user.email).join(Repository) + if request.json.get("onlyActive"): + owner = owner.filter_by(isActive=True) + spectator = spectator.filter(Repository.isActive == True) + elif request.json.get("onlyDead"): + owner = owner.filter_by(isActive=False) + spectator = spectator.filter(Repository.isActive == False) + owner = owner.all() + spectator = spectator.all() + return json_success({"owner": [r.to_json() for r in owner], + "spectator": [r.repository.to_json() for r in spectator]})