Skip to content

Commit

Permalink
Merge pull request #769 from frankrousseau/master
Browse files Browse the repository at this point in the history
File routes improvements
  • Loading branch information
EvanBldy authored Feb 9, 2024
2 parents 98d6851 + 8472cb5 commit 8b74d2a
Show file tree
Hide file tree
Showing 8 changed files with 441 additions and 20 deletions.
24 changes: 24 additions & 0 deletions tests/files/test_route_output_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,30 @@ def test_get_output_types(self):
)
self.assertEqual(result[0]["id"], self.cache_type_id)

def test_get_output_files_for_project(self):
self.generate_fixture_output_type()
geometry = self.output_type

self.generate_fixture_output_file(geometry, 1, representation="obj")
self.generate_fixture_output_file(geometry, 2, representation="obj")
self.generate_fixture_output_file(geometry, 3, representation="obj")
self.generate_fixture_output_file(geometry, 4, representation="obj")
output_files = self.get(
"data/projects/%s/output-files" % self.project.id
)
self.assertEqual(len(output_files), 4)

self.generate_fixture_project("Sprite Fright")
self.generate_fixture_asset("Rabbit")
self.generate_fixture_output_file(geometry, 1, representation="max")
self.generate_fixture_output_file(geometry, 2, representation="max")
self.generate_fixture_output_file(geometry, 3, representation="max")

output_files = self.get(
"data/projects/%s/output-files" % self.project.id
)
self.assertEqual(len(output_files), 3)

def test_get_output_files_for_output_type_and_entity(self):
self.generate_fixture_output_type()
geometry = self.output_type
Expand Down
50 changes: 49 additions & 1 deletion zou/app/blueprints/crud/output_file.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
from flask import abort, current_app

from sqlalchemy.exc import StatementError
from flask_jwt_extended import jwt_required

from zou.app.models.output_file import OutputFile
from zou.app.models.entity import Entity
from zou.app.models.project import Project
from zou.app.services import user_service, entities_service
from zou.app.services import user_service, entities_service, files_service
from zou.app.utils import permissions

from zou.app.blueprints.crud.base import BaseModelsResource, BaseModelResource
Expand Down Expand Up @@ -44,3 +49,46 @@ def check_update_permissions(self, output_file, data):
return user_service.check_working_on_entity(
output_file["temporal_entity_id"] or output_file["entity_id"]
)

@jwt_required()
def get(self, instance_id):
"""
Retrieve a model corresponding at given ID and return it as a JSON
object.
---
tags:
- Crud
parameters:
- in: path
name: output_file_id
required: True
type: string
format: UUID
x-example: a24a6ea4-ce75-4665-a070-57453082c25
responses:
200:
description: Model as a JSON object
400:
description: Statement error
404:
description: Value error
"""
try:
output_file = files_service.get_output_file(instance_id)
self.check_read_permissions(output_file)
return self.clean_get_result(output_file)

except StatementError as exception:
current_app.logger.error(str(exception), exc_info=1)
return {"message": str(exception)}, 400

except ValueError:
abort(404)

def post_update(self, instance_dict):
files_service.clear_output_file_cache(instance_dict["id"])
return instance_dict

def post_delete(self, instance_dict):
files_service.clear_output_file_cache(instance_dict["id"])
return instance_dict
49 changes: 49 additions & 0 deletions zou/app/blueprints/crud/output_type.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
from flask import abort, current_app

from sqlalchemy.exc import StatementError
from flask_jwt_extended import jwt_required

from zou.app.models.output_type import OutputType
from zou.app.blueprints.crud.base import BaseModelResource, BaseModelsResource
from zou.app.services import files_service


class OutputTypesResource(BaseModelsResource):
Expand All @@ -16,3 +22,46 @@ def __init__(self):

def check_read_permissions(self, instance):
return True

@jwt_required()
def get(self, instance_id):
"""
Retrieve an output type corresponding at given ID and return it as a
JSON object.
---
tags:
- Crud
parameters:
- in: path
name: output_type_id
required: True
type: string
format: UUID
x-example: a24a6ea4-ce75-4665-a070-57453082c25
responses:
200:
description: Model as a JSON object
400:
description: Statement error
404:
description: Value error
"""
try:
output_type = files_service.get_output_type(instance_id)
self.check_read_permissions(output_type)
return self.clean_get_result(output_type)

except StatementError as exception:
current_app.logger.error(str(exception), exc_info=1)
return {"message": str(exception)}, 400

except ValueError:
abort(404)

def post_update(self, instance_dict):
files_service.clear_output_type_cache(instance_dict["id"])
return instance_dict

def post_delete(self, instance_dict):
files_service.clear_output_type_cache(instance_dict["id"])
return instance_dict
49 changes: 49 additions & 0 deletions zou/app/blueprints/crud/software.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
from flask import abort, current_app

from sqlalchemy.exc import StatementError
from flask_jwt_extended import jwt_required

from zou.app.models.software import Software
from zou.app.blueprints.crud.base import BaseModelResource, BaseModelsResource
from zou.app.services import files_service


class SoftwaresResource(BaseModelsResource):
Expand All @@ -16,3 +22,46 @@ def __init__(self):

def check_read_permissions(self, instance):
return True

@jwt_required()
def get(self, instance_id):
"""
Retrieve a software corresponding at given ID and return it as a
JSON object.
---
tags:
- Crud
parameters:
- in: path
name: software_id
required: True
type: string
format: UUID
x-example: a24a6ea4-ce75-4665-a070-57453082c25
responses:
200:
description: Model as a JSON object
400:
description: Statement error
404:
description: Value error
"""
try:
software = files_service.get_software(instance_id)
self.check_read_permissions(software)
return self.clean_get_result(software)

except StatementError as exception:
current_app.logger.error(str(exception), exc_info=1)
return {"message": str(exception)}, 400

except ValueError:
abort(404)

def post_update(self, instance_dict):
files_service.clear_software_cache(instance_dict["id"])
return instance_dict

def post_delete(self, instance_dict):
files_service.clear_software_cache(instance_dict["id"])
return instance_dict
48 changes: 48 additions & 0 deletions zou/app/blueprints/crud/working_file.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
from flask import abort, current_app

from sqlalchemy.exc import StatementError
from flask_jwt_extended import jwt_required

from zou.app.blueprints.crud.base import BaseModelsResource, BaseModelResource

from zou.app.models.entity import Entity
Expand Down Expand Up @@ -52,3 +57,46 @@ def check_update_permissions(self, instance, data):
user_service.check_project_access(task["project_id"])
user_service.check_entity_access(task["entity_id"])
return True

@jwt_required()
def get(self, instance_id):
"""
Retrieve a working file corresponding at given ID and return it as a
JSON object.
---
tags:
- Crud
parameters:
- in: path
name: working_file_id
required: True
type: string
format: UUID
x-example: a24a6ea4-ce75-4665-a070-57453082c25
responses:
200:
description: Model as a JSON object
400:
description: Statement error
404:
description: Value error
"""
try:
working_file = files_service.get_working_file(instance_id)
self.check_read_permissions(working_file)
return self.clean_get_result(working_file)

except StatementError as exception:
current_app.logger.error(str(exception), exc_info=1)
return {"message": str(exception)}, 400

except ValueError:
abort(404)

def post_update(self, instance_dict):
files_service.clear_working_file_cache(instance_dict["id"])
return instance_dict

def post_delete(self, instance_dict):
files_service.clear_working_file_cache(instance_dict["id"])
return instance_dict
2 changes: 2 additions & 0 deletions zou/app/blueprints/files/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
InstanceOutputTypesResource,
InstanceOutputTypeOutputFilesResource,
EntityOutputFilesResource,
ProjectOutputFilesResource,
InstanceOutputFilesResource,
SetTreeResource,
FileResource,
Expand Down Expand Up @@ -88,6 +89,7 @@
EntityOutputTypeOutputFilesResource,
),
("/data/entities/<entity_id>/output-files", EntityOutputFilesResource),
("/data/projects/<project_id>/output-files", ProjectOutputFilesResource),
(
"/data/asset-instances/<asset_instance_id>/output-files",
InstanceOutputFilesResource,
Expand Down
Loading

0 comments on commit 8b74d2a

Please sign in to comment.