107
107
Semester.year, Semester.semester)
110
class SubjectShortNameUniquenessValidator(formencode.FancyValidator):
111
"""A FormEncode validator that checks that a subject name is unused.
110
class SubjectUniquenessValidator(formencode.FancyValidator):
111
"""A FormEncode validator that checks that a subject attribute is unique.
113
113
The subject referenced by state.existing_subject is permitted
114
114
to hold that name. If any other object holds it, the input is rejected.
116
:param attribute: the name of the attribute to check.
117
:param display: a string to identify the field in case of error.
116
def __init__(self, matching=None):
117
self.matching = matching
120
def __init__(self, attribute, display):
121
self.attribute = attribute
122
self.display = display
119
124
def _to_python(self, value, state):
120
if (state.store.find(
121
Subject, short_name=value).one() not in
125
if (state.store.find(Subject, **{self.attribute: value}).one() not in
122
126
(None, state.existing_subject)):
123
127
raise formencode.Invalid(
124
'Short name already taken', value, state)
128
'%s already taken' % self.display, value, state)
128
132
class SubjectSchema(formencode.Schema):
129
133
short_name = formencode.All(
130
SubjectShortNameUniquenessValidator(),
134
SubjectUniquenessValidator('short_name', 'URL name'),
131
135
URLNameValidator(not_empty=True))
132
136
name = formencode.validators.UnicodeString(not_empty=True)
133
code = formencode.validators.UnicodeString(not_empty=True)
137
code = formencode.All(
138
SubjectUniquenessValidator('code', 'Subject code'),
139
formencode.validators.UnicodeString(not_empty=True))
136
142
class SubjectFormView(BaseFormView):