Ignore:
Timestamp:
25 Oct 2012, 09:44:02 (12 years ago)
Author:
Henrik Bettermann
Message:

Reorganize allocation of students to beds. We can't use the StudentSource? in live systems. The select box would be filled with ten thousands of students.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/src/waeup/kofa/hostels/interfaces.py

    r9400 r9414  
    1616## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
    1717##
     18from  grok import getSite
    1819from datetime import datetime
     20from zope.component import getUtility
     21from zope.catalog.interfaces import ICatalog
    1922from zope.interface import invariant, Invalid, Attribute
    2023from zope import schema
     
    2326from waeup.kofa.interfaces import MessageFactory as _
    2427from waeup.kofa.hostels.vocabularies import (
    25     bed_letters, blocks, SpecialHandlingSource, StudentSource)
     28    bed_letters, blocks, SpecialHandlingSource,
     29    NOT_OCCUPIED)
    2630
    2731class IHostelsContainer(IKofaObject):
     
    236240        )
    237241
    238 
    239 
    240 class IBedAllocateStudent(IBed):
    241     """A representation of beds for allocation form only.
    242 
    243     """
    244 
    245     owner = schema.Choice(
    246         title = _(u'Owner (Student)'),
    247         source = StudentSource(),
    248         default = None,
    249         required = True,
    250         )
     242    @invariant
     243    def allowed_owners(bed):
     244        if bed.owner == NOT_OCCUPIED:
     245            return
     246        catalog = getUtility(ICatalog, name='students_catalog')
     247        accommodation_session = getSite()['hostels'].accommodation_session
     248        students = catalog.searchResults(current_session=(
     249            accommodation_session,accommodation_session))
     250        student_ids = [student.student_id for student in students]
     251        if not bed.owner in student_ids:
     252            raise Invalid(_(
     253                "Either student does not exist or is not in accommodation session."))
     254        catalog = getUtility(ICatalog, name='beds_catalog')
     255        beds = catalog.searchResults(owner=(bed.owner,bed.owner))
     256        if len(beds):
     257            allocated_bed = [bed.bed_id for bed in beds][0]
     258            raise Invalid(_(
     259                "This student resides in bed ${a}.", mapping = {'a':allocated_bed}))
Note: See TracChangeset for help on using the changeset viewer.