Skip to content

Commit

Permalink
fix using re
Browse files Browse the repository at this point in the history
  • Loading branch information
v.khadyev committed Jan 10, 2024
1 parent c3fdd06 commit 39bed8e
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions overhave/admin/views/testing_users.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from string import ascii_letters, digits
from re import fullmatch
from typing import cast

from flask_admin.form import JSONField
Expand Down Expand Up @@ -51,8 +51,7 @@ class TestUserView(ModelViewConfigured):

_feature_type: FeatureTypeName | None = None

_russian_alphabet = "".join([chr(char) for char in range(ord("а"), ord("а") + 32)] + ["ё"])
_allowed_symbols = ascii_letters + digits + "_" + _russian_alphabet + _russian_alphabet.upper()
_allowed_symbols = r"\w*"

def on_form_prefill(self, form: Form, id) -> None: # type: ignore # noqa: A002
if not isinstance(form._obj, db.TestUser):
Expand Down Expand Up @@ -82,14 +81,10 @@ def _validate_json(model: db.TestUser) -> None:
raise ValidationError(f"Could not convert specified data into {parser.__name__} model!")

def on_model_change(self, form: Form, model: db.TestUser, is_created: bool) -> None:
if model.name is not None and any((char not in self._allowed_symbols) for char in model.name):
raise ValidationError(
"Name should contain only characters from russian or english alphabet, digits or underscore!"
)
if model.key is not None and any((char not in self._allowed_symbols) for char in model.key):
raise ValidationError(
"Key should contain only characters from russian or english alphabet, digits or underscore!"
)
if model.name is not None and not fullmatch(self._allowed_symbols, model.name):
raise ValidationError("Name should contain only characters, digits or underscore!")
if model.key is not None and not fullmatch(self._allowed_symbols, model.key):
raise ValidationError("Key should contain only characters, digits or underscore!")
self._feature_type = cast(FeatureTypeName, model.feature_type.name)
self._validate_json(model)
if is_created:
Expand Down

0 comments on commit 39bed8e

Please sign in to comment.