114
114
code = formencode.validators.UnicodeString(not_empty=True)
117
class SubjectFormView(XHTMLView):
117
class SubjectFormView(BaseFormView):
118
118
"""An abstract form to add or edit a subject."""
121
121
def authorize(self, req):
122
122
return req.user is not None and req.user.admin
124
def filter(self, stream, ctx):
125
return stream | HTMLFormFiller(data=ctx['data'])
127
124
def populate_state(self, state):
128
125
state.existing_subject = None
130
def populate(self, req, ctx):
131
if req.method == 'POST':
132
data = dict(req.get_fieldstorage())
134
validator = SubjectSchema()
135
self.populate_state(req)
136
data = validator.to_python(data, state=req)
138
subject = self.update_subject_object(req, data)
141
req.throw_redirect(req.publisher.generate(subject))
142
except formencode.Invalid, e:
143
errors = e.unpack_errors()
145
data = self.get_default_data(req)
151
ctx['context'] = self.context
152
ctx['data'] = data or {}
153
ctx['errors'] = errors
129
return SubjectSchema()
131
def get_return_url(self, obj):
156
135
class SubjectNew(SubjectFormView):
157
136
"""A form to create a subject."""
158
137
template = 'templates/subject-new.html'
160
def populate_state(self, state):
161
state.existing_subject = self.context
163
139
def get_default_data(self, req):
166
def update_subject_object(self, req, data):
142
def save_object(self, req, data):
167
143
new_subject = Subject()
168
144
new_subject.short_name = data['short_name']
169
145
new_subject.name = data['name']
187
163
'code': self.context.code,
190
def update_subject_object(self, req, data):
166
def save_object(self, req, data):
191
167
self.context.short_name = data['short_name']
192
168
self.context.name = data['name']
193
169
self.context.code = data['code']