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

31 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-10-16 23:09 +0200

1"""Model 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 def find_templates(self, subjects, where, user=None, mime=None): 

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

19 

20 def find_template(self, subject, where, user=None, mime=None): 

21 for obj in where: 

22 if not obj: 

23 continue 

24 p = self._find(subject, obj, user, mime) 

25 if p: 

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

27 return p 

28 

29 p = self._find(subject, self.root.app, user, mime) 

30 if p: 

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

32 return p 

33 

34 def _find(self, subject, obj, user, mime): 

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

36 if tpl.subject != subject: 

37 continue 

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

39 continue 

40 if mime and tpl.mimeTypes and mime not in tpl.mimeTypes: 

41 continue 

42 return tpl 

43 

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

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

46 if path.endswith(ext): 

47 return self.root.create_shared( 

48 gws.ext.object.template, 

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

50 **kwargs 

51 )