Coverage for /opt/conda/envs/apienv/lib/python3.10/site-packages/daiquiri/core/layout.py: 83%
41 statements
« prev ^ index » next coverage.py v7.6.5, created at 2024-11-15 02:12 +0000
« prev ^ index » next coverage.py v7.6.5, created at 2024-11-15 02:12 +0000
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3import re
4import logging
5import ruamel.yaml
6from flask import g
7from daiquiri.core import CoreBase, CoreResource, marshal
8from daiquiri.resources.utils import get_resource_provider, YamlDict
9from daiquiri.core.schema.metadata import paginated
10from daiquiri.core.schema.layout import LayoutSchema
11from daiquiri.core.exceptions import SyntaxErrorYAML
13logger = logging.getLogger(__name__)
16class LayoutsResource(CoreResource):
17 @marshal(out=[[200, paginated(LayoutSchema), "A list of layouts"]])
18 def get(self):
19 """List of layouts"""
20 return self._parent.list(), 200
23class Layout(CoreBase):
24 """The layouts handler"""
26 _require_session = True
27 _schemas = {}
29 def setup(self):
30 self.register_route(LayoutsResource, "")
32 def list(self):
33 """Get a list of layouts"""
34 layouts = []
36 provider = get_resource_provider()
37 names = provider.list_resource_names("layout", "*.yml")
38 for name in sorted(names):
39 try:
40 layout = YamlDict("layout", name)
42 # Make acronym url safe
43 layout["acronym"] = "".join(
44 [
45 c
46 for c in layout.get("acronym", layout["name"])
47 if re.match(r"\w", c)
48 ]
49 )
51 requires = layout.get("require_staff", None)
52 if requires:
53 if hasattr(g, "user"):
54 logger.info(f"User login {g.user} require_staff:{requires}")
55 if g.user.staff():
56 layouts.append(layout)
57 else:
58 layouts.append(layout)
60 except ruamel.yaml.YAMLError as ex:
61 layouts.append(
62 {
63 "acronym": name.replace(".yml", ""),
64 "name": name,
65 "error": "Invalid YAML Syntax\n" + str(ex),
66 }
67 )
69 except SyntaxErrorYAML as ex:
70 layouts.append(
71 {
72 "acronym": name.replace(".yml", ""),
73 "name": name,
74 "error": ex.pretty(),
75 }
76 )
78 except Exception as ex:
79 layouts.append(
80 {
81 "acronym": name.replace(".yml", ""),
82 "name": name,
83 "error": "Unexpected error while reading layout: \n" + str(ex),
84 }
85 )
87 return {"total": len(layouts), "rows": layouts}