source: main/waeup.kofa/trunk/src/waeup/kofa/students/container.py @ 8515

Last change on this file since 8515 was 8410, checked in by uli, 13 years ago

Create unique stud ids in the students container.

  • Property svn:keywords set to Id
File size: 2.8 KB
Line 
1## $Id: container.py 8410 2012-05-10 16:37:07Z uli $
2##
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"""
19Containers for students.
20"""
21import grok
22from thread import allocate_lock
23from transaction import commit
24from zope.component import getUtility
25from waeup.kofa.students.interfaces import (
26    IStudentsContainer, IStudent, IStudentsUtils)
27from waeup.kofa.utils.helpers import attrs_to_fields
28from waeup.kofa.utils.logger import Logger
29
30lock = allocate_lock() # a lock object to lock threads.
31
32class StudentsContainer(grok.Container, Logger):
33    """
34    The node containing the student models
35    """
36
37    grok.implements(IStudentsContainer)
38
39    _curr_stud_id = 10 ** 6
40
41    @property
42    def unique_student_id(self):
43        """A unique student id.
44
45        The student id returned is guaranteed to be unique. It
46        consists of some prefix (normally a single letter) followed by
47        a number with at least 7 digits.
48
49        Once a student id was issued, it won't be issued again.
50
51        Obtaining a student id is currently not thread-safe but can be
52        made easily by enabling commented lines.
53        """
54        prefix = getUtility(IStudentsUtils).STUDENT_ID_PREFIX
55
56        # lock.acquire() # lock data
57        new_id = u'%s%s' % (prefix, self._curr_stud_id)
58        self._curr_stud_id += 1
59        # self._p_changed = True
60        # commit()
61        # lock.release() # end of lock
62        return new_id
63
64    def archive(self, id=None):
65        raise NotImplementedError()
66
67    def clear(self, id=None, archive=True):
68        raise NotImplementedError()
69
70    def addStudent(self, student):
71        """Add a student with subcontainers.
72        """
73        if not IStudent.providedBy(student):
74            raise TypeError(
75                'StudentsContainers contain only IStudent instances')
76        self[student.student_id] = student
77        return
78
79    logger_name = 'waeup.kofa.${sitename}.students'
80    logger_filename = 'students.log'
81
82    def logger_info(self, ob_class, target, comment=None):
83        """Get the logger's info method.
84        """
85        self.logger.info('%s - %s - %s' % (
86                ob_class, target, comment))
87        return
88
89StudentsContainer = attrs_to_fields(StudentsContainer)
Note: See TracBrowser for help on using the repository browser.