Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various fixes #713

Merged
merged 2 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions zou/app/blueprints/breakdown/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ def get(self, scene_id):
return breakdown_service.get_camera_instances_for_scene(scene_id)


class ProjectEntityLinksResource(Resource):
class ProjectEntityLinksResource(Resource, ArgsMixin):
@jwt_required()
def get(self, project_id):
"""
Expand All @@ -394,7 +394,11 @@ def get(self, project_id):
"""
user_service.check_manager_project_access(project_id)
projects_service.get_project(project_id)
return entities_service.get_entity_links_for_project(project_id)
page = self.get_page()
limit = self.get_limit()
return entities_service.get_entity_links_for_project(
project_id, page, limit
)


class ProjectEntityLinkResource(Resource):
Expand Down
6 changes: 5 additions & 1 deletion zou/app/blueprints/crud/project.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from flask_jwt_extended import jwt_required

from flask_restful import Resource


Expand Down Expand Up @@ -96,7 +97,10 @@ def __init__(self):
self.protected_fields.append("team")

def check_read_permissions(self, project):
user_service.check_project_access(project["id"])
return user_service.check_project_access(project["id"])

def check_update_permissions(self, project, data):
return user_service.check_manager_project_access(project["id"])

def pre_update(self, project_dict, data):
if "team" in data:
Expand Down
18 changes: 17 additions & 1 deletion zou/app/mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,18 @@ def clear_empty_fields(self, data):

def get_page(self):
"""
Returns page requested by the user.
Returns page requested by the user as an integer.
"""
options = request.args
return int(options.get("page", "-1"))

def get_limit(self):
"""
Returns limit requested by the user as an integer.
"""
options = request.args
return int(options.get("limit", "-1"))

def get_sort_by(self):
"""
Returns sort by option value
Expand Down Expand Up @@ -115,14 +122,23 @@ def get_no_job(self):
return self.get_bool_parameter("no_job")

def get_text_parameter(self, field_name):
"""
Returns text parameter value matching `field_name`.
"""
options = request.args
return options.get(field_name, None)

def get_bool_parameter(self, field_name, default="false"):
"""
Returns bool parameter value matching `field_name`.
"""
options = request.args
return options.get(field_name, default).lower() == "true"

def get_date_parameter(self, field_name):
"""
Returns date parameter value matching `field_name`.
"""
self.parse_date_parameter(self.get_text_parameter(field_name))

def parse_date_parameter(self, param):
Expand Down
26 changes: 22 additions & 4 deletions zou/app/services/entities_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
projects_service,
notifications_service,
)
from zou.app.utils import cache, events, fields
from zou.app.utils import cache, events, fields, query as query_utils

from zou.app.models.entity import Entity, EntityLink
from zou.app.models.entity_type import EntityType
Expand Down Expand Up @@ -152,15 +152,33 @@ def get_entities_for_project(
return Entity.serialize_list(result, obj_type=obj_type)


def get_entity_links_for_project(project_id):
def get_entity_links_for_project(project_id, page=None, limit=None):
"""
Retrieve entity links for
"""
query = EntityLink.query.join(
Entity, EntityLink.entity_in_id == Entity.id
).filter(Entity.project_id == project_id)
result = query.all()
return Entity.serialize_list(result)

results = []
if page is not None and page > 0:
if limit < 1:
limit = None
results = query_utils.get_paginated_result(query, page, limit=limit)
else:
for entity_link in query.all():
results.append(
{
"id": entity_link.id,
"entity_in_id": entity_link.entity_in_id,
"entity_out_id": entity_link.entity_out_id,
"nb_occurences": entity_link.nb_occurences,
"label": entity_link.label,
"data": entity_link.data,
"type": "EntityLink",
}
)
return results


def get_entities_and_tasks(criterions={}):
Expand Down
4 changes: 2 additions & 2 deletions zou/app/utils/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ def apply_criterions_to_db_query(model, db_query, criterions):
return db_query.filter_by(**criterions)


def get_paginated_results(query, page, relations=False):
def get_paginated_results(query, page, limit=None, relations=False):
"""
Apply pagination to the query object.
"""
if page < 1:
entries = query.all()
return fields.serialize_models(entries, relations=relations)
else:
limit = app.config["NB_RECORDS_PER_PAGE"]
limit = limit or app.config["NB_RECORDS_PER_PAGE"]
total = query.count()
offset = (page - 1) * limit

Expand Down