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

29 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-04-17 01:37 +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_template(self, subject, where, user=None, mime=None): 

18 for obj in where: 

19 if not obj: 

20 continue 

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

22 if p: 

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

24 return p 

25 

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

27 if p: 

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

29 return p 

30 

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

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

33 if tpl.subject != subject: 

34 continue 

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

36 continue 

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

38 continue 

39 return tpl 

40 

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

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

43 if path.endswith(ext): 

44 return self.root.create_shared( 

45 gws.ext.object.template, 

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

47 **kwargs 

48 )