[7191] | 1 | ## $Id: container.py 13165 2015-07-13 07:53:26Z henrik $ |
---|
| 2 | ## |
---|
[6621] | 3 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
| 4 | ## This program is free software; you can redistribute it and/or modify |
---|
| 5 | ## it under the terms of the GNU General Public License as published by |
---|
| 6 | ## the Free Software Foundation; either version 2 of the License, or |
---|
| 7 | ## (at your option) any later version. |
---|
| 8 | ## |
---|
| 9 | ## This program is distributed in the hope that it will be useful, |
---|
| 10 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 11 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 12 | ## GNU General Public License for more details. |
---|
| 13 | ## |
---|
| 14 | ## You should have received a copy of the GNU General Public License |
---|
| 15 | ## along with this program; if not, write to the Free Software |
---|
| 16 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 17 | ## |
---|
| 18 | """ |
---|
| 19 | Containers for students. |
---|
| 20 | """ |
---|
| 21 | import grok |
---|
[8410] | 22 | from thread import allocate_lock |
---|
| 23 | from transaction import commit |
---|
| 24 | from zope.component import getUtility |
---|
[7811] | 25 | from waeup.kofa.students.interfaces import ( |
---|
[8410] | 26 | IStudentsContainer, IStudent, IStudentsUtils) |
---|
| 27 | from waeup.kofa.utils.helpers import attrs_to_fields |
---|
[7811] | 28 | from waeup.kofa.utils.logger import Logger |
---|
[6621] | 29 | |
---|
[8410] | 30 | lock = allocate_lock() # a lock object to lock threads. |
---|
| 31 | |
---|
[6637] | 32 | class StudentsContainer(grok.Container, Logger): |
---|
[6621] | 33 | """ |
---|
| 34 | The node containing the student models |
---|
| 35 | """ |
---|
| 36 | |
---|
| 37 | grok.implements(IStudentsContainer) |
---|
| 38 | |
---|
[8410] | 39 | _curr_stud_id = 10 ** 6 |
---|
| 40 | |
---|
[8737] | 41 | logger_name = 'waeup.kofa.${sitename}.students' |
---|
| 42 | logger_filename = 'students.log' |
---|
| 43 | |
---|
[8410] | 44 | @property |
---|
| 45 | def unique_student_id(self): |
---|
| 46 | """A unique student id. |
---|
| 47 | |
---|
| 48 | The student id returned is guaranteed to be unique. It |
---|
| 49 | consists of some prefix (normally a single letter) followed by |
---|
| 50 | a number with at least 7 digits. |
---|
| 51 | |
---|
| 52 | Once a student id was issued, it won't be issued again. |
---|
| 53 | |
---|
| 54 | Obtaining a student id is currently not thread-safe but can be |
---|
| 55 | made easily by enabling commented lines. |
---|
| 56 | """ |
---|
| 57 | prefix = getUtility(IStudentsUtils).STUDENT_ID_PREFIX |
---|
| 58 | |
---|
| 59 | # lock.acquire() # lock data |
---|
| 60 | new_id = u'%s%s' % (prefix, self._curr_stud_id) |
---|
| 61 | self._curr_stud_id += 1 |
---|
| 62 | # self._p_changed = True |
---|
| 63 | # commit() |
---|
| 64 | # lock.release() # end of lock |
---|
| 65 | return new_id |
---|
| 66 | |
---|
[6633] | 67 | def addStudent(self, student): |
---|
| 68 | """Add a student with subcontainers. |
---|
| 69 | """ |
---|
| 70 | if not IStudent.providedBy(student): |
---|
[6663] | 71 | raise TypeError( |
---|
| 72 | 'StudentsContainers contain only IStudent instances') |
---|
[6633] | 73 | self[student.student_id] = student |
---|
[6845] | 74 | return |
---|
[6637] | 75 | |
---|
[8410] | 76 | StudentsContainer = attrs_to_fields(StudentsContainer) |
---|