Skip to content

Commit

Permalink
Fix string replacement function and fix flaw in regex to match {{mode…
Browse files Browse the repository at this point in the history
…l.prop}} tags
  • Loading branch information
sadnub committed Apr 23, 2024
1 parent 56cbbbe commit bcc1986
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
13 changes: 8 additions & 5 deletions api/tacticalrmm/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from base64 import b64encode
from contextlib import suppress
from requests.utils import requote_uri
from typing import TYPE_CHECKING, Any, Dict, List, Optional, cast, Tuple
from typing import TYPE_CHECKING, List, Optional, cast, Tuple

import requests
import websockets
Expand Down Expand Up @@ -219,14 +219,17 @@ def make_alpha_numeric(s: str):

def find_and_replace_db_values_str(*, text: str, instance):
import re

from tacticalrmm.utils import RE_DB_VALUE, get_db_value

if not instance:
return text

return_string = text

for string, model, prop in re.findall(RE_DB_VALUE, text):
value = get_db_value(string=f"{model}.{prop}", instance=instance)
return text.replace(string, str(value))
else:
return text
return_string = return_string.replace(string, str(value))
return return_string


def _run_url_rest_action(*, url: str, method, body: str, headers: str, instance=None):
Expand Down
2 changes: 1 addition & 1 deletion api/tacticalrmm/tacticalrmm/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ def get_latest_trmm_ver() -> str:
# regex for db data replacement
# will return 3 groups of matches in a tuple when uses with re.findall
# i.e. - {{client.name}}, client, name
RE_DB_VALUE = re.compile(r"(\{\{\s*(client|site|agent|global|alert)\.(.*)\s*\}\})")
RE_DB_VALUE = re.compile(r"(\{\{\s*(client|site|agent|global|alert)\.([\w\-\s]+)\s*\}\})")


# Receives something like {{ client.name }} and a Model instance of Client, Site, or Agent. If an
Expand Down

0 comments on commit bcc1986

Please sign in to comment.