Changeset 4562 for waeup/branches/ulif-rewrite/src
- Timestamp:
- 16 Dec 2009, 10:46:08 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
waeup/branches/ulif-rewrite/src/waeup/university/certificate.py
r4516 r4562 9 9 from waeup.interfaces import (ICertificate, ICertificateContainer, 10 10 ICertificateCourse, ICourse, IDepartment) 11 from waeup.viewlets import FormWrapMixin, Index, Manage, MainArea 11 from waeup.viewlets import (FormWrapMixin, Index, Manage, MainArea, 12 AddCertificateCourse) 12 13 from waeup.utils.helpers import getName 13 14 … … 52 53 code = "%s_%s" % (course.code, level) 53 54 self[code] = CertificateCourse(course, level, core_or_elective) 54 55 def delCourseRef(self, code): 55 self[code].__parent__ = self 56 self[code].__name__ = code 57 self._p_changed = True 58 59 def delCourseRef(self, code, level=None): 56 60 """Delete a course denoted by its code. 57 61 """ … … 59 63 if self[key].getCourseCode() != code: 60 64 continue 65 if level is not None and str(self[key].level) != str(level): 66 # found a course with correct key but wrong level... 67 continue 61 68 del self[key] 62 69 self._p_changed = True … … 113 120 grok.order(2) 114 121 122 def getURLAndName(self, context): 123 url = self.view.url(context) 124 name = getName(context) 125 return (url, name) 126 115 127 def render(self): 116 return '''<h5>%s</h5><h4>%s</h4><h3>%s</h3>''' % ( 117 getName(self.context.__parent__.__parent__.__parent__), 118 getName(self.context.__parent__.__parent__), 119 self.context.title) 128 url1, name1 = self.getURLAndName( 129 self.context.__parent__.__parent__.__parent__) 130 url2, name2 = self.getURLAndName( 131 self.context.__parent__.__parent__) 132 url3 = self.view.url(self.context) 133 name3 = self.context.title 134 return '''<h5><a href="%s">%s</a></h5> 135 <h4><a href="%s">%s</a></h4> 136 <h3><a href="%s">%s</a></h3>''' % ( 137 url1, name1, url2, name2, url3, name3) 120 138 121 139 class DisplayForm(grok.DisplayForm): … … 178 196 grok.order(2) 179 197 180 def render(self): 181 result = '<h3>Courses</h4>\n' 198 def update(self): 199 if 'delcourse' in self.request.form: 200 code = self.request.form['code'] 201 level = self.request.form['level'] 202 self.context.delCourseRef(code, level=level) 203 return 204 205 def getCourseList(self): 206 def cmpCourses(a, b): 207 if a['level'] < b['level']: 208 return -1 209 if a['level'] > b['level']: 210 return 1 211 if a['mandatory'] != b['mandatory']: 212 if a['mandatory'] == False and b['mandatory'] == True: 213 return -1 214 return 1 215 if a['code'] < b['code']: 216 return -1 217 if a['code'] > b['code']: 218 return 1 219 return 0 220 221 entries = [] 222 levels = dict() 182 223 for key, course in self.context.items(): 183 224 certcourse = self.context[key] 184 225 course = certcourse.course 185 result += '<div><a href="%s">%s</a>%s (%s / %s)</div>' % ( 186 self.view.url(course), course.code, course.title, 187 certcourse.level, certcourse.core_or_elective) 188 return result 226 level = certcourse.level 227 mandatory = certcourse.core_or_elective 228 title = course.title 229 code = course.code 230 course_url = self.view.url(self.context[key]) 231 semester = course.semester 232 233 new_entry = dict( 234 level=level, mandatory=mandatory, url=course_url, 235 title=title, code=code, semester=semester) 236 if str(level) not in levels.keys(): 237 levels[str(level)] = dict() 238 if str(semester) not in levels[str(level)].keys(): 239 levels[str(level)][str(semester)] = [] 240 levels[str(level)][str(semester)].append(new_entry) 241 return levels 242 243 244 class AddCertificateCourseForm(grok.AddForm): 245 grok.context(Certificate) 246 247 form_fields = grok.AutoFields(ICertificateCourse) 248 label = 'Add a course' 249 250 @grok.action('Add course') 251 def addCertcourse(self, **data): 252 try: 253 self.context.addCourseRef(**data) 254 except DuplicationError: 255 self.status = Invalid('The chosen course is already part of' 256 'this certificate') 257 return 258 self.redirect(self.url(self.context)) 259 260 @grok.action('Cancel') 261 def cancel(self, **data): 262 self.redirect(self.url(self.context)) 263 return 264 265 class AddCertificateCourse(FormWrapMixin, grok.Viewlet): 266 """A viewlet that wraps the `AddCertificateCourseForm`. 267 """ 268 grok.viewletmanager(MainArea) 269 grok.context(ICertificate) 270 grok.view(AddCertificateCourse) 271 grok.require('waeup.manageUniversity') 272 273 formview_name = 'addcertificatecourseform' # The name of the formview we 274 # want to be rendered in this 275 # viewlet. 276 277 class DisplayCertCourseForm(grok.DisplayForm): 278 grok.context(ICertificateCourse) 279 form_fields = grok.AutoFields(ICertificateCourse) 280 281 class CertCourseContent(FormWrapMixin, grok.Viewlet): 282 """A viewlet that wraps the `DisplayForm`. 283 """ 284 grok.viewletmanager(MainArea) 285 grok.context(ICertificateCourse) 286 grok.view(Index) 287 grok.order(2) 288 formview_name = 'displaycertcourseform' # The name of the formview we want 289 # to be rendered in this viewlet. 290 291 class ManageCertCourseForm(grok.EditForm): 292 """Manage the basic properties of a `CertificateCourse` instance. 293 """ 294 grok.context(ICertificateCourse) 295 form_fields = grok.AutoFields(ICertificateCourse) 296 297 @property 298 def label(self): 299 # Set certificate name as form label 300 return 'Edit certificate course %s' % self.context.course.code 301 return self.form_fields['title'].field.get(self.context) 302 303 @grok.action('Save and return') 304 def saveAndReturn(self, **data): 305 parent = self.context.__parent__ 306 if self.context.level == data['level']: 307 self.applyData(self.context, **data) 308 else: 309 # The level changed. We have to create a new certcourse as 310 # the key of the entry will change as well... 311 old_level = self.context.level 312 parent.addCourseRef(**data) 313 parent.delCourseRef(data['course'].code, level=old_level) 314 self.redirect(self.url(parent)) 315 return 316 317 @grok.action('Cancel') 318 def cancel(self, **data): 319 self.redirect(self.url(self.context)) 320 return 321 322 class ManageCertificateCourse(FormWrapMixin, grok.Viewlet): 323 """A viewlet that renders the `ManageCertCourseForm`. 324 """ 325 grok.viewletmanager(MainArea) 326 grok.context(ICertificateCourse) 327 grok.view(Manage) 328 grok.name('manage') 329 #grok.require('waeup.manageUniversity') 330 331 formview_name = 'managecertcourseform' # The name of the formview 332 # we want to be rendered 333 # in this viewlet. 334 189 335 190 336 @grok.subscribe(ICourse, grok.IObjectRemovedEvent)
Note: See TracChangeset for help on using the changeset viewer.