Changeset 13455


Ignore:
Timestamp:
14 Nov 2015, 07:55:17 (9 years ago)
Author:
Henrik Bettermann
Message:

Move student relocation code to BedTicket?.relocateStudent method.

Location:
main/waeup.kofa/trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/docs/source/userdocs/students/browser.rst

    r13170 r13455  
    361361------------------
    362362
    363 Officers with `ManageHostels` permission do see a 'Relocate student'
    364 link button which calls the `BedTicketRelocationView`. This view
    365 relocates the student if student parameters or the bed type of the
    366 bed have changed. The `update` method of this view checks first, if
    367 the student has a 'reserved' bed space. Students in reserved beds
    368 are never subject to relocation. It checks secondly, if booking has
    369 been cancelled in the accommodation section but other bed space has
    370 been manually allocated after cancellation. Then this bed is used,
    371 no matter whether the bed meets the bed type criteria or not. If
    372 both checks are negative, Kofa searches for a free bed space, which
    373 meets the student's bed type criteria. Only if it finds a new and
    374 free bed space, it starts the relocation process by releasing the
    375 old bed, booking the new bed and designating the new bed in the bed
    376 ticket.
     363Officers with `ManageHostels` permission do see a 'Relocate student' link
     364button which calls the `BedTicketRelocationView`. This view relocates the
     365student if student parameters or the bed type of the bed have changed. The
     366`update` method of this view calls the
     367:py:meth:`BedTicket.relocateStudent<waeup.kofa.students.accommodation.BedTicket.relocateStudent>`
     368method which checks first, if the student has a 'reserved' bed space.
     369Students in reserved beds are never subject to relocation. It checks secondly,
     370if booking has been cancelled in the accommodation section but other bed space
     371has been manually allocated after cancellation. Then this bed is used, no
     372matter whether the bed meets the bed type criteria or not. If both checks are
     373negative, Kofa searches for a free bed space, which meets the student's bed
     374type criteria. Only if it finds a new and free bed space, it starts the
     375relocation process by releasing the old bed, booking the new bed and
     376designating the new bed in the bed ticket.
    377377
    378378.. seealso::
  • main/waeup.kofa/trunk/src/waeup/kofa/students/accommodation.py

    r13314 r13455  
    2121from datetime import datetime
    2222import grok
    23 from zope.component import getUtility
     23from zope.component import getUtility, queryUtility
    2424from zope.component.interfaces import IFactory
     25from zope.event import notify
     26from zope.i18n import translate
     27from zope.catalog.interfaces import ICatalog
    2528from zope.interface import implementedBy
    2629from waeup.kofa.interfaces import academic_sessions_vocab, IKofaUtils
     30from waeup.kofa.interfaces import MessageFactory as _
    2731from waeup.kofa.students.interfaces import (
    2832    IStudentAccommodation, IStudentNavigation, IBedTicket, IStudentsUtils)
    2933from waeup.kofa.utils.helpers import attrs_to_fields
     34from waeup.kofa.hostels.hostel import NOT_OCCUPIED
     35
    3036
    3137class StudentAccommodation(grok.Container):
     
    94100        return False
    95101
     102    def relocateStudent(self):
     103        """Relocate student if student parameters have changed or the bed_type
     104        of the bed has changed.
     105        """
     106        if self.booking_session != grok.getSite()[
     107            'hostels'].accommodation_session:
     108            return False, _("Previous session bookings can't be changed.")
     109        student = self.student
     110        students_utils = getUtility(IStudentsUtils)
     111        acc_details  = students_utils.getAccommodationDetails(student)
     112        if self.bed != None and \
     113              'reserved' in self.bed.bed_type:
     114            return False, _("Students in reserved beds can't be relocated.")
     115        if acc_details['bt'] == self.bed_type and \
     116                self.bed != None and \
     117                self.bed.bed_type == self.bed_type and \
     118                self.bed.__parent__.__parent__:
     119            return False, _("Student can't be relocated.")
     120        # Search a bed
     121        cat = queryUtility(ICatalog, name='beds_catalog', default=None)
     122        entries = cat.searchResults(
     123            owner=(student.student_id,student.student_id))
     124        if len(entries) and self.bed == None:
     125            # If booking has been cancelled but other bed space has been
     126            # manually allocated after cancellation use this bed
     127            new_bed = [entry for entry in entries][0]
     128        else:
     129            # Search for other available beds
     130            entries = cat.searchResults(
     131                bed_type=(acc_details['bt'],acc_details['bt']))
     132            available_beds = [
     133                entry for entry in entries if entry.owner == NOT_OCCUPIED]
     134            if available_beds:
     135                new_bed = students_utils.selectBed(available_beds)
     136                new_bed.bookBed(student.student_id)
     137            else:
     138                return False, _('There is no free bed in your category ${a}.',
     139                                 mapping = {'a':acc_details['bt']})
     140        # Release old bed if exists
     141        if self.bed != None:
     142            self.bed.owner = NOT_OCCUPIED
     143            notify(grok.ObjectModifiedEvent(self.bed))
     144        # Designate new bed in ticket
     145        self.bed_type = acc_details['bt']
     146        self.bed = new_bed
     147        hall_title = new_bed.__parent__.hostel_name
     148        coordinates = new_bed.coordinates[1:]
     149        block, room_nr, bed_nr = coordinates
     150        bc = _('${a}, Block ${b}, Room ${c}, Bed ${d} (${e})', mapping = {
     151            'a':hall_title, 'b':block,
     152            'c':room_nr, 'd':bed_nr,
     153            'e':new_bed.bed_type})
     154        portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
     155        self.bed_coordinates = translate(
     156            bc, 'waeup.kofa',target_language=portal_language)
     157        self.writeLogMessage(self, 'relocated: %s' % new_bed.bed_id)
     158        return True, _('Student relocated: ${a}',
     159                 mapping = {'a':self.display_coordinates})
     160
    96161    def writeLogMessage(self, view, message):
    97162        return self.__parent__.__parent__.writeLogMessage(view, message)
  • main/waeup.kofa/trunk/src/waeup/kofa/students/browser.py

    r13439 r13455  
    21592159    # of the bed has changed
    21602160    def update(self):
    2161         student = self.context.student
    2162         students_utils = getUtility(IStudentsUtils)
    2163         acc_details  = students_utils.getAccommodationDetails(student)
    2164         if self.context.bed != None and \
    2165               'reserved' in self.context.bed.bed_type:
    2166             self.flash(_("Students in reserved beds can't be relocated."),
    2167                        type="warning")
    2168             self.redirect(self.url(self.context))
    2169             return
    2170         if acc_details['bt'] == self.context.bed_type and \
    2171                 self.context.bed != None and \
    2172                 self.context.bed.bed_type == self.context.bed_type and \
    2173                 self.context.bed.__parent__.__parent__ :
    2174             self.flash(_("Student can't be relocated."), type="warning")
    2175             self.redirect(self.url(self.context))
    2176             return
    2177         # Search a bed
    2178         cat = queryUtility(ICatalog, name='beds_catalog', default=None)
    2179         entries = cat.searchResults(
    2180             owner=(student.student_id,student.student_id))
    2181         if len(entries) and self.context.bed == None:
    2182             # If booking has been cancelled but other bed space has been
    2183             # manually allocated after cancellation use this bed
    2184             new_bed = [entry for entry in entries][0]
    2185         else:
    2186             # Search for other available beds
    2187             entries = cat.searchResults(
    2188                 bed_type=(acc_details['bt'],acc_details['bt']))
    2189             available_beds = [
    2190                 entry for entry in entries if entry.owner == NOT_OCCUPIED]
    2191             if available_beds:
    2192                 students_utils = getUtility(IStudentsUtils)
    2193                 new_bed = students_utils.selectBed(available_beds)
    2194                 new_bed.bookBed(student.student_id)
    2195             else:
    2196                 self.flash(_('There is no free bed in your category ${a}.',
    2197                     mapping = {'a':acc_details['bt']}), type="warning")
    2198                 self.redirect(self.url(self.context))
    2199                 return
    2200         # Release old bed if exists
    2201         if self.context.bed != None:
    2202             self.context.bed.owner = NOT_OCCUPIED
    2203             notify(grok.ObjectModifiedEvent(self.context.bed))
    2204         # Designate new bed in ticket
    2205         self.context.bed_type = acc_details['bt']
    2206         self.context.bed = new_bed
    2207         hall_title = new_bed.__parent__.hostel_name
    2208         coordinates = new_bed.coordinates[1:]
    2209         block, room_nr, bed_nr = coordinates
    2210         bc = _('${a}, Block ${b}, Room ${c}, Bed ${d} (${e})', mapping = {
    2211             'a':hall_title, 'b':block,
    2212             'c':room_nr, 'd':bed_nr,
    2213             'e':new_bed.bed_type})
    2214         portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
    2215         self.context.bed_coordinates = translate(
    2216             bc, 'waeup.kofa',target_language=portal_language)
    2217         self.context.writeLogMessage(self, 'relocated: %s' % new_bed.bed_id)
    2218         self.flash(_('Student relocated: ${a}',
    2219             mapping = {'a':self.context.display_coordinates}))
     2161        success, msg = self.context.relocateStudent()
     2162        if not success:
     2163            self.flash(msg, type="warning")
     2164        else:
     2165            self.flash(msg)
    22202166        self.redirect(self.url(self.context))
    22212167        return
  • main/waeup.kofa/trunk/src/waeup/kofa/students/tests/test_browser.py

    r13431 r13455  
    12561256        # Relocation is properly logged
    12571257        logcontent = open(logfile).read()
    1258         self.assertTrue('zope.mgr - students.browser.BedTicketRelocationView '
     1258        self.assertTrue('zope.mgr - students.accommodation.BedTicket '
    12591259            '- K1000000 - relocated: hall-1_A_101_B' in logcontent)
    12601260        # The payment object still shows the original payment item
Note: See TracChangeset for help on using the changeset viewer.