source: main/waeup.kofa/trunk/src/waeup/kofa/students/accommodation.py @ 13606

Last change on this file since 13606 was 13457, checked in by Henrik Bettermann, 9 years ago

Add option which allows students to select a desired hostel before booking accommodation.

  • Property svn:keywords set to Id
File size: 7.1 KB
RevLine 
[7190]1## $Id: accommodation.py 13457 2015-11-16 09:05:30Z henrik $
2##
[6635]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"""
[7599]19Student accommodation components.
[6635]20"""
[6992]21from datetime import datetime
[6635]22import grok
[13455]23from zope.component import getUtility, queryUtility
[6635]24from zope.component.interfaces import IFactory
[13455]25from zope.event import notify
26from zope.i18n import translate
27from zope.catalog.interfaces import ICatalog
[7256]28from zope.interface import implementedBy
[8182]29from waeup.kofa.interfaces import academic_sessions_vocab, IKofaUtils
[13455]30from waeup.kofa.interfaces import MessageFactory as _
[7811]31from waeup.kofa.students.interfaces import (
[9987]32    IStudentAccommodation, IStudentNavigation, IBedTicket, IStudentsUtils)
[7811]33from waeup.kofa.utils.helpers import attrs_to_fields
[13455]34from waeup.kofa.hostels.hostel import NOT_OCCUPIED
[6635]35
[13455]36
[6635]37class StudentAccommodation(grok.Container):
[9423]38    """This is a container for bed tickets.
[6635]39    """
[6642]40    grok.implements(IStudentAccommodation, IStudentNavigation)
[6635]41    grok.provides(IStudentAccommodation)
42
43    def __init__(self):
44        super(StudentAccommodation, self).__init__()
45        return
46
[6989]47    def addBedTicket(self, bedticket):
48        """Add a bed ticket object.
49        """
50        if not IBedTicket.providedBy(bedticket):
51            raise TypeError(
52                'StudentAccommodation containers contain only IBedTicket instances')
[9423]53        self[str(bedticket.booking_session)] = bedticket
[6989]54        return
55
[8736]56    @property
57    def student(self):
[6642]58        return self.__parent__
59
[8735]60    def writeLogMessage(self, view, message):
61        return self.__parent__.writeLogMessage(view, message)
62
[6989]63StudentAccommodation = attrs_to_fields(StudentAccommodation)
64
65class BedTicket(grok.Model):
[9424]66    """This is a bed ticket which shows that the student has booked a bed.
[6989]67    """
68    grok.implements(IBedTicket, IStudentNavigation)
69    grok.provides(IBedTicket)
70
71    def __init__(self):
72        super(BedTicket, self).__init__()
[8194]73        self.booking_date = datetime.utcnow()
[6996]74        self.bed = None
[6989]75        return
76
[8736]77    @property
78    def student(self):
79        try:
80            return self.__parent__.__parent__
81        except AttributeError:
82            return None
[6989]83
[9984]84    @property
85    def display_coordinates(self):
[9987]86        students_utils = getUtility(IStudentsUtils)
87        return students_utils.getBedCoordinates(self)
[9984]88
[13314]89    @property
90    def maint_payment_made(self):
91        try:
92            if len(self.student['payments']):
93                for ticket in self.student['payments'].values():
94                    if ticket.p_category == 'hostel_maintenance' and \
95                        ticket.p_session == self.booking_session and \
96                        ticket.p_state == 'paid':
97                            return True
98        except TypeError: # in unit tests
99            pass
100        return False
101
[13455]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:
[13457]135                new_bed = students_utils.selectBed(
136                    available_beds, self.__parent__.desired_hostel)
137                if new_bed is None:
138                    return False, _(
139                        'There is no free bed in your desired hostel.')
[13455]140                new_bed.bookBed(student.student_id)
141            else:
142                return False, _('There is no free bed in your category ${a}.',
143                                 mapping = {'a':acc_details['bt']})
144        # Release old bed if exists
145        if self.bed != None:
146            self.bed.owner = NOT_OCCUPIED
147            notify(grok.ObjectModifiedEvent(self.bed))
148        # Designate new bed in ticket
149        self.bed_type = acc_details['bt']
150        self.bed = new_bed
151        hall_title = new_bed.__parent__.hostel_name
152        coordinates = new_bed.coordinates[1:]
153        block, room_nr, bed_nr = coordinates
154        bc = _('${a}, Block ${b}, Room ${c}, Bed ${d} (${e})', mapping = {
155            'a':hall_title, 'b':block,
156            'c':room_nr, 'd':bed_nr,
157            'e':new_bed.bed_type})
158        portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
159        self.bed_coordinates = translate(
160            bc, 'waeup.kofa',target_language=portal_language)
161        self.writeLogMessage(self, 'relocated: %s' % new_bed.bed_id)
162        return True, _('Student relocated: ${a}',
163                 mapping = {'a':self.display_coordinates})
164
[8735]165    def writeLogMessage(self, view, message):
166        return self.__parent__.__parent__.writeLogMessage(view, message)
167
[6994]168    def getSessionString(self):
169        return academic_sessions_vocab.getTerm(
170            self.booking_session).title
171
[9984]172BedTicket = attrs_to_fields(BedTicket, omit=['display_coordinates'])
[6992]173
[9984]174
[6992]175# Bed tickets must be importable. So we might need a factory.
176class BedTicketFactory(grok.GlobalUtility):
177    """A factory for bed tickets.
178    """
179    grok.implements(IFactory)
180    grok.name(u'waeup.BedTicket')
181    title = u"Create a new bed ticket.",
182    description = u"This factory instantiates new bed ticket instances."
183
184    def __call__(self, *args, **kw):
185        return BedTicket()
186
187    def getInterfaces(self):
[7811]188        return implementedBy(BedTicket)
Note: See TracBrowser for help on using the repository browser.