[5978] | 1 | Course referrers |
---|
| 2 | **************** |
---|
[4488] | 3 | |
---|
[5978] | 4 | Courses are referred to by certificate courses. |
---|
[4488] | 5 | |
---|
| 6 | :Test-Layer: functional |
---|
| 7 | |
---|
[5978] | 8 | We can be sure, that when removing courses or referrers of them, |
---|
[4488] | 9 | other data will be handled in a way we expect. That is, |
---|
| 10 | |
---|
[5978] | 11 | * when we remove a course, we want all referrers to it be removed as |
---|
[4488] | 12 | well |
---|
| 13 | |
---|
[5978] | 14 | * when we remove a referrer of a course, nothing else should be |
---|
| 15 | touched but only this very referrer. |
---|
[4488] | 16 | |
---|
| 17 | |
---|
[5978] | 18 | As correct deletion of referrer needs support of catalogs (we lookup |
---|
| 19 | catalogs to find referrers), we need a fully blown ZODB here, |
---|
[4921] | 20 | including an instance of :class:`waeup.sirp.app.University`, as this is our |
---|
[4488] | 21 | `ISite` object where the catalogs are stored. |
---|
| 22 | |
---|
| 23 | We create a complete university tree, starting with a |
---|
| 24 | :class:`University` instance and going down to courses, certificats |
---|
| 25 | and course certificates: |
---|
| 26 | |
---|
[4921] | 27 | >>> from waeup.sirp.app import University |
---|
[4488] | 28 | >>> root = getRootFolder() |
---|
| 29 | >>> root['app'] = University() |
---|
| 30 | |
---|
| 31 | We have to set the site, to enable proper working of catalogs and |
---|
| 32 | `IObjectRemoved` handlers: |
---|
| 33 | |
---|
| 34 | >>> from zope.app.component.hooks import setSite |
---|
| 35 | >>> setSite(root['app']) |
---|
| 36 | |
---|
| 37 | >>> from zope.component import createObject |
---|
| 38 | >>> faculty = createObject('waeup.Faculty') |
---|
| 39 | >>> faculty.code = 'FAC' |
---|
[4749] | 40 | >>> root['app']['faculties'].addFaculty(faculty) |
---|
| 41 | >>> fac = root['app']['faculties']['FAC'] |
---|
[4488] | 42 | |
---|
| 43 | >>> dept = createObject('waeup.Department') |
---|
| 44 | >>> dept.code = 'DEPT' |
---|
[4749] | 45 | >>> root['app']['faculties']['FAC'].addDepartment(dept) |
---|
[4488] | 46 | |
---|
| 47 | >>> course = createObject('waeup.Course') |
---|
| 48 | >>> course.code = 'CRS1' |
---|
| 49 | >>> fac['DEPT'].courses.addCourse(course) |
---|
| 50 | |
---|
| 51 | Now let's add another department, which will also hold a course: |
---|
| 52 | |
---|
| 53 | >>> dept2 = createObject('waeup.Department') |
---|
| 54 | >>> dept2.code = 'DEPT2' |
---|
[4749] | 55 | >>> root['app']['faculties']['FAC'].addDepartment(dept2) |
---|
[4488] | 56 | |
---|
| 57 | >>> course = createObject('waeup.Course') |
---|
| 58 | >>> course.code = 'CRS2' |
---|
| 59 | >>> fac['DEPT2'].courses.addCourse(course) |
---|
| 60 | |
---|
[5978] | 61 | Finally, we add a certificate with referrers (certificate courses) to |
---|
[4488] | 62 | the created courses: |
---|
| 63 | |
---|
| 64 | >>> cert = createObject('waeup.Certificate') |
---|
| 65 | >>> cert.code = 'CERT' |
---|
| 66 | >>> fac['DEPT'].certificates.addCertificate(cert) |
---|
| 67 | |
---|
| 68 | >>> fac['DEPT'].certificates['CERT'].addCourseRef( |
---|
| 69 | ... fac['DEPT'].courses['CRS1']) |
---|
| 70 | |
---|
| 71 | >>> fac['DEPT'].certificates['CERT'].addCourseRef( |
---|
| 72 | ... fac['DEPT2'].courses['CRS2']) |
---|
| 73 | |
---|
| 74 | We add the latter course again, but this time for the second level: |
---|
| 75 | |
---|
| 76 | >>> fac['DEPT'].certificates['CERT'].addCourseRef( |
---|
| 77 | ... fac['DEPT2'].courses['CRS2'], level=200) |
---|
| 78 | |
---|
[5978] | 79 | So, we have three referrers in our certificate of two different |
---|
[4488] | 80 | courses stored somewhere in the application: |
---|
| 81 | |
---|
| 82 | >>> list(fac['DEPT'].certificates['CERT']) |
---|
| 83 | [u'CRS1_100', u'CRS2_100', u'CRS2_200'] |
---|
| 84 | |
---|
[5978] | 85 | Both, the courses themselves and the referrers of them, are the same |
---|
[4488] | 86 | (not only equal): |
---|
| 87 | |
---|
[4749] | 88 | >>> a = root['app']['faculties']['FAC']['DEPT'] |
---|
[4488] | 89 | >>> b1 = a.courses['CRS1'] |
---|
| 90 | >>> b2 = a.certificates['CERT']['CRS1_100'].course |
---|
| 91 | >>> b1 is b2 |
---|
| 92 | True |
---|
| 93 | |
---|
[5978] | 94 | If we remove the course, also the select item in the certificate course |
---|
[4488] | 95 | will be gone: |
---|
| 96 | |
---|
| 97 | >>> list(a.certificates['CERT']) |
---|
| 98 | [u'CRS1_100', u'CRS2_100', u'CRS2_200'] |
---|
| 99 | |
---|
| 100 | >>> list(a.courses) |
---|
| 101 | [u'CRS1'] |
---|
| 102 | |
---|
[4749] | 103 | >>> del root['app']['faculties']['FAC']['DEPT'].courses['CRS1'] |
---|
[4488] | 104 | >>> list(a.certificates['CERT']) |
---|
| 105 | [u'CRS2_100', u'CRS2_200'] |
---|
| 106 | |
---|
| 107 | >>> list(a.courses) |
---|
| 108 | [] |
---|
| 109 | |
---|
[5978] | 110 | If we remove a referrer of a course, this will not touch the course |
---|
| 111 | nor other referrers: |
---|
[4488] | 112 | |
---|
| 113 | >>> del fac['DEPT'].certificates['CERT']['CRS2_200'] |
---|
| 114 | >>> list(a.certificates['CERT']) |
---|
| 115 | [u'CRS2_100'] |
---|
| 116 | |
---|
| 117 | >>> list(fac['DEPT2'].courses) |
---|
| 118 | [u'CRS2'] |
---|