Skip to content

Commit

Permalink
intermediary commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronald Krist committed Sep 19, 2024
1 parent 12a3163 commit ae35da6
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 27 deletions.
14 changes: 14 additions & 0 deletions oarepo_ui/resources/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ def get_template_folder(self):
request_view_args = {}


class FormConfigResourceConfig(ResourceConfig):
application_id = "Default"

def form_config(self, **kwargs):
"""Get the react form configuration."""

return dict(
**kwargs,
)

request_view_args = {}
components = None


class TemplatePageUIResourceConfig(UIResourceConfig):
pages = {}
"""
Expand Down
84 changes: 57 additions & 27 deletions oarepo_ui/resources/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#
from ..proxies import current_oarepo_ui
from .config import (
FormConfigResourceConfig,
RecordsUIResourceConfig,
TemplatePageUIResourceConfig,
UIResourceConfig,
Expand All @@ -54,11 +55,29 @@
from_conf("request_file_view_args"), location="view_args"
)

request_create_args = request_parser(
from_conf("request_create_args"), location="args"
)
request_create_args = request_parser(from_conf("request_create_args"), location="args")


class UIComponentsMixin:

#
# Pluggable components
#

@property
def components(self) -> Iterator["UIResourceComponent"]:
"""Return initialized service components."""
return (c(self) for c in self.config.components or [])

def run_components(self, action, *args, **kwargs):
"""Run components for a given action."""

class UIResource(Resource):
for component in self.components:
if hasattr(component, action):
getattr(component, action)(*args, **kwargs)


class UIResource(UIComponentsMixin, Resource):
"""Record resource."""

config: UIResourceConfig
Expand All @@ -82,28 +101,38 @@ def as_blueprint(self, **options):

return blueprint

#
# Pluggable components
#
@property
def components(self) -> Iterator["UIResourceComponent"]:
"""Return initialized service components."""
return (c(self) for c in self.config.components or [])

def run_components(self, action, *args, **kwargs):
"""Run components for a given action."""

for component in self.components:
if hasattr(component, action):
getattr(component, action)(*args, **kwargs)

def fill_jinja_context(self):
"""function providing flask template app context processors"""
ret = {}
self.run_components("fill_jinja_context", context=ret)
return ret


class FormConfigResource(UIComponentsMixin, Resource):
config: FormConfigResourceConfig

def create_url_rules(self):
"""Create the URL rules for the record resource."""
routes = []
route_config = self.config.routes
for route_name, route_url in route_config.items():
routes.append(route("GET", route_url, getattr(self, route_name)))
return routes

def _get_form_config(self, **kwargs):
return self.config.form_config(**kwargs)

@request_view_args
def form_config(self):
form_config = self._get_form_config()
self.run_components(
"form_config",
form_config=form_config,
view_args=resource_requestctx.view_args,
)
return form_config


class RecordsUIResource(UIResource):
config: RecordsUIResourceConfig

Expand Down Expand Up @@ -530,9 +559,10 @@ def create(self):
extra_context=extra_context,
ui_links=ui_links,
)
empty_record = self.default_communities(empty_record, form_config, resource_requestctx)
empty_record = self.default_communities(
empty_record, form_config, resource_requestctx
)


self.run_components(
"before_ui_create",
data=empty_record,
Expand Down Expand Up @@ -572,21 +602,21 @@ def has_deposit_permissions(self, identity):
)
else:
return self.api_service.check_permission(identity, "create", record=None)

def default_communities(self, empty_record, form_config, resource_requestctx):
if 'allowed_communities' not in form_config:
if "allowed_communities" not in form_config:
return empty_record
if "community" in resource_requestctx.args:
community = resource_requestctx.args["community"]
for c in form_config["allowed_communities"]:
if c['slug'] == community:
empty_record["parent"]["communities"]["default"] = c['id']
if c["slug"] == community:
empty_record["parent"]["communities"]["default"] = c["id"]
break
elif len(form_config["allowed_communities"]) == 1:
community = form_config["allowed_communities"][0]
empty_record["parent"]["communities"]["default"] = community["id"]
return empty_record

@property
def api_service(self):
return current_service_registry.get(self.config.api_service)
Expand Down Expand Up @@ -638,7 +668,7 @@ def permission_denied(self, error, *args, **kwargs):
default_macro="PermissionDenied",
),
pid=getattr(error, "pid_value", None) or getattr(error, "pid", None),
error=error
error=error,
)


Expand Down

0 comments on commit ae35da6

Please sign in to comment.