~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to ivle/webapp/admin/subject.py

  • Committer: William Grant
  • Date: 2010-02-12 04:14:03 UTC
  • Revision ID: grantw@unimelb.edu.au-20100212041403-ilywc9djfi3arxfl
Derive the subject forms from BaseFormView.

Show diffs side-by-side

added added

removed removed

Lines of Context:
114
114
    code = formencode.validators.UnicodeString(not_empty=True)
115
115
 
116
116
 
117
 
class SubjectFormView(XHTMLView):
 
117
class SubjectFormView(BaseFormView):
118
118
    """An abstract form to add or edit a subject."""
119
119
    tab = 'subjects'
120
120
 
121
121
    def authorize(self, req):
122
122
        return req.user is not None and req.user.admin
123
123
 
124
 
    def filter(self, stream, ctx):
125
 
        return stream | HTMLFormFiller(data=ctx['data'])
126
 
 
127
124
    def populate_state(self, state):
128
125
        state.existing_subject = None
129
126
 
130
 
    def populate(self, req, ctx):
131
 
        if req.method == 'POST':
132
 
            data = dict(req.get_fieldstorage())
133
 
            try:
134
 
                validator = SubjectSchema()
135
 
                self.populate_state(req)
136
 
                data = validator.to_python(data, state=req)
137
 
 
138
 
                subject = self.update_subject_object(req, data)
139
 
 
140
 
                req.store.commit()
141
 
                req.throw_redirect(req.publisher.generate(subject))
142
 
            except formencode.Invalid, e:
143
 
                errors = e.unpack_errors()
144
 
        else:
145
 
            data = self.get_default_data(req)
146
 
            errors = {}
147
 
 
148
 
        if errors:
149
 
            req.store.rollback()
150
 
 
151
 
        ctx['context'] = self.context
152
 
        ctx['data'] = data or {}
153
 
        ctx['errors'] = errors
 
127
    @property
 
128
    def validator(self):
 
129
        return SubjectSchema()
 
130
 
 
131
    def get_return_url(self, obj):
 
132
        return '/subjects'
154
133
 
155
134
 
156
135
class SubjectNew(SubjectFormView):
157
136
    """A form to create a subject."""
158
137
    template = 'templates/subject-new.html'
159
138
 
160
 
    def populate_state(self, state):
161
 
        state.existing_subject = self.context
162
 
 
163
139
    def get_default_data(self, req):
164
140
        return {}
165
141
 
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,
188
164
            }
189
165
 
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']