@@ -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,43 @@ 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
500+ from the webservice. If you are trying to fetch
501+ a private template (ie. one you made yourself
502+ and is not available to others) then you may need to authenticate
503+
504+ @see: L{intermine.webservice.Service.__init__}
505+
506+ @param name: the template's name
507+ @type name: string
508+
509+ @param username: the username
510+ @type name: string
511+
512+ @raise ServiceError: if the template or user does not exist
513+ @raise QueryParseError: if the template cannot be parsed
514+
515+ @return: L{intermine.query.Template}
516+ """
517+ try :
518+ templates = self .all_templates [username ]
519+ except KeyError :
520+ raise ServiceError ("There is no user called '" + username + "'" )
521+ try :
522+ t = templates [name ]
523+ except KeyError :
524+ raise ServiceError ("There is no template called '"
525+ + name + "' at this service belonging to '" + username + "'" )
526+ if not isinstance (t , Template ):
527+ t = Template .from_xml (t , self .model , self )
528+ self .all_templates [name ] = t
529+ return t
530+
491531 def _get_json (self , path , payload = None ):
492532 headers = {'Accept' : 'application/json' }
493533 with closing (self .opener .open (self .root + path , payload ,
@@ -605,6 +645,8 @@ def flush(self):
605645 self ._list_manager .delete_temporary_lists ()
606646 self ._list_manager = ListManager (self )
607647 self ._templates = None
648+ self ._all_templates = None
649+ self .all_templates_names = None
608650 self ._model = None
609651 self ._version = None
610652 self ._release = None
@@ -644,6 +686,77 @@ def templates(self):
644686 self ._templates = templates
645687 return self ._templates
646688
689+ @property
690+ def all_templates (self ):
691+ """
692+ The dictionary of templates by users from the webservice
693+ ========================================================
694+
695+ Service.all_templates S{->} dict(string|string)
696+
697+ For efficiency's sake, Templates are not parsed until
698+ they are required, and until then they are stored as XML
699+ strings. It is recommended that in most cases you would want
700+ to use L{Service.get_template}.
701+
702+ You can use this property however to test for template existence though::
703+
704+ if name in service.templates:
705+ template = service.get_template(name)
706+
707+ @rtype: dict
708+
709+ """
710+ if self ._all_templates is None :
711+ all_templates = {}
712+ dom = self ._get_xml (self .ALL_TEMPLATES_PATH )
713+ for e in dom .getElementsByTagName ('template' ):
714+ user = e .getAttribute ('userName' )
715+ name = e .getAttribute ('name' )
716+ if user in all_templates :
717+ templates = all_templates [user ]
718+ templates [name ] = e .toxml ()
719+ else :
720+ templates = {}
721+ templates [name ] = e .toxml ()
722+ all_templates [user ] = templates
723+ self ._all_templates = all_templates
724+ return self ._all_templates
725+
726+ @property
727+ def all_templates_names (self ):
728+ """
729+ The dictionary of templates name by users from the webservice
730+ =============================================================
731+
732+ Service.all_templates_names S{->} dict(string|array)
733+
734+ You can use this property however to test for template existence though::
735+
736+ allTemplatesNames = service.all_templates_names
737+ for user in allTemplatesNames:
738+ userTemplatesNames = allTemplatesNames[user]
739+ for templateName in userTemplatesNames:
740+ template = service.get_template_by_user(templateName, user)
741+
742+ @rtype: dict
743+
744+ """
745+ if self ._all_templates_names is None :
746+ all_templates_names = {}
747+ dom = self ._get_xml (self .ALL_TEMPLATES_PATH )
748+ for e in dom .getElementsByTagName ('template' ):
749+ user = e .getAttribute ('userName' )
750+ name = e .getAttribute ('name' )
751+ if user in all_templates_names :
752+ all_templates_names [user ].append (name )
753+ else :
754+ templates_names = []
755+ templates_names .append (name )
756+ all_templates_names [user ] = templates_names
757+ self ._all_templates_names = all_templates_names
758+ return self ._all_templates_names
759+
647760 @property
648761 def model (self ):
649762 """
0 commit comments