Skip to content

Commit

Permalink
Formatted sources
Browse files Browse the repository at this point in the history
  • Loading branch information
mesemus committed Feb 24, 2024
1 parent 9066e7e commit f8773c5
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 13 deletions.
8 changes: 5 additions & 3 deletions oarepo_ui/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ def __init__(self, app):
def reinitialize_catalog(self):
self._catalog = None
try:
del self.catalog # noqa - this is a documented method of clearing the cache
except AttributeError: # but does not work if the cache is not initialized yet, thus the try/except
del self.catalog # noqa - this is a documented method of clearing the cache
except (
AttributeError
): # but does not work if the cache is not initialized yet, thus the try/except
pass

@functools.cached_property
Expand Down Expand Up @@ -84,7 +86,7 @@ def init_config(self, app):
app.config.setdefault(k, getattr(config, k))

# merge in default filters and globals if they have not been overridden
for k in ('OAREPO_UI_JINJAX_FILTERS', 'OAREPO_UI_JINJAX_GLOBALS'):
for k in ("OAREPO_UI_JINJAX_FILTERS", "OAREPO_UI_JINJAX_GLOBALS"):
for name, val in getattr(config, k).items():
if name not in app.config[k]:
app.config[k][name] = val
1 change: 0 additions & 1 deletion oarepo_ui/resources/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from invenio_records_resources.services.records.results import RecordItem
from oarepo_runtime.datastreams.utils import get_file_service_for_record_service

import oarepo_ui.resources.templating.filters
from ..proxies import current_oarepo_ui

if TYPE_CHECKING:
Expand Down
4 changes: 2 additions & 2 deletions oarepo_ui/resources/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
)
from invenio_records_resources.services import LinksTemplate

import oarepo_ui.resources.templating.filters
from oarepo_ui.utils import dump_empty

from .templating.data import FieldData

if TYPE_CHECKING:
Expand Down Expand Up @@ -181,7 +181,7 @@ def detail(self):
extra_context=extra_context,
ui_links=ui_links,
context=current_oarepo_ui.catalog.jinja_env.globals,
d=FieldData(record, {}) # TODO: pass ui here
d=FieldData(record, {}), # TODO: pass ui here
)

def make_links_absolute(self, links, api_prefix):
Expand Down
2 changes: 1 addition & 1 deletion oarepo_ui/resources/templating/catalog.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
import typing as t
from collections import namedtuple
from functools import cached_property
from itertools import chain
Expand All @@ -12,7 +13,6 @@
from jinjax import Catalog
from jinjax.exceptions import ComponentNotFound
from jinjax.jinjax import JinjaX
import typing as t

DEFAULT_URL_ROOT = "/static/components/"
ALLOWED_EXTENSIONS = (".css", ".js")
Expand Down
10 changes: 7 additions & 3 deletions oarepo_ui/resources/templating/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,18 @@ def __get(self, name: Union[str, int]):

if isinstance(self.__data, dict):
if name in self.__data:
return FieldData(self.__data.get(name), self.__ui.get(name, {}), self.__path + [name])
return FieldData(
self.__data.get(name), self.__ui.get(name, {}), self.__path + [name]
)
else:
return EMPTY_FIELD_DATA

if isinstance(self.__data, list):
idx = int(name)
if idx < len(self.__data):
return FieldData(self.__data[idx], self.__ui.get('items', {}), self.__path + [idx])
return FieldData(
self.__data[idx], self.__ui.get("items", {}), self.__path + [idx]
)
return EMPTY_FIELD_DATA

return EMPTY_FIELD_DATA
Expand All @@ -56,7 +60,7 @@ def _as_array(self):
ret = []
if isinstance(self.__data, list):
for val in self.__data:
ret.append(FieldData(val, self.__ui.get('items', {})))
ret.append(FieldData(val, self.__ui.get("items", {})))
elif isinstance(self.__data, dict):
for key, val in self.__data.items():
ret.append(FieldData(val, self.__ui.get(key, {})))
Expand Down
7 changes: 6 additions & 1 deletion oarepo_ui/resources/templating/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ def ichain(*iterables, remove_empty=False):
elif it is not None:
ret.extend(ensure_array(it))
if remove_empty:
ret = [x for x in ret if (isinstance(x, FieldData) or not x._is_empty) or x]
ret = [
x
for x in ret
if (isinstance(x, FieldData) and not x._is_empty)
or (not isinstance(x, FieldData) and x)
]
return ret


Expand Down
14 changes: 12 additions & 2 deletions oarepo_ui/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,18 @@ def add_jinja_filters(state):

# modified the global env - not pretty, but gets filters to search as well
env = app.jinja_env
env.filters.update({k: obj_or_import_string(v) for k, v in app.config["OAREPO_UI_JINJAX_FILTERS"].items()})
env.globals.update({k: obj_or_import_string(v) for k, v in app.config["OAREPO_UI_JINJAX_GLOBALS"].items()})
env.filters.update(
{
k: obj_or_import_string(v)
for k, v in app.config["OAREPO_UI_JINJAX_FILTERS"].items()
}
)
env.globals.update(
{
k: obj_or_import_string(v)
for k, v in app.config["OAREPO_UI_JINJAX_GLOBALS"].items()
}
)
env.policies.setdefault("json.dumps_kwargs", {}).setdefault("default", str)

# the catalogue should not have been used at this point but if it was, we need to reinitialize it
Expand Down

0 comments on commit f8773c5

Please sign in to comment.