Skip to content

Commit

Permalink
Forbid hyphens in CommunicationEventType code field
Browse files Browse the repository at this point in the history
  • Loading branch information
codeinthehole committed Jun 4, 2015
1 parent eda02cf commit 65bcafb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/oscar/apps/customer/abstract_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.conf import settings
from django.contrib.auth import models as auth_models
from django.core.urlresolvers import reverse
from django.core.validators import RegexValidator
from django.db import models
from django.template import Template, Context, TemplateDoesNotExist
from django.template.loader import get_template
Expand Down Expand Up @@ -141,6 +142,12 @@ class AbstractCommunicationEventType(models.Model):
code = AutoSlugField(
_('Code'), max_length=128, unique=True, populate_from='name',
separator=six.u("_"), uppercase=True, editable=True,
validators=[
RegexValidator(
regex=r'^[a-zA-Z_][0-9a-zA-Z_]*$',
message=_(
"Code can only contain the letters a-z, A-Z, digits, "
"and underscores, and can't start with a digit"))],
help_text=_("Code used for looking up this event programmatically"))

#: Name is the friendly description of an event for use in the admin
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/customer/test_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import pytest
from django.core import exceptions

from oscar.apps.customer import models


def test_communication_event_type_code_forbids_hyphens():
ctype = models.CommunicationEventType(code="A-B")
with pytest.raises(exceptions.ValidationError):
ctype.full_clean()

0 comments on commit 65bcafb

Please sign in to comment.