[7190] | 1 | ## $Id: accommodation.py 15310 2019-01-28 13:29:48Z 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] | 19 | Student accommodation components. |
---|
[6635] | 20 | """ |
---|
[6992] | 21 | from datetime import datetime |
---|
[6635] | 22 | import grok |
---|
[13455] | 23 | from zope.component import getUtility, queryUtility |
---|
[6635] | 24 | from zope.component.interfaces import IFactory |
---|
[13455] | 25 | from zope.event import notify |
---|
| 26 | from zope.i18n import translate |
---|
| 27 | from zope.catalog.interfaces import ICatalog |
---|
[7256] | 28 | from zope.interface import implementedBy |
---|
[8182] | 29 | from waeup.kofa.interfaces import academic_sessions_vocab, IKofaUtils |
---|
[13455] | 30 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
[7811] | 31 | from waeup.kofa.students.interfaces import ( |
---|
[9987] | 32 | IStudentAccommodation, IStudentNavigation, IBedTicket, IStudentsUtils) |
---|
[7811] | 33 | from waeup.kofa.utils.helpers import attrs_to_fields |
---|
[13455] | 34 | from waeup.kofa.hostels.hostel import NOT_OCCUPIED |
---|
[6635] | 35 | |
---|
[13455] | 36 | |
---|
[6635] | 37 | class 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') |
---|
[15309] | 53 | try: |
---|
| 54 | self[str(bedticket.booking_session)] = bedticket |
---|
| 55 | except KeyError: |
---|
| 56 | if 'booking expired' in self[ |
---|
[15310] | 57 | str(bedticket.booking_session)].bed_coordinates: |
---|
[15309] | 58 | del self[str(bedticket.booking_session)] |
---|
[15310] | 59 | self[str(bedticket.booking_session)] = bedticket |
---|
[6989] | 60 | return |
---|
| 61 | |
---|
[8736] | 62 | @property |
---|
| 63 | def student(self): |
---|
[6642] | 64 | return self.__parent__ |
---|
| 65 | |
---|
[8735] | 66 | def writeLogMessage(self, view, message): |
---|
| 67 | return self.__parent__.writeLogMessage(view, message) |
---|
| 68 | |
---|
[6989] | 69 | StudentAccommodation = attrs_to_fields(StudentAccommodation) |
---|
| 70 | |
---|
| 71 | class BedTicket(grok.Model): |
---|
[9424] | 72 | """This is a bed ticket which shows that the student has booked a bed. |
---|
[6989] | 73 | """ |
---|
| 74 | grok.implements(IBedTicket, IStudentNavigation) |
---|
| 75 | grok.provides(IBedTicket) |
---|
| 76 | |
---|
| 77 | def __init__(self): |
---|
| 78 | super(BedTicket, self).__init__() |
---|
[8194] | 79 | self.booking_date = datetime.utcnow() |
---|
[6996] | 80 | self.bed = None |
---|
[6989] | 81 | return |
---|
| 82 | |
---|
[8736] | 83 | @property |
---|
| 84 | def student(self): |
---|
| 85 | try: |
---|
| 86 | return self.__parent__.__parent__ |
---|
| 87 | except AttributeError: |
---|
| 88 | return None |
---|
[6989] | 89 | |
---|
[9984] | 90 | @property |
---|
| 91 | def display_coordinates(self): |
---|
[9987] | 92 | students_utils = getUtility(IStudentsUtils) |
---|
| 93 | return students_utils.getBedCoordinates(self) |
---|
[9984] | 94 | |
---|
[13314] | 95 | @property |
---|
| 96 | def maint_payment_made(self): |
---|
| 97 | try: |
---|
| 98 | if len(self.student['payments']): |
---|
| 99 | for ticket in self.student['payments'].values(): |
---|
| 100 | if ticket.p_category == 'hostel_maintenance' and \ |
---|
| 101 | ticket.p_session == self.booking_session and \ |
---|
| 102 | ticket.p_state == 'paid': |
---|
| 103 | return True |
---|
| 104 | except TypeError: # in unit tests |
---|
| 105 | pass |
---|
| 106 | return False |
---|
| 107 | |
---|
[13455] | 108 | def relocateStudent(self): |
---|
| 109 | """Relocate student if student parameters have changed or the bed_type |
---|
| 110 | of the bed has changed. |
---|
| 111 | """ |
---|
| 112 | if self.booking_session != grok.getSite()[ |
---|
| 113 | 'hostels'].accommodation_session: |
---|
| 114 | return False, _("Previous session bookings can't be changed.") |
---|
| 115 | student = self.student |
---|
| 116 | students_utils = getUtility(IStudentsUtils) |
---|
| 117 | acc_details = students_utils.getAccommodationDetails(student) |
---|
| 118 | if self.bed != None and \ |
---|
| 119 | 'reserved' in self.bed.bed_type: |
---|
| 120 | return False, _("Students in reserved beds can't be relocated.") |
---|
| 121 | if acc_details['bt'] == self.bed_type and \ |
---|
| 122 | self.bed != None and \ |
---|
| 123 | self.bed.bed_type == self.bed_type and \ |
---|
| 124 | self.bed.__parent__.__parent__: |
---|
| 125 | return False, _("Student can't be relocated.") |
---|
| 126 | # Search a bed |
---|
| 127 | cat = queryUtility(ICatalog, name='beds_catalog', default=None) |
---|
| 128 | entries = cat.searchResults( |
---|
| 129 | owner=(student.student_id,student.student_id)) |
---|
| 130 | if len(entries) and self.bed == None: |
---|
| 131 | # If booking has been cancelled but other bed space has been |
---|
| 132 | # manually allocated after cancellation use this bed |
---|
| 133 | new_bed = [entry for entry in entries][0] |
---|
| 134 | else: |
---|
| 135 | # Search for other available beds |
---|
| 136 | entries = cat.searchResults( |
---|
| 137 | bed_type=(acc_details['bt'],acc_details['bt'])) |
---|
| 138 | available_beds = [ |
---|
| 139 | entry for entry in entries if entry.owner == NOT_OCCUPIED] |
---|
| 140 | if available_beds: |
---|
[13457] | 141 | new_bed = students_utils.selectBed( |
---|
| 142 | available_beds, self.__parent__.desired_hostel) |
---|
| 143 | if new_bed is None: |
---|
| 144 | return False, _( |
---|
| 145 | 'There is no free bed in your desired hostel.') |
---|
[13455] | 146 | new_bed.bookBed(student.student_id) |
---|
| 147 | else: |
---|
| 148 | return False, _('There is no free bed in your category ${a}.', |
---|
| 149 | mapping = {'a':acc_details['bt']}) |
---|
| 150 | # Release old bed if exists |
---|
| 151 | if self.bed != None: |
---|
| 152 | self.bed.owner = NOT_OCCUPIED |
---|
| 153 | notify(grok.ObjectModifiedEvent(self.bed)) |
---|
| 154 | # Designate new bed in ticket |
---|
| 155 | self.bed_type = acc_details['bt'] |
---|
| 156 | self.bed = new_bed |
---|
| 157 | hall_title = new_bed.__parent__.hostel_name |
---|
| 158 | coordinates = new_bed.coordinates[1:] |
---|
| 159 | block, room_nr, bed_nr = coordinates |
---|
| 160 | bc = _('${a}, Block ${b}, Room ${c}, Bed ${d} (${e})', mapping = { |
---|
| 161 | 'a':hall_title, 'b':block, |
---|
| 162 | 'c':room_nr, 'd':bed_nr, |
---|
| 163 | 'e':new_bed.bed_type}) |
---|
| 164 | portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE |
---|
| 165 | self.bed_coordinates = translate( |
---|
| 166 | bc, 'waeup.kofa',target_language=portal_language) |
---|
| 167 | self.writeLogMessage(self, 'relocated: %s' % new_bed.bed_id) |
---|
| 168 | return True, _('Student relocated: ${a}', |
---|
| 169 | mapping = {'a':self.display_coordinates}) |
---|
| 170 | |
---|
[8735] | 171 | def writeLogMessage(self, view, message): |
---|
| 172 | return self.__parent__.__parent__.writeLogMessage(view, message) |
---|
| 173 | |
---|
[6994] | 174 | def getSessionString(self): |
---|
| 175 | return academic_sessions_vocab.getTerm( |
---|
| 176 | self.booking_session).title |
---|
| 177 | |
---|
[9984] | 178 | BedTicket = attrs_to_fields(BedTicket, omit=['display_coordinates']) |
---|
[6992] | 179 | |
---|
[9984] | 180 | |
---|
[6992] | 181 | # Bed tickets must be importable. So we might need a factory. |
---|
| 182 | class BedTicketFactory(grok.GlobalUtility): |
---|
| 183 | """A factory for bed tickets. |
---|
| 184 | """ |
---|
| 185 | grok.implements(IFactory) |
---|
| 186 | grok.name(u'waeup.BedTicket') |
---|
| 187 | title = u"Create a new bed ticket.", |
---|
| 188 | description = u"This factory instantiates new bed ticket instances." |
---|
| 189 | |
---|
| 190 | def __call__(self, *args, **kw): |
---|
| 191 | return BedTicket() |
---|
| 192 | |
---|
| 193 | def getInterfaces(self): |
---|
[7811] | 194 | return implementedBy(BedTicket) |
---|