@@ -211,6 +211,7 @@ class Service(object):
211211 MODEL_PATH = '/model'
212212 TEMPLATES_PATH = '/templates'
213213 TEMPLATEQUERY_PATH = '/template/results'
214+ ALL_TEMPLATES_PATH = '/alltemplates'
214215 LIST_PATH = '/lists'
215216 LIST_CREATION_PATH = '/lists'
216217 LIST_RENAME_PATH = '/lists/rename'
@@ -270,6 +271,8 @@ def __init__(self, root,
270271 self .prefetch_id_only = prefetch_id_only
271272 # Initialize empty cached data.
272273 self ._templates = None
274+ self ._all_templates = None
275+ self ._all_templates_names = None
273276 self ._model = None
274277 self ._version = None
275278 self ._release = None
@@ -488,6 +491,44 @@ def get_template(self, name):
488491 self .templates [name ] = t
489492 return t
490493
494+ def get_template_by_user (self , name , username ):
495+ """
496+ Returns a template of the given name belonging to username
497+ ==========================================================
498+
499+ Tries to retrieve a template of the given name belonging
500+ to the username from the webservice. You need to authenticate
501+ as admin
502+
503+ @see: L{intermine.webservice.Service.__init__}
504+
505+ @param name: the template's name
506+ @type name: string
507+
508+ @param username: the username
509+ @type name: string
510+
511+ @raise ServiceError: if the template or user does not exist
512+ @raise QueryParseError: if the template cannot be parsed
513+
514+ @return: L{intermine.query.Template}
515+ """
516+ try :
517+ templates = self .all_templates [username ]
518+ except KeyError :
519+ raise ServiceError ("There is no user called '" + username + "'" )
520+ try :
521+ t = templates [name ]
522+ except KeyError :
523+ raise ServiceError ("There is no template called '"
524+ + name + "' at this service belonging to '"
525+ + username + "'" )
526+ if not isinstance (t , Template ):
527+ t = Template .from_xml (t , self .model , self )
528+ t .user_name = username
529+ self .all_templates [name ] = t
530+ return t
531+
491532 def _get_json (self , path , payload = None ):
492533 headers = {'Accept' : 'application/json' }
493534 with closing (self .opener .open (self .root + path , payload ,
@@ -605,6 +646,8 @@ def flush(self):
605646 self ._list_manager .delete_temporary_lists ()
606647 self ._list_manager = ListManager (self )
607648 self ._templates = None
649+ self ._all_templates = None
650+ self ._all_templates_names = None
608651 self ._model = None
609652 self ._version = None
610653 self ._release = None
@@ -644,6 +687,80 @@ def templates(self):
644687 self ._templates = templates
645688 return self ._templates
646689
690+ @property
691+ def all_templates (self ):
692+ """
693+ The dictionary of templates by users from the webservice
694+ ========================================================
695+
696+ Service.all_templates S{->} dict(string|string)
697+
698+ You need to be authenticated as admin.
699+
700+ For efficiency's sake, Templates are not parsed until
701+ they are required, and until then they are stored as XML
702+ strings. It is recommended that in most cases you would want
703+ to use L{Service.get_template}.
704+
705+ You can use this property however to test for template existence::
706+
707+ if name in service.templates:
708+ template = service.get_template(name)
709+
710+ @rtype: dict
711+
712+ """
713+ if self ._all_templates is None :
714+ all_templates = {}
715+ dom = self ._get_xml (self .ALL_TEMPLATES_PATH )
716+ for e in dom .getElementsByTagName ('template' ):
717+ user = e .getAttribute ('userName' )
718+ name = e .getAttribute ('name' )
719+ if user in all_templates :
720+ templates = all_templates [user ]
721+ templates [name ] = e .toxml ()
722+ else :
723+ templates = {}
724+ templates [name ] = e .toxml ()
725+ all_templates [user ] = templates
726+ self ._all_templates = all_templates
727+ return self ._all_templates
728+
729+ @property
730+ def all_templates_names (self ):
731+ """
732+ The dictionary of templates names by users from the webservice
733+ =============================================================
734+
735+ Service.all_templates_names S{->} dict(string|array)
736+
737+ You need to be authenticated as admin.
738+
739+ Example::
740+ allTemplatesNames = service.all_templates_names
741+ for user in allTemplatesNames:
742+ userTemplatesNames = allTemplatesNames[user]
743+ for templateName in userTemplatesNames:
744+ template = service.get_template_by_user(templateName, user)
745+
746+ @rtype: dict
747+
748+ """
749+ if self ._all_templates_names is None :
750+ all_templates_names = {}
751+ dom = self ._get_xml (self .ALL_TEMPLATES_PATH )
752+ for e in dom .getElementsByTagName ('template' ):
753+ user = e .getAttribute ('userName' )
754+ name = e .getAttribute ('name' )
755+ if user in all_templates_names :
756+ all_templates_names [user ].append (name )
757+ else :
758+ templates_names = []
759+ templates_names .append (name )
760+ all_templates_names [user ] = templates_names
761+ self ._all_templates_names = all_templates_names
762+ return self ._all_templates_names
763+
647764 @property
648765 def model (self ):
649766 """
0 commit comments