Jump to content

Mirroring statuses listener


Alberto GZ

Recommended Posts

I have done a little modify from cascade_thumbails.py listener created by Mattias Lagergren, to adjust it to get version status and set related task status when it changed.

At this point, I find a couple of things to solve it

1. Related task status is getting always from the last changed version status. I would like that only it gets status from the last version.

2. I have some statuses overrides and some statuses availables for versions are different for tasks, so I would like create a relationship table for statutes, to this way when version status be Approved task status be Completed for example. (see picture above)

# :coding: utf-8
# :copyright: Copyright (c) 2015 ftrack

# Original snippet:(https://bitbucket.org/snippets/ftrack/ngaro/cascade-thumbnails-listener)
# This snippet was last tested with ftrack 3.3.16 and Python API 0.13.0.
import config
import logging
import ftrack_api


logging.basicConfig()


def mirroring_version_status(event):
    '''Handle *event* and populate version status to task status.'''

    for entity in event['data'].get('entities', []):
        asset_version = None
        entity_id = None

        # Handle new or updated versions.
        if (
            entity.get('entityType') == 'assetversion' and
            entity.get('action') in ('add', 'update') and
            entity.get('keys', None) and
            'statusid' in entity.get('keys', [])
        ):
            entity_id = entity['entityId']
	
		
        # If entity was found, try to get it.
        if entity_id:
            # Get asset version and preload data for performance and to avoid
            # caching issues.
            asset_version = session.query(
                'select status_id, asset, asset.parent, task '
                'from AssetVersion where id is {0}'.format(
                    entity['entityId']
                )
            ).first()

        if asset_version and asset_version['status_id']:
            # Update related task if the status is set.
            task = asset_version['task']
			
			# Update parent if the status is set. //optional//
			#parent = asset_version['asset']['parent'] 
			#parent['status_id'] = asset_version['status_id']

            if task:
                task['status_id'] = asset_version['status_id']

    session.commit()


# Subscribe to events with the update topic.
session = ftrack_api.Session()
session.event_hub.subscribe('topic=ftrack.update', mirroring_version_status)
session.event_hub.wait()

 

statuses_relationship.png

Link to comment
Share on other sites

Hey, that sounds doable. You would need to query for other versions  and see if there is a later version on the same asset:

# Disclaimer: I haven't tested this code.

later_version = session.query(
	'AssetVersion where version > {0} and asset_id is "{1}"'.format(
		asset_version['version'], asset_version['asset_id']
	)
).first()

if later_version:
	# Return since there is a later version than the version that was being updated.
	return

For the statuses I would create some kind of map, either with status ids, name based or with the status entities:

MapStatuses = {
	'New': 'In progress',
	... etc.
}

 

Link to comment
Share on other sites

Thanks Mattias!

I've tested your code for get last version, there is look at all versions status except the first one. 

I changed 'greater than 0' in the query:

later_version = session.query(
	'AssetVersion where version is {0} and asset_id is "{1}"'.format(

thinking index for last version would be always 0, but not works.

Checkbox for 'lastest version' in the web interface gets the last version, so I think its code should be able for my purpose.

About building a dictionary for statuses relationship, its a good idea, but I don't know how it added in the code.

In the current code gets status_id from query entities, then it sets the same status_id to task.

So to use MapStatuses I would see into. This code would get each value for each key?

asset_version['status_id'] = MapStatuses.keys()

task['status_id'] = MapStatuses.values()

 

Link to comment
Share on other sites

Hi, 

3 hours ago, AlbertoGz said:

thinking index for last version would be always 0, but not works.

The version is not an index, it is the version number of the asset version. So if you do the version > {0} check you query and see if there is any newer version on the same asset.

3 hours ago, AlbertoGz said:

So to use MapStatuses I would see into. This code would get each value for each key?

I was thinking more something like:

task['status_id'] = MapStatuses.get(asset_version['status_id'])

In this example MapStatuses is a dict that contains asset version status ids, and how they map to a corresponding task status

Link to comment
Share on other sites

When I change the version status from web interface I obtain the next error message: 'the status is not valid for this object'

I use this dict:

MapStatuses = {
    0: 1,
    1: 2
}

And try getting status with your line Mattias

 task['status_id'] = MapStatuses.get(asset_version['status_id'])

 

Link to comment
Share on other sites

The MapStatuses would need to contain the status id's that you want to use. 

To see your statuses you can print them like this:

>>> import ftrack_api
>>> session = ftrack_api.Session()
>>> for status in session.query('Status'):
...     print status['name'], status['id']
...
Not started 44dd9fb2-4164-11df-9218-0019bb4983d8
In progress 44ddd0fe-4164-11df-9218-0019bb4983d8
Awaiting approval 44dded64-4164-11df-9218-0019bb4983d8
Pending 69c8858a-4dbf-11e1-9902-f23c91df25eb
Approved 44de097a-4164-11df-9218-0019bb4983d8
Rejected 5c74bae6-5659-11e1-b145-f23c91df25eb

And then create the map from the ones you like to have:

MapStatuses = {
    # Pending to In progress
    '69c8858a-4dbf-11e1-9902-f23c91df25eb': '44ddd0fe-4164-11df-9218-0019bb4983d8',
    ...
}

 

Link to comment
Share on other sites

Uuuuuhuuu! Now it works! Thanks a lot Mattias!

I had tried with an example from old api documentation where used... 

task.setStatus(statuses[1])

...and I thinking status id was a simple integer. This is a change between API versions?

Only 'New' status don't wanna accept its ID. Can be due is an exclusive version status?

In any case, at this time, the script now can changes automatically the task status getting from version status with mapping status. That is useful for me. Great!

In the future I would like can refine a bit more the script doing that only gets the status from latest version (I don't success it), and also mapping statuses by each one task overrides.

 

Link to comment
Share on other sites

On 6/3/2016 at 9:19 PM, AlbertoGz said:

Uuuuuhuuu! Now it works! Thanks a lot Mattias!

 

Great, I'm glad to hear that!

On 6/3/2016 at 9:19 PM, AlbertoGz said:

I had tried with an example from old api documentation where used... 

task.setStatus(statuses[1])

...and I thinking status id was a simple integer. This is a change between API versions?

Only 'New' status don't wanna accept its ID. Can be due is an exclusive version status?

Status ids are UUIDs in both APIs, but in the example you reference the statuses variable is an array which is indexed into. I think the example is just a bit odd.

   

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...