Coverage for gws-app/gws/base/template/manager.py: 81%

31 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-10-05 13:35 +0200

1"""Template manager.""" 

2 

3import gws 

4import gws.lib.mime 

5 

6# @TODO template types should be configurable 

7 

8TEMPLATE_TYPES = { 

9 '.cx.html': 'html', 

10 '.cx.csv': 'csv', 

11 '.qgs': 'qgis', 

12 '.cx.py': 'py', 

13} 

14 

15 

16class Object(gws.TemplateManager): 

17 """Template manager.""" 

18 

19 def find_templates(self, subjects, where, user=None, mime_type=None): 

20 return gws.u.compact(self.find_template(s, where, user, mime_type) for s in subjects) 

21 

22 def find_template(self, subject, where, user=None, mime_type=None): 

23 for obj in where: 

24 if not obj: 

25 continue 

26 p = self._find(subject, obj, user, mime_type) 

27 if p: 

28 gws.log.debug(f'find_template: found {subject=} {obj=}') 

29 return p 

30 

31 p = self._find(subject, self.root.app, user, mime_type) 

32 if p: 

33 gws.log.debug(f'find_template: found {subject=} APP') 

34 return p 

35 

36 def _find(self, subject, obj, user, mime_type): 

37 """Return the first matching template of an object.""" 

38 for tpl in getattr(obj, 'templates', []): 

39 if tpl.subject != subject: 

40 continue 

41 if user and not user.can_use(tpl): 

42 continue 

43 if mime_type and tpl.mimeTypes and mime_type not in tpl.mimeTypes: 

44 continue 

45 return tpl 

46 

47 def template_from_path(self, path, **kwargs): 

48 for ext, typ in TEMPLATE_TYPES.items(): 

49 if path.endswith(ext): 

50 return self.root.create_shared( 

51 gws.ext.object.template, 

52 gws.Config(uid=gws.u.sha256(path), type=typ, path=path), 

53 **kwargs 

54 )