Skip to content

Commit

Permalink
Validation of brand tags (#145)
Browse files Browse the repository at this point in the history
* Added Brand Tag validation on Save to allow only alpha-numerical characters, underscores and dashes

* Tag Validation. Only alpha-numeric characters, underscore and dash are allowed. When there is an error, the form does not get submitted, the error message is displayed near the Ta
g field. Tests are also completed.

* reformatting with black

* added 'http://' to the website link so that the tests passed
  • Loading branch information
evkonradi authored Nov 20, 2023
1 parent 5a558d6 commit dbfa71f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
16 changes: 16 additions & 0 deletions brand/models/brand.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.db import models
from django.db.models.query import QuerySet
from django.template.defaultfilters import truncatechars
from django.core.exceptions import ValidationError

from cities_light.models import Region, SubRegion
from django_countries.fields import CountryField
Expand All @@ -16,6 +17,16 @@
# from Levenshtein import distance as lev


def validate_tag(value):
"""This is the function that is used to validate the TAG"""
if re.match("^[A-Za-z0-9_-]*$", str(value)):
return value
else:
raise ValidationError(
"Tag can contain only alpha-numeric characters, underscores and dashes"
)


class Brand(TimeStampedModel):
"""
A "Brand" is the instance shown to the end user.
Expand Down Expand Up @@ -68,6 +79,7 @@ def short_tag(self):
editable=True,
unique=True,
help_text="the tag we use or this brand record at Bank.Green. ",
validators=[validate_tag],
)
tag_locked = models.BooleanField(default=True)

Expand Down Expand Up @@ -318,3 +330,7 @@ def create_spelling_dictionary(cls):
}

return spelling_dict

def save(self, *args, **kwargs):
self.full_clean()
return super().save(*args, **kwargs)
22 changes: 22 additions & 0 deletions brand/tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,25 @@ def test_compute_inherited_rating_circular_path_can_return_unknown(self):
self.assertEqual(
self.commentary5.compute_inherited_rating(throw_error=False), RatingChoice.UNKNOWN
)


class BrandTagTestCase(TestCase):
# test for a Valid Tag
def test_brand_valid_tag(self):
brand1 = Brand(name="Test Brand 1", tag="Tag_Brand-1")
# this should not raise any errors
brand1.save()

# test for an Invalid Tags
def test_brand_invalid_tag(self):
# test case with not allowed characters
brand1 = Brand(name="Test Brand 1", tag="TagBrand%$1")

with self.assertRaises(ValidationError):
brand1.save()

# test case with the spaces
brand1 = Brand(name="Test Brand 1", tag="Tag Brand 1")

with self.assertRaises(ValidationError):
brand1.save()
2 changes: 1 addition & 1 deletion brand/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def create_test_brands():
tag="test_brand_1",
name="Test Brand 1",
aliases="test brand, testb",
website="www.testbrand.com",
website="http://www.testbrand.com",
permid="test permid",
viafid="test viafid",
lei="test lei",
Expand Down

0 comments on commit dbfa71f

Please sign in to comment.