Skip to content

Commit

Permalink
fix: Improve and standardize Script module vfuncs
Browse files Browse the repository at this point in the history
- Fixes #1386
- Fixes #1387
  • Loading branch information
phorward committed Jan 24, 2025
1 parent 40d52b8 commit f52259a
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/viur/core/modules/script.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import re
import typing as t
from viur.core.bones import *
from viur.core.prototypes.tree import Tree, TreeSkel, SkelType
from viur.core.modules.file import File
from viur.core import db, conf, current, skeleton, tasks, errors
from viur.core.decorators import exposed
from viur.core.i18n import translate

# pre-compile patterns for vfuncs
DIRECTORY_PATTERN = re.compile(r'^[a-zA-Z0-9äöüÄÖÜ_-]*$')
FILE_PATTERN = re.compile(r'^[a-zA-Z0-9äöüÄÖÜ_-]+?.py$')


class BaseScriptAbstractSkel(TreeSkel):

Expand All @@ -24,7 +20,7 @@ def fromClient(cls, skel, data, *args, **kwargs):
# Set script name when provided, so that the path can be regenerated
if name := data.get("name"):
skel["name"] = name
conf.main_app.vi.script.update_path(skel)
conf.main_app.script.update_path(skel)

ret = super().fromClient(skel, data, *args, **kwargs)

Expand Down Expand Up @@ -54,7 +50,7 @@ class ScriptNodeSkel(BaseScriptAbstractSkel):
name = StringBone(
descr="Folder",
required=True,
vfunc=lambda value: not DIRECTORY_PATTERN.match(value)
vfunc=lambda value: None if File.is_valid_filename(value) else "Foldername is invalid"
)


Expand All @@ -63,12 +59,16 @@ class ScriptLeafSkel(BaseScriptAbstractSkel):

name = StringBone(
descr="Filename",
vfunc=lambda value: not FILE_PATTERN.match(value),
required=True,
vfunc=lambda value:
None if File.is_valid_filename(value) and value.endswith(".py")
else "Filename is invalid or doesn't have a '.py'-suffix",
)

script = RawBone(
descr="Code",
indexed=False
indexed=False,
required=True,
)

access = SelectBone(
Expand Down

0 comments on commit f52259a

Please sign in to comment.