Changeset 4332 for waeup/branches/ulif-rewrite
- Timestamp:
- 22 Jun 2009, 12:57:10 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
waeup/branches/ulif-rewrite/src/waeup/university/certificate.txt
r4325 r4332 230 230 <waeup.university.certificate.Certificate object at 0x...> 231 231 232 Event Subscribers 233 ================= 234 235 .. function:: removedCourseHandler(course, event) 236 237 An event subscriber triggered for 238 :class:`grok.IObjectRemovedEvent`s, when an :class:`ICourse` 239 instance is removed from a container. 240 241 Tries to remove all referencing :class:`CertificateCourse` 242 instances that reference the removed course. 243 244 To accomplish that, the parents of the removed course are looked up 245 for a certifcate container which contains a certificate-course that 246 contains a reference to the deleted course. 247 248 .. seealso:: :ref:`removecertificatecourses` 249 250 **handles:** 251 :class:`ICourse` 252 253 **event type:** 254 :class:`grok.IObjectRemovedEvent` 255 232 256 Examples 233 257 ======== … … 268 292 True 269 293 294 Also instances of :class:`CertificateCourse` can be created by asking 295 the component architechture: 296 297 >>> from zope.component import createObject 298 >>> mycertcourse = createObject(u'waeup.CertificateCourse') 299 >>> mycertcourse 300 <waeup.university.certificate.CertificateCourse object at 0x...> 301 302 .. _removecertificatecourses: 303 304 Persistence of certificate courses 305 ---------------------------------- 306 307 If a certificate course requires a certain course and this is course 308 is deleted, also the referencing certificate course is deleted. 309 310 We setup a data structure that reflects typical usage. It looks like 311 this:: 312 313 Department-Instance 314 | 315 +---> courses 316 | | 317 | +--------------------> Course-Instance 318 | ^ 319 +---> certificates | 320 | | 321 +-----> Certificate-Instance | 322 | | 323 +------> Certificate-Course 324 325 The certifcate-Course here refers to a Course-Instance. 326 327 In Python we build such a structure like this (from top to bottom): 328 329 >>> from zope.component import createObject 330 >>> mydept = createObject('waeup.Department') 331 332 In real world use this data will be stored in a ZODB. We setup our own 333 litte ZODB backend (which is easy!): 334 335 >>> from ZODB import FileStorage, DB 336 >>> dbpath = 'tinyData.fs' 337 >>> class TinyZODB(object): 338 ... def __init__(self, path=dbpath): 339 ... self.storage = FileStorage.FileStorage(path) 340 ... self.db = DB(self.storage) 341 ... self.connection = self.db.open() 342 ... self.dbroot = self.connection.root() 343 ... def close(self): 344 ... self.connection.close() 345 ... self.db.close() 346 ... self.storage.close() 347 348 Now we can use this ZODB as backend database and store our data 349 structure: 350 351 >>> import transaction 352 >>> db = TinyZODB() 353 >>> dbroot = db.dbroot 354 >>> dbroot['mydept'] = mydept 355 >>> mycourse = createObject('waeup.Course') 356 >>> mycourse.code = 'MYCOURSE' 357 >>> mydept.courses.addCourse(mycourse) 358 >>> mycert = createObject('waeup.Certificate') 359 >>> mycert.code = 'MYCERT' 360 >>> mydept.certificates.addCertificate(mycert) 361 >>> mycert.addCourseRef(mycourse) 362 363 >>> transaction.commit() 364 365 The data is now stored in the ZODB. We can close the DB, reopen it 366 later and the data will still be there: 367 368 >>> db.close() 369 >>> newdb = TinyZODB() 370 >>> newdbroot = newdb.dbroot 371 >>> list(newdbroot) 372 ['mydept'] 373 374 The certificate-course we stored in the certificate is indeed a 375 reference to the course, not a copy of it: 376 377 >>> course = newdbroot['mydept'].courses['MYCOURSE'] 378 >>> certcourse = newdbroot['mydept'].certificates['MYCERT']['MYCOURSE'] 379 >>> certcourse.course is course 380 True 381 382 So, we can be sure that modifications to the course are immediately 383 reflected in the certcourse. 384 385 The interesting question now is: what happens to ``mycertcourse`` when 386 the referenced course ``mycourse`` is deleted? 387 388 Let's have a look: 389 390 >>> list(newdbroot['mydept'].certificates['MYCERT'].keys()) 391 [u'MYCOURSE'] 392 393 >>> del newdbroot['mydept'].courses['MYCOURSE'] 394 >>> newdbroot['mydept']._p_changed = True 395 >>> transaction.commit() 396 >>> list(newdbroot['mydept'].certificates['MYCERT'].keys()) 397 [] 398 399 As we can see, the course was also removed from the certificate. 400 401 Clean up: 402 403 >>> import os 404 >>> os.unlink(dbpath)
Note: See TracChangeset for help on using the changeset viewer.