Skip to content

Commit

Permalink
Increase interest
Browse files Browse the repository at this point in the history
  • Loading branch information
atompie committed Dec 1, 2023
1 parent 76d17f7 commit 8e7b51c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 8 deletions.
10 changes: 10 additions & 0 deletions test/unit/test_is_valid_string.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from tracardi.process_engine.action.v1.interest.utils import is_valid_string


def test_alphanumeric():
assert is_valid_string("abc123") == True
assert is_valid_string("abc_123") == True
assert is_valid_string("abc-123") == True
assert is_valid_string("abc@123") == False
assert is_valid_string("___---") == False
assert is_valid_string("_") == False
19 changes: 15 additions & 4 deletions tracardi/process_engine/action/v1/interest/decrease/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from tracardi.service.plugin.runner import ActionRunner
from tracardi.service.plugin.domain.result import Result
from ..config import Configuration
from ..utils import is_valid_string


def validate(config: dict):
Expand All @@ -28,7 +29,16 @@ async def run(self, payload: dict, in_edge=None) -> Result:

return Result(value={"message": msg}, port="error")

self.profile.decrease_interest(self.config.interest, float(self.config.value))
dot = self._get_dot_accessor(payload)
interest_key = dot[self.config.interest]

if not is_valid_string(interest_key):
message = (f"Invalid interest name. Expected alpha-numeric string without spaces, got `{interest_key}`. "
f"Interest name must be an alpha-numeric string without spaces. Hyphen and dashes are allowed.")
self.console.error(message)
return Result(value={"message": message}, port="error")

self.profile.decrease_interest(interest_key, float(self.config.value))
self.profile.operation.update = True

return Result(port="payload", value=payload)
Expand All @@ -54,7 +64,7 @@ def register() -> Plugin:
id="interest",
name="Interest name",
description="Please type interest name.",
component=FormComponent(type="text", props={"label": "Interest name"})
component=FormComponent(type="dotPath", props={"label": "Interest name"})
),
FormField(
id="value",
Expand All @@ -65,9 +75,10 @@ def register() -> Plugin:
]
)]
),
version='0.8.0',
version='0.8.2',
license="MIT + CC",
author="Risto Kowaczewski"
author="Risto Kowaczewski",
manual="decrease_interest"
),
metadata=MetaData(
name='Decrease interest',
Expand Down
19 changes: 15 additions & 4 deletions tracardi/process_engine/action/v1/interest/increase/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from tracardi.service.plugin.runner import ActionRunner
from tracardi.service.plugin.domain.result import Result
from ..config import Configuration
from ..utils import is_valid_string


def validate(config: dict):
Expand All @@ -28,7 +29,16 @@ async def run(self, payload: dict, in_edge=None) -> Result:

return Result(value={"message": msg}, port="error")

self.profile.increase_interest(self.config.interest, float(self.config.value))
dot = self._get_dot_accessor(payload)
interest_key = dot[self.config.interest]

if not is_valid_string(interest_key):
message = (f"Invalid interest name. Expected alpha-numeric string without spaces, got `{interest_key}`. "
f"Interest name must be an alpha-numeric string without spaces. Hyphen and dashes are allowed.")
self.console.error(message)
return Result(value={"message": message}, port="error")

self.profile.increase_interest(interest_key, float(self.config.value))
self.profile.operation.update = True

return Result(port="payload", value=payload)
Expand All @@ -54,7 +64,7 @@ def register() -> Plugin:
id="interest",
name="Interest name",
description="Please type interest name.",
component=FormComponent(type="text", props={"label": "Interest name"})
component=FormComponent(type="dotPath", props={"label": "Interest name"})
),
FormField(
id="value",
Expand All @@ -65,9 +75,10 @@ def register() -> Plugin:
]
)]
),
version='0.8.0',
version='0.8.2',
license="MIT + CC",
author="Risto Kowaczewski"
author="Risto Kowaczewski",
manual="increase_interest"
),
metadata=MetaData(
name='Increase interest',
Expand Down
21 changes: 21 additions & 0 deletions tracardi/process_engine/action/v1/interest/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
def is_valid_string(variable):
"""
Check if the variable is a string composed of alphanumeric characters, underscores (_), and hyphens (-),
contains no spaces, and has at least one alphanumeric character.
Args:
variable (str): The variable to be checked.
Returns:
bool: True if the variable meets the criteria, False otherwise.
"""
if not isinstance(variable, str):
return False

# Check for at least one alphanumeric character
if not any(char.isalnum() for char in variable):
return False

# Checking if all characters are alphanumeric, underscore, or hyphen, and if there are no spaces
return all(char.isalnum() or char in ['_', '-'] for char in variable) and ' ' not in variable

0 comments on commit 8e7b51c

Please sign in to comment.