profile: add extra validation to profile field names (#33849)

This commit is contained in:
Frédéric Péters 2019-06-23 12:09:32 +02:00
parent 57765a7916
commit e0192298af
2 changed files with 17 additions and 2 deletions

View File

@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import django.core.validators
from django.db import models, migrations
@ -16,7 +17,12 @@ class Migration(migrations.Migration):
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('label', models.CharField(unique=True, max_length=63, verbose_name='label')),
('description', models.TextField(verbose_name='description', blank=True)),
('name', models.SlugField(unique=True, max_length=256, verbose_name='name')),
('name', models.SlugField(unique=True, max_length=256, verbose_name='name',
error_messages={b'unique': 'Field names must be unique.'},
validators=[django.core.validators.RegexValidator(
b'^[a-z][a-z0-9_]*\\Z',
'Enter valid variable name starting with a letter and consisting of letters, numbers, or underscores.')]
)),
('required', models.BooleanField(default=False, verbose_name='required')),
('asked_on_registration', models.BooleanField(default=False, verbose_name='asked on registration')),
('user_editable', models.BooleanField(default=True, verbose_name='user editable')),

View File

@ -14,17 +14,26 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from django.core.validators import RegexValidator
from django.db import models
from django.utils.translation import ugettext_lazy as _
validate_attribute_name = RegexValidator(
r'^[a-z][a-z0-9_]*\Z',
_('Enter valid variable name starting with a letter and consisting of letters, numbers, or underscores.'),
)
class AttributeDefinition(models.Model):
label = models.CharField(verbose_name=_('label'),
max_length=63, unique=True)
description = models.TextField(verbose_name=_('description'),
blank=True)
name = models.SlugField(verbose_name=_('name'),
max_length=256, unique=True)
max_length=256, unique=True,
validators=[validate_attribute_name],
error_messages={'unique': _('Field names must be unique.')})
required = models.BooleanField(verbose_name=_('required'),
blank=True, default=False)
asked_on_registration = models.BooleanField(verbose_name=_('asked on registration'),