Changeset 9414


Ignore:
Timestamp:
25 Oct 2012, 09:44:02 (14 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.

Location:
main/waeup.kofa/trunk/src/waeup/kofa/hostels
Files:
5 edited

Legend:

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

    r9217 r9414  
    3737from waeup.kofa.hostels.hostel import Hostel
    3838from waeup.kofa.hostels.interfaces import (
    39     IHostelsContainer, IHostel, IBed, IBedAllocateStudent)
     39    IHostelsContainer, IHostel, IBed)
    4040from waeup.kofa.widgets.datewidget import FriendlyDatetimeDisplayWidget
    4141
    … …  
    350350    """ View to edit bed data
    351351    """
    352     grok.context(IBedAllocateStudent)
     352    grok.context(IBed)
    353353    grok.name('index')
    354354    grok.require('waeup.manageHostels')
    355     form_fields = grok.AutoFields(IBedAllocateStudent).omit(
     355    form_fields = grok.AutoFields(IBed).omit(
    356356        'bed_id', 'bed_number', 'bed_type')
    357357    label = _('Allocate student')
  • main/waeup.kofa/trunk/src/waeup/kofa/hostels/hostel.py

    r9202 r9414  
    2626from waeup.kofa.utils.helpers import attrs_to_fields
    2727from waeup.kofa.hostels.vocabularies import NOT_OCCUPIED
    28 from waeup.kofa.hostels.interfaces import IHostel, IBed, IBedAllocateStudent
     28from waeup.kofa.hostels.interfaces import IHostel, IBed
    2929from waeup.kofa.students.interfaces import IBedTicket
    3030from waeup.kofa.interfaces import IKofaUtils
    … …  
    143143    """This is a bed.
    144144    """
    145     grok.implements(IBed, IBedAllocateStudent)
     145    grok.implements(IBed)
    146146    grok.provides(IBed)
    147147
  • 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}))
  • main/waeup.kofa/trunk/src/waeup/kofa/hostels/tests.py

    r9283 r9414  
    313313        self.assertMatches(
    314314          '...No allocated bed selected...', self.browser.contents)
    315         # Managers can manually allocate students after cancellation
     315        # Managers can manually allocate eligible students after cancellation
    316316        self.browser.open(self.container_path + '/hall-1/hall-1_A_101_A')
    317         self.browser.getControl(name="form.owner").value = [self.student_id]
     317        self.browser.getControl(name="form.owner").value = 'nonsense'
     318        self.browser.getControl("Save").click()
     319        self.assertMatches(
     320            "...Either student does not exist or is not in accommodation session...",
     321            self.browser.contents)
     322        self.browser.getControl(name="form.owner").value = self.student_id
    318323        self.browser.getControl("Save").click()
    319324        self.assertMatches("...Form has been saved...", self.browser.contents)
     325        # Students can only be allocated once
     326        self.browser.open(self.container_path + '/hall-1/hall-1_A_101_B')
     327        self.browser.getControl(name="form.owner").value = self.student_id
     328        self.browser.getControl("Save").click()
     329        self.assertMatches(
     330            "...This student resides in bed hall-1_A_101_A...",
     331            self.browser.contents)
    320332        # If we open the same form again, we will be redirected to hostel
    321333        # manage page. Beds must be released first before they can be
    … …  
    347359        # Also the number of the bed has changed.
    348360        self.assertFalse(new_number == old_number)
    349         # The number of occupied bed are displayed on container page.
     361        # The number of occupied beds are displayed on container page.
    350362        self.browser.open(self.container_path)
    351363        self.assertTrue('1 of 8' in self.browser.contents)
  • main/waeup.kofa/trunk/src/waeup/kofa/hostels/vocabularies.py

    r9400 r9414  
    2727
    2828NOT_OCCUPIED = u'not occupied'
    29 
    30 class StudentSource(BasicContextualSourceFactory):
    31     """A students source delivers all students in accommodation session.
    32     """
    33 
    34     def acco_students(self, context):
    35         catalog = getUtility(ICatalog, name='students_catalog')
    36         accommodation_session = getSite()['hostels'].accommodation_session
    37         students = catalog.searchResults(current_session=(
    38             accommodation_session,accommodation_session))
    39         existing_students = [
    40             context.__parent__[key].owner
    41             for key in context.__parent__.keys()]
    42         students = [student for student in students
    43                       if not student.student_id in existing_students]
    44         students = sorted(list(students),
    45                           key=lambda value: value.student_id)
    46         return dict([(student.student_id,student.fullname) for student in students])
    47 
    48     def getValues(self, context):
    49         return self.acco_students(context).keys()
    50 
    51     def getToken(self, context, value):
    52         return value
    53 
    54     def getTitle(self, context, value):
    55         return "%s - %s" % (value, self.acco_students(context)[value])
    5629
    5730class SpecialHandlingSource(ContextualDictSourceFactoryBase):
Note: See TracChangeset for help on using the changeset viewer.