Ignore:
Timestamp:
10 May 2012, 16:37:07 (12 years ago)
Author:
uli
Message:

Create unique stud ids in the students container.

Location:
main/waeup.kofa/trunk/src/waeup/kofa/students
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/src/waeup/kofa/students/container.py

    r7811 r8410  
    2020"""
    2121import grok
     22from thread import allocate_lock
     23from transaction import commit
     24from zope.component import getUtility
    2225from waeup.kofa.students.interfaces import (
    23     IStudentsContainer, IStudent)
     26    IStudentsContainer, IStudent, IStudentsUtils)
     27from waeup.kofa.utils.helpers import attrs_to_fields
    2428from waeup.kofa.utils.logger import Logger
     29
     30lock = allocate_lock() # a lock object to lock threads.
    2531
    2632class StudentsContainer(grok.Container, Logger):
     
    3036
    3137    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
    3263
    3364    def archive(self, id=None):
     
    5586                ob_class, target, comment))
    5687        return
     88
     89StudentsContainer = attrs_to_fields(StudentsContainer)
  • main/waeup.kofa/trunk/src/waeup/kofa/students/utils.py

    r8307 r8410  
    9595    return tag1 + u'%s</font>' % text
    9696
    97 def generate_student_id(students,letter):
    98     if letter == '?':
    99         letter= r().choice('ABCDEFGHKLMNPQRSTUVWXY')
    100     sid = u"%c%d" % (letter,r().randint(99999,1000000))
    101     while sid in students.keys():
    102         sid = u"%c%d" % (letter,r().randint(99999,1000000))
    103     return sid
     97def generate_student_id(students, letter='?'):
     98    students = grok.getSite()['students']
     99    new_id = students.unique_student_id
     100    return new_id
    104101
    105102def set_up_widgets(view, ignore_request=False):
     
    393390    SEPARATORS_DICT = {
    394391        }
     392
     393    #: A prefix used when generating new student ids. Each student id will
     394    #: start with this string. The default is 'K' for ``Kofa``.
     395    STUDENT_ID_PREFIX = u'K'
Note: See TracChangeset for help on using the changeset viewer.