Skip to content

Commit

Permalink
Merge pull request #307 from EvanBldy/master
Browse files Browse the repository at this point in the history
various improvements
  • Loading branch information
EvanBldy authored Jan 9, 2024
2 parents 27f79a6 + 31d1e28 commit fd3e37e
Show file tree
Hide file tree
Showing 9 changed files with 466 additions and 35 deletions.
1 change: 1 addition & 0 deletions gazu/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from . import task
from . import user
from . import playlist
from . import concept

from .exception import (
AuthFailedException,
Expand Down
149 changes: 149 additions & 0 deletions gazu/concept.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
from . import client as raw

from .sorting import sort_by_name
from .cache import cache
from .helpers import (
normalize_model_parameter,
normalize_list_of_models_for_links,
)

default = raw.default_client


@cache
def all_concepts(client=default):
"""
Returns:
list: All concepts from database.
"""
concepts = raw.fetch_all("concepts", client=client)
return sort_by_name(concepts)


@cache
def all_concepts_for_project(project, client=default):
"""
Args:
project (str / dict): The project dict or the project ID.
Returns:
list: Concepts from database or for given project.
"""
project = normalize_model_parameter(project)
concepts = raw.fetch_all(
"projects/%s/concepts" % project["id"], client=client
)
return sort_by_name(concepts)


@cache
def all_previews_for_concept(concept, client=default):
"""
Args:
concept (str / dict): The concept dict or the concept ID.
Returns:
list: Previews from database for given concept.
"""
concept = normalize_model_parameter(concept)
return raw.fetch_all(
"concepts/%s/preview-files" % concept["id"], client=client
)


def remove_concept(concept, force=False, client=default):
"""
Remove given concept from database.
Args:
concept (dict / str): Concept to remove.
"""
concept = normalize_model_parameter(concept)
path = "data/concepts/%s" % concept["id"]
params = {}
if force:
params = {"force": "true"}
return raw.delete(path, params, client=client)


@cache
def get_concept(concept_id, client=default):
"""
Args:
concept_id (str): ID of claimed concept.
Returns:
dict: Concept corresponding to given concept ID.
"""
return raw.fetch_one("concepts", concept_id, client=client)


@cache
def get_concept_by_name(project, concept_name, client=default):
"""
Args:
project (str / dict): The project dict or the project ID.
concept_name (str): Name of claimed concept.
Returns:
dict: Concept corresponding to given name and project.
"""
project = normalize_model_parameter(project)
return raw.fetch_first(
"concepts",
{"project_id": project["id"], "name": concept_name},
client=client,
)


def new_concept(
project,
name,
description=None,
data={},
entity_concept_links=[],
client=default,
):
"""
Create a concept for given project. Allow to set metadata too.
Args:
project (str / dict): The project dict or the project ID.
name (str): The name of the concept to create.
data (dict): Free field to set metadata of any kind.
entity_concept_links (list): List of entities to tag.
Returns:
Created concept.
"""
project = normalize_model_parameter(project)
data = {
"name": name,
"data": data,
"entity_concept_links": normalize_list_of_models_for_links(
entity_concept_links
),
}

if description is not None:
data["description"] = description

concept = get_concept_by_name(project, name, client=client)
if concept is None:
path = "data/projects/%s/concepts" % project["id"]
return raw.post(path, data, client=client)
else:
return concept


def update_concept(concept, client=default):
"""
Save given concept data into the API. Metadata are fully replaced by the ones
set on given concept.
Args:
concept (dict): The concept dict to update.
Returns:
dict: Updated concept.
"""
return raw.put("data/entities/%s" % concept["id"], concept, client=client)
13 changes: 13 additions & 0 deletions gazu/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,16 @@ def remove_entity(entity, force=False, client=default):
if force:
params = {"force": "true"}
return raw.delete(path, params, client=client)


def all_entities_with_tasks_linked_to_entity(entity, client=default):
"""
Args:
entity (dict): Entity to get linked entities.
Returns:
list: Retrieve all entities linked to given entity.
"""
entity = normalize_model_parameter(entity)
return raw.fetch_all(
"entities/%s/entities-linked/with-tasks" % entity["id"], client=client
)
39 changes: 37 additions & 2 deletions gazu/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,25 @@ def all_tasks_for_shot(shot, relations=False, client=default):
return sort_by_name(tasks)


@cache
def all_tasks_for_concept(concept, relations=False, client=default):
"""
Args:
concept (str / dict): The concept dict or the concept ID.
Returns:
list: Tasks linked to given concept.
"""
concept = normalize_model_parameter(concept)
params = {}
if relations:
params = {"relations": "true"}
tasks = raw.fetch_all(
"concepts/%s/tasks" % concept["id"], params, client=client
)
return sort_by_name(tasks)


@cache
def all_tasks_for_edit(edit, relations=False, client=default):
"""
Expand Down Expand Up @@ -272,6 +291,21 @@ def all_task_types_for_shot(shot, client=default):
return sort_by_name(task_types)


@cache
def all_task_types_for_concept(concept, client=default):
"""
Args:
concept (str / dict): The concept dict or the concept ID.
Returns
list: Task types of task linked to given concept.
"""
concept = normalize_model_parameter(concept)
path = "concepts/%s/task-types" % concept["id"]
task_types = raw.fetch_all(path, client=client)
return sort_by_name(task_types)


@cache
def all_task_types_for_asset(asset, client=default):
"""
Expand Down Expand Up @@ -1078,19 +1112,20 @@ def clear_assignations(tasks, person=None, client=default):
)


def new_task_type(name, color="#000000", client=default):
def new_task_type(name, color="#000000", for_entity="Asset", client=default):
"""
Create a new task type with the given name.
Args:
name (str): The name of the task type
color (str): The color of the task type as an hexadecimal string
with # as first character. ex : #00FF00
for_entity (str): The entity type for which the task type is created.
Returns:
dict: The created task type
"""
data = {"name": name, "color": color}
data = {"name": name, "color": color, "for_entity": for_entity}
return raw.post("data/task-types", data, client=client)


Expand Down
6 changes: 3 additions & 3 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ classifiers =
zip_safe = False
packages = find:
install_requires =
python-socketio[client]==5.10.0; python_version != '2.7'
python-socketio[client]==5.11.0; python_version != '2.7'
requests>=2.25.1

[options.packages.find]
Expand All @@ -47,5 +47,5 @@ test =
requests_mock

lint =
black==23.9.1; python_version != '2.7'
pre-commit==3.5.0; python_version >= '3.8'
black==23.12.1; python_version >= '3.8'
pre-commit==3.6.0; python_version >= '3.9'
Loading

0 comments on commit fd3e37e

Please sign in to comment.