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

Last change on this file since 8589 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
RevLine 
[7191]1## $Id: container.py 8410 2012-05-10 16:37:07Z uli $
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"""
19Containers for students.
20"""
21import grok
[8410]22from thread import allocate_lock
23from transaction import commit
24from zope.component import getUtility
[7811]25from waeup.kofa.students.interfaces import (
[8410]26    IStudentsContainer, IStudent, IStudentsUtils)
27from waeup.kofa.utils.helpers import attrs_to_fields
[7811]28from waeup.kofa.utils.logger import Logger
[6621]29
[8410]30lock = allocate_lock() # a lock object to lock threads.
31
[6637]32class 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
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
[6621]64    def archive(self, id=None):
65        raise NotImplementedError()
66
67    def clear(self, id=None, archive=True):
68        raise NotImplementedError()
69
[6633]70    def addStudent(self, student):
71        """Add a student with subcontainers.
72        """
73        if not IStudent.providedBy(student):
[6663]74            raise TypeError(
75                'StudentsContainers contain only IStudent instances')
[6633]76        self[student.student_id] = student
[6845]77        return
[6637]78
[7811]79    logger_name = 'waeup.kofa.${sitename}.students'
[6637]80    logger_filename = 'students.log'
81
[6644]82    def logger_info(self, ob_class, target, comment=None):
[6637]83        """Get the logger's info method.
84        """
[7652]85        self.logger.info('%s - %s - %s' % (
86                ob_class, target, comment))
[6637]87        return
[8410]88
89StudentsContainer = attrs_to_fields(StudentsContainer)
Note: See TracBrowser for help on using the repository browser.