Snippets

ftrack Assign users and groups to projects

Updated by Lorenzo Angeli

File user_and_group.py Modified

  • Ignore whitespace
  • Hide word diff
     'Appointment where context.id is "{}" and resource.id = "{}" and type="allocation"'.format(
         first_project['id'], my_user['id']
     )
-).one()
+).first()
 
 if not project_has_user:
     print 'Assigning user {} to project {}'.format(my_user['username'], first_project['name']) 
     'Appointment where context.id is "{}" and resource.id = "{}" and type="allocation"'.format(
         first_project['id'], new_group['id']
     )
-).one()
+).first()
 
 if not project_has_group:
     print 'Assigning group {} to project {}'.format(new_group['name'], first_project['name']) 
Created by Lorenzo Angeli

File user_and_group.py Added

  • Ignore whitespace
  • Hide word diff
+from ftrack_api import Session
+
+session = Session()
+
+first_project = session.query('Project').first()
+print 'Working on project:', first_project['name']
+my_user = session.query('User where username is "{}"'.format(session.api_user)).one()
+new_group = session.ensure('Group', {"name":"NewTestingGroup", "local":False})
+
+print 'Assigning user {} to group {}'.format(my_user['username'], new_group['name']) 
+new_membership = session.ensure('Membership', {"group_id":new_group['id'], "user_id": my_user['id']})
+
+project_has_user = session.query(
+    'Appointment where context.id is "{}" and resource.id = "{}" and type="allocation"'.format(
+        first_project['id'], my_user['id']
+    )
+).one()
+
+if not project_has_user:
+    print 'Assigning user {} to project {}'.format(my_user['username'], first_project['name']) 
+    session.create('Appointment', {
+        'context': first_project,
+        'resource': my_user,
+        'type': 'allocation'
+    })
+else:
+    print 'User {} already in assigned to project {}'.format(my_user['username'], first_project['name']) 
+
+project_has_group = session.query(
+    'Appointment where context.id is "{}" and resource.id = "{}" and type="allocation"'.format(
+        first_project['id'], new_group['id']
+    )
+).one()
+
+if not project_has_group:
+    print 'Assigning group {} to project {}'.format(new_group['name'], first_project['name']) 
+    session.create('Appointment', {
+        'context': first_project,
+        'resource': new_group,
+        'type': 'allocation'
+    })
+else:
+    print 'Group {} already in assigned to project {}'.format(new_group['name'], first_project['name']) 
+
+
+session.commit()
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.