[4313] | 1 | :mod:`waeup.university.certificate` -- Certificates for WAeUP |
---|
| 2 | ************************************************************* |
---|
[4290] | 3 | |
---|
[4325] | 4 | .. module:: waeup.university.certificate |
---|
| 5 | |
---|
[4313] | 6 | Components that represent and manage certificates. |
---|
| 7 | |
---|
[4290] | 8 | :Test-Layer: unit |
---|
| 9 | |
---|
[4312] | 10 | Because certificates make use of components registered with the Zope |
---|
| 11 | Component Architecture (ZCA), we first have to grok the `waeup` |
---|
| 12 | package. This happens automatically in real-world use: |
---|
| 13 | |
---|
| 14 | >>> import grok |
---|
| 15 | >>> grok.testing.grok('waeup') |
---|
| 16 | |
---|
[4290] | 17 | |
---|
[4313] | 18 | Content Classes (models and containers) |
---|
| 19 | ======================================= |
---|
[4290] | 20 | |
---|
[4322] | 21 | |
---|
[4313] | 22 | :class:`Certificate` |
---|
| 23 | -------------------- |
---|
[4290] | 24 | |
---|
[4313] | 25 | .. class:: Certificate([code=u'NA',[ title=u'Unnamed Certificate',[ category=None,[ study_mode=None,[ start_level=None,[ end_level=None,[ application_category=None,[ m_prefix=u'',[ max_pass = u'']]]]]]]]]) |
---|
[4290] | 26 | |
---|
[4313] | 27 | Create a certificate object with the given parameters. |
---|
[4312] | 28 | |
---|
[4313] | 29 | .. attribute:: grok.implements(ICertificate) |
---|
[4290] | 30 | |
---|
[4313] | 31 | All parameters are optional: |
---|
[4290] | 32 | |
---|
[4313] | 33 | >>> from waeup.university.certificate import Certificate |
---|
| 34 | >>> mycertificate = Certificate() |
---|
[4290] | 35 | |
---|
[4313] | 36 | Certificates have the attributes required by the `ICertificate` interface: |
---|
[4290] | 37 | |
---|
[4313] | 38 | >>> from waeup.interfaces import ICertificate |
---|
| 39 | >>> ICertificate.providedBy(mycertificate) |
---|
| 40 | True |
---|
[4290] | 41 | |
---|
[4313] | 42 | >>> from zope.interface.verify import verifyObject |
---|
| 43 | >>> verifyObject(ICertificate, mycertificate) |
---|
| 44 | True |
---|
[4290] | 45 | |
---|
[4322] | 46 | Beside the attributes, certificates are containers for |
---|
| 47 | certificate-courses (see :class:`CertificateCourse`). Each |
---|
| 48 | certificate course can be accessed by the code of the course it wraps. |
---|
| 49 | |
---|
[4313] | 50 | .. attribute:: title |
---|
[4290] | 51 | |
---|
[4313] | 52 | Each certificate has a title: |
---|
[4290] | 53 | |
---|
[4313] | 54 | >>> mycertificate.title |
---|
| 55 | u'Unnamed Certificate' |
---|
[4290] | 56 | |
---|
[4313] | 57 | .. attribute:: code |
---|
[4290] | 58 | |
---|
[4313] | 59 | Each certificate holds a code, which might be a shortcut or |
---|
| 60 | abbreviation of the real certificate name. By default the code |
---|
| 61 | is ``NA`` (=not assigned): |
---|
[4312] | 62 | |
---|
[4313] | 63 | >>> mycertificate.code |
---|
| 64 | u'NA' |
---|
[4312] | 65 | |
---|
[4313] | 66 | .. attribute:: review_state |
---|
[4312] | 67 | |
---|
[4313] | 68 | The review state can have one of the ``checking`` states defined |
---|
| 69 | in the WAeUP workflow. These are at least the states ``checked`` |
---|
| 70 | and ``unchecked``. After a certificate is created, the review |
---|
| 71 | state is ``unchecked``: |
---|
[4312] | 72 | |
---|
[4313] | 73 | >>> mycertificate.review_state |
---|
| 74 | 'unchecked' |
---|
[4312] | 75 | |
---|
[4313] | 76 | .. seealso:: |
---|
| 77 | :meth:`Certificate.check` -- mark a certificate as ``checked`` |
---|
[4312] | 78 | |
---|
[4313] | 79 | .. attribute:: category |
---|
[4312] | 80 | |
---|
[4313] | 81 | Each :class:`Certificate` instance has a category: |
---|
| 82 | |
---|
| 83 | >>> print mycertificate.category |
---|
| 84 | None |
---|
| 85 | |
---|
| 86 | .. XXX: This is not a proper description |
---|
| 87 | |
---|
| 88 | .. attribute:: study_mode |
---|
| 89 | |
---|
| 90 | Each :class:`Certificate` instance has a study mode: |
---|
| 91 | |
---|
| 92 | >>> print mycertificate.study_mode |
---|
| 93 | None |
---|
| 94 | |
---|
| 95 | .. XXX: This is not a proper description |
---|
| 96 | |
---|
| 97 | .. attribute:: start_level |
---|
| 98 | |
---|
| 99 | Each :class:`Certificate` instance has a start level: |
---|
| 100 | |
---|
| 101 | >>> print mycertificate.start_level |
---|
| 102 | None |
---|
| 103 | |
---|
| 104 | .. XXX: This is not a proper description |
---|
| 105 | |
---|
| 106 | .. attribute:: end_level |
---|
| 107 | |
---|
| 108 | Each :class:`Certificate` instance has a end level: |
---|
| 109 | |
---|
| 110 | >>> print mycertificate.end_level |
---|
| 111 | None |
---|
| 112 | |
---|
| 113 | .. XXX: This is not a proper description |
---|
| 114 | |
---|
| 115 | |
---|
| 116 | .. attribute:: application_category |
---|
| 117 | |
---|
| 118 | Each :class:`Certificate` instance has an application category: |
---|
| 119 | |
---|
| 120 | >>> print mycertificate.application_category |
---|
| 121 | None |
---|
| 122 | |
---|
| 123 | .. XXX: This is not a proper description |
---|
| 124 | |
---|
| 125 | |
---|
| 126 | .. attribute:: m_prefix |
---|
| 127 | |
---|
| 128 | Each :class:`Certificate` instance has an ``m_prefix``: |
---|
| 129 | |
---|
[4317] | 130 | >>> mycertificate.m_prefix |
---|
| 131 | u'' |
---|
[4313] | 132 | |
---|
| 133 | .. XXX: This is not a proper description |
---|
| 134 | |
---|
| 135 | |
---|
| 136 | .. attribute:: max_pass |
---|
| 137 | |
---|
| 138 | Each :class:`Certificate` instance has a maximum number of passes: |
---|
| 139 | |
---|
[4317] | 140 | >>> mycertificate.max_pass |
---|
| 141 | u'' |
---|
[4313] | 142 | |
---|
| 143 | .. XXX: This is not a proper description |
---|
| 144 | |
---|
| 145 | |
---|
| 146 | .. method:: check() |
---|
| 147 | |
---|
| 148 | Mark a certificate instance's review state as ``checked``: |
---|
| 149 | |
---|
| 150 | >>> mycertificate.review_state |
---|
| 151 | 'unchecked' |
---|
| 152 | |
---|
| 153 | >>> mycertificate.check() |
---|
| 154 | >>> mycertificate.review_state |
---|
| 155 | 'checked' |
---|
| 156 | |
---|
| 157 | We cannot uncheck a certificate: |
---|
| 158 | |
---|
| 159 | >>> mycertificate.review_state = 'init' |
---|
| 160 | Traceback (most recent call last): |
---|
| 161 | ... |
---|
| 162 | InvalidTransitionError: Transition 'init' requires 'None' |
---|
| 163 | as source state (is: 'checked') |
---|
| 164 | |
---|
[4322] | 165 | .. method:: addCourseRef(course[, level=100,[ core_or_elective=True]]) |
---|
[4313] | 166 | |
---|
[4322] | 167 | Add a reference to a course. A course is an object implementing |
---|
| 168 | :class:`waeup.interfaces.ICourse`. |
---|
| 169 | |
---|
| 170 | Please don't be confused by the term 'reference'. This just |
---|
| 171 | means an ordinary :class:`waeup.university.course.Course` object |
---|
| 172 | in almost all cases. As this object will normaly be one stored |
---|
| 173 | in a department, the course here will simply become a reference |
---|
| 174 | to the 'real' one in the department container. |
---|
| 175 | |
---|
| 176 | .. method:: delCourseRef(code) |
---|
| 177 | |
---|
| 178 | Remove a course from a certificate. |
---|
| 179 | |
---|
| 180 | The course must be given by its code number. |
---|
| 181 | |
---|
| 182 | :class:`CertificateCourse` |
---|
| 183 | -------------------------- |
---|
| 184 | |
---|
| 185 | .. class:: CertificateCourse(course[, level=100[, core_or_elective=True]]) |
---|
| 186 | |
---|
| 187 | Create a certificate course. |
---|
| 188 | |
---|
| 189 | A certificate-course is a course (:class:`Course`) which is part of |
---|
| 190 | a certificate. Normally, certificate-courses are held in |
---|
| 191 | certificates and refer to an existing :class:`Course` instance held |
---|
| 192 | elsewhere. |
---|
| 193 | |
---|
| 194 | A certificate can require several courses and one |
---|
| 195 | course can be required by several certificates. |
---|
| 196 | |
---|
| 197 | .. attribute:: course |
---|
| 198 | |
---|
| 199 | An instance of :class:`ICourse`. |
---|
| 200 | |
---|
| 201 | .. attribute:: level |
---|
| 202 | |
---|
| 203 | An integer telling the level to which this course applies. |
---|
| 204 | |
---|
| 205 | .. attribute:: core_or_elective |
---|
| 206 | |
---|
| 207 | A bool stating whether this course is required or optional to |
---|
| 208 | get the certificate. |
---|
| 209 | |
---|
| 210 | |
---|
| 211 | |
---|
[4313] | 212 | Utilities |
---|
| 213 | ========= |
---|
| 214 | |
---|
| 215 | :class:`CertificateFactory` |
---|
| 216 | --------------------------- |
---|
| 217 | |
---|
| 218 | .. class:: CertificateFactory() |
---|
| 219 | |
---|
| 220 | .. attribute:: grok.name(u'waeup.Certificate') |
---|
| 221 | |
---|
| 222 | .. attribute:: grok.implements(IFactory) |
---|
| 223 | |
---|
| 224 | A named utility to deliver new instances of :class:`Certificate` |
---|
| 225 | without the need to import the implementation before: |
---|
| 226 | |
---|
| 227 | >>> from zope.component import createObject |
---|
| 228 | >>> mycertificate = createObject(u'waeup.Certificate') |
---|
| 229 | >>> mycertificate |
---|
| 230 | <waeup.university.certificate.Certificate object at 0x...> |
---|
| 231 | |
---|
[4384] | 232 | The factory complies with the specifications from the |
---|
| 233 | :class:`IFactory` insterface: |
---|
| 234 | |
---|
| 235 | >>> from zope.interface.verify import verifyClass |
---|
| 236 | >>> from zope.component.interfaces import IFactory |
---|
| 237 | >>> from waeup.university.certificate import CertificateFactory |
---|
| 238 | >>> verifyClass(IFactory, CertificateFactory) |
---|
| 239 | True |
---|
| 240 | |
---|
| 241 | This means also, that we can get the interfaces of the created |
---|
| 242 | object from the factory: |
---|
| 243 | |
---|
| 244 | >>> certificate_factory = CertificateFactory() |
---|
| 245 | >>> certificate_factory.getInterfaces() |
---|
| 246 | <implementedBy waeup.university.certificate.Certificate> |
---|
| 247 | |
---|
| 248 | |
---|
[4386] | 249 | :class:`CertificateCourseFactory` |
---|
| 250 | --------------------------------- |
---|
| 251 | |
---|
| 252 | .. class:: CertificateCourseFactory() |
---|
| 253 | |
---|
| 254 | .. attribute:: grok.name(u'waeup.CertificateCourse') |
---|
| 255 | |
---|
| 256 | .. attribute:: grok.implements(IFactory) |
---|
| 257 | |
---|
| 258 | A named utility to deliver new instances of :class:`CertificateCourse` |
---|
| 259 | without the need to import the implementation before: |
---|
| 260 | |
---|
| 261 | >>> from zope.component import createObject |
---|
| 262 | >>> mycertificatecourse = createObject(u'waeup.CertificateCourse') |
---|
| 263 | >>> mycertificatecourse |
---|
| 264 | <waeup.university.certificate.CertificateCourse object at 0x...> |
---|
| 265 | |
---|
| 266 | The factory complies with the specifications from the |
---|
| 267 | :class:`IFactory` insterface: |
---|
| 268 | |
---|
| 269 | >>> from zope.interface.verify import verifyClass |
---|
| 270 | >>> from zope.component.interfaces import IFactory |
---|
| 271 | >>> from waeup.university.certificate import CertificateCourseFactory |
---|
| 272 | >>> verifyClass(IFactory, CertificateCourseFactory) |
---|
| 273 | True |
---|
| 274 | |
---|
| 275 | This means also, that we can get the interfaces of the created |
---|
| 276 | object from the factory: |
---|
| 277 | |
---|
| 278 | >>> certcourse_factory = CertificateCourseFactory() |
---|
| 279 | >>> certcourse_factory.getInterfaces() |
---|
| 280 | <implementedBy waeup.university.certificate.CertificateCourse> |
---|
| 281 | |
---|
| 282 | |
---|
[4332] | 283 | Event Subscribers |
---|
| 284 | ================= |
---|
| 285 | |
---|
| 286 | .. function:: removedCourseHandler(course, event) |
---|
| 287 | |
---|
| 288 | An event subscriber triggered for |
---|
| 289 | :class:`grok.IObjectRemovedEvent`s, when an :class:`ICourse` |
---|
| 290 | instance is removed from a container. |
---|
| 291 | |
---|
| 292 | Tries to remove all referencing :class:`CertificateCourse` |
---|
| 293 | instances that reference the removed course. |
---|
| 294 | |
---|
| 295 | To accomplish that, the parents of the removed course are looked up |
---|
| 296 | for a certifcate container which contains a certificate-course that |
---|
| 297 | contains a reference to the deleted course. |
---|
| 298 | |
---|
| 299 | .. seealso:: :ref:`removecertificatecourses` |
---|
| 300 | |
---|
| 301 | **handles:** |
---|
| 302 | :class:`ICourse` |
---|
| 303 | |
---|
| 304 | **event type:** |
---|
| 305 | :class:`grok.IObjectRemovedEvent` |
---|
| 306 | |
---|
[4313] | 307 | Examples |
---|
| 308 | ======== |
---|
| 309 | |
---|
[4322] | 310 | Certificates |
---|
| 311 | ------------ |
---|
[4313] | 312 | |
---|
| 313 | We can create certificates: |
---|
| 314 | |
---|
| 315 | >>> from waeup.university.certificate import Certificate |
---|
| 316 | >>> mycertificate = Certificate() |
---|
| 317 | >>> mycertificate |
---|
| 318 | <waeup.university.certificate.Certificate object at 0x...> |
---|
| 319 | |
---|
| 320 | Another way to create certificates is by asking for a factory called |
---|
| 321 | ``waeup.Certificate``. This way we can create a factory without |
---|
| 322 | importing a class: |
---|
| 323 | |
---|
| 324 | >>> from zope.component import createObject |
---|
| 325 | >>> mycertificate = createObject(u'waeup.Certificate') |
---|
| 326 | >>> mycertificate |
---|
| 327 | <waeup.university.certificate.Certificate object at 0x...> |
---|
| 328 | |
---|
[4322] | 329 | CertificateCourses |
---|
| 330 | ------------------ |
---|
| 331 | |
---|
| 332 | :class:`CertificateCourse` instances comply with the |
---|
| 333 | :class:`ICertificateCourse` interface: |
---|
| 334 | |
---|
| 335 | >>> from waeup.interfaces import ICertificateCourse |
---|
| 336 | >>> from waeup.university.certificate import CertificateCourse |
---|
| 337 | >>> mycertcourse = CertificateCourse(None, 200, False) |
---|
| 338 | >>> ICertificateCourse.providedBy(mycertcourse) |
---|
| 339 | True |
---|
| 340 | |
---|
| 341 | >>> from zope.interface.verify import verifyObject |
---|
| 342 | >>> verifyObject(ICertificateCourse, mycertcourse) |
---|
| 343 | True |
---|
| 344 | |
---|
[4332] | 345 | Also instances of :class:`CertificateCourse` can be created by asking |
---|
| 346 | the component architechture: |
---|
| 347 | |
---|
| 348 | >>> from zope.component import createObject |
---|
| 349 | >>> mycertcourse = createObject(u'waeup.CertificateCourse') |
---|
| 350 | >>> mycertcourse |
---|
| 351 | <waeup.university.certificate.CertificateCourse object at 0x...> |
---|
| 352 | |
---|
| 353 | .. _removecertificatecourses: |
---|
| 354 | |
---|
| 355 | Persistence of certificate courses |
---|
| 356 | ---------------------------------- |
---|
| 357 | |
---|
| 358 | If a certificate course requires a certain course and this is course |
---|
| 359 | is deleted, also the referencing certificate course is deleted. |
---|
| 360 | |
---|
| 361 | We setup a data structure that reflects typical usage. It looks like |
---|
| 362 | this:: |
---|
| 363 | |
---|
| 364 | Department-Instance |
---|
| 365 | | |
---|
| 366 | +---> courses |
---|
| 367 | | | |
---|
| 368 | | +--------------------> Course-Instance |
---|
| 369 | | ^ |
---|
| 370 | +---> certificates | |
---|
| 371 | | | |
---|
| 372 | +-----> Certificate-Instance | |
---|
| 373 | | | |
---|
| 374 | +------> Certificate-Course |
---|
| 375 | |
---|
| 376 | The certifcate-Course here refers to a Course-Instance. |
---|
| 377 | |
---|
| 378 | In Python we build such a structure like this (from top to bottom): |
---|
| 379 | |
---|
| 380 | >>> from zope.component import createObject |
---|
| 381 | >>> mydept = createObject('waeup.Department') |
---|
| 382 | |
---|
| 383 | In real world use this data will be stored in a ZODB. We setup our own |
---|
| 384 | litte ZODB backend (which is easy!): |
---|
| 385 | |
---|
| 386 | >>> from ZODB import FileStorage, DB |
---|
| 387 | >>> dbpath = 'tinyData.fs' |
---|
| 388 | >>> class TinyZODB(object): |
---|
| 389 | ... def __init__(self, path=dbpath): |
---|
| 390 | ... self.storage = FileStorage.FileStorage(path) |
---|
| 391 | ... self.db = DB(self.storage) |
---|
| 392 | ... self.connection = self.db.open() |
---|
| 393 | ... self.dbroot = self.connection.root() |
---|
| 394 | ... def close(self): |
---|
| 395 | ... self.connection.close() |
---|
| 396 | ... self.db.close() |
---|
| 397 | ... self.storage.close() |
---|
| 398 | |
---|
| 399 | Now we can use this ZODB as backend database and store our data |
---|
| 400 | structure: |
---|
| 401 | |
---|
| 402 | >>> import transaction |
---|
| 403 | >>> db = TinyZODB() |
---|
| 404 | >>> dbroot = db.dbroot |
---|
| 405 | >>> dbroot['mydept'] = mydept |
---|
| 406 | >>> mycourse = createObject('waeup.Course') |
---|
| 407 | >>> mycourse.code = 'MYCOURSE' |
---|
| 408 | >>> mydept.courses.addCourse(mycourse) |
---|
| 409 | >>> mycert = createObject('waeup.Certificate') |
---|
| 410 | >>> mycert.code = 'MYCERT' |
---|
| 411 | >>> mydept.certificates.addCertificate(mycert) |
---|
| 412 | >>> mycert.addCourseRef(mycourse) |
---|
| 413 | |
---|
| 414 | >>> transaction.commit() |
---|
| 415 | |
---|
| 416 | The data is now stored in the ZODB. We can close the DB, reopen it |
---|
| 417 | later and the data will still be there: |
---|
| 418 | |
---|
| 419 | >>> db.close() |
---|
| 420 | >>> newdb = TinyZODB() |
---|
| 421 | >>> newdbroot = newdb.dbroot |
---|
| 422 | >>> list(newdbroot) |
---|
| 423 | ['mydept'] |
---|
| 424 | |
---|
| 425 | The certificate-course we stored in the certificate is indeed a |
---|
| 426 | reference to the course, not a copy of it: |
---|
| 427 | |
---|
| 428 | >>> course = newdbroot['mydept'].courses['MYCOURSE'] |
---|
[4489] | 429 | >>> certcourse = newdbroot['mydept'].certificates['MYCERT']['MYCOURSE_100'] |
---|
[4332] | 430 | >>> certcourse.course is course |
---|
| 431 | True |
---|
| 432 | |
---|
| 433 | So, we can be sure that modifications to the course are immediately |
---|
| 434 | reflected in the certcourse. |
---|
| 435 | |
---|