[7190] | 1 | ## $Id: accommodation.py 13314 2015-10-13 17:29:07Z 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 |
---|
[8182] | 23 | from zope.component import getUtility |
---|
[6635] | 24 | from zope.component.interfaces import IFactory |
---|
[7256] | 25 | from zope.interface import implementedBy |
---|
[8182] | 26 | from waeup.kofa.interfaces import academic_sessions_vocab, IKofaUtils |
---|
[7811] | 27 | from waeup.kofa.students.interfaces import ( |
---|
[9987] | 28 | IStudentAccommodation, IStudentNavigation, IBedTicket, IStudentsUtils) |
---|
[7811] | 29 | from waeup.kofa.utils.helpers import attrs_to_fields |
---|
[6635] | 30 | |
---|
| 31 | class StudentAccommodation(grok.Container): |
---|
[9423] | 32 | """This is a container for bed tickets. |
---|
[6635] | 33 | """ |
---|
[6642] | 34 | grok.implements(IStudentAccommodation, IStudentNavigation) |
---|
[6635] | 35 | grok.provides(IStudentAccommodation) |
---|
| 36 | |
---|
| 37 | def __init__(self): |
---|
| 38 | super(StudentAccommodation, self).__init__() |
---|
| 39 | return |
---|
| 40 | |
---|
[6989] | 41 | def addBedTicket(self, bedticket): |
---|
| 42 | """Add a bed ticket object. |
---|
| 43 | """ |
---|
| 44 | if not IBedTicket.providedBy(bedticket): |
---|
| 45 | raise TypeError( |
---|
| 46 | 'StudentAccommodation containers contain only IBedTicket instances') |
---|
[9423] | 47 | self[str(bedticket.booking_session)] = bedticket |
---|
[6989] | 48 | return |
---|
| 49 | |
---|
[8736] | 50 | @property |
---|
| 51 | def student(self): |
---|
[6642] | 52 | return self.__parent__ |
---|
| 53 | |
---|
[8735] | 54 | def writeLogMessage(self, view, message): |
---|
| 55 | return self.__parent__.writeLogMessage(view, message) |
---|
| 56 | |
---|
[6989] | 57 | StudentAccommodation = attrs_to_fields(StudentAccommodation) |
---|
| 58 | |
---|
| 59 | class BedTicket(grok.Model): |
---|
[9424] | 60 | """This is a bed ticket which shows that the student has booked a bed. |
---|
[6989] | 61 | """ |
---|
| 62 | grok.implements(IBedTicket, IStudentNavigation) |
---|
| 63 | grok.provides(IBedTicket) |
---|
| 64 | |
---|
| 65 | def __init__(self): |
---|
| 66 | super(BedTicket, self).__init__() |
---|
[8194] | 67 | self.booking_date = datetime.utcnow() |
---|
[6996] | 68 | self.bed = None |
---|
[6989] | 69 | return |
---|
| 70 | |
---|
[8736] | 71 | @property |
---|
| 72 | def student(self): |
---|
| 73 | try: |
---|
| 74 | return self.__parent__.__parent__ |
---|
| 75 | except AttributeError: |
---|
| 76 | return None |
---|
[6989] | 77 | |
---|
[9984] | 78 | @property |
---|
| 79 | def display_coordinates(self): |
---|
[9987] | 80 | students_utils = getUtility(IStudentsUtils) |
---|
| 81 | return students_utils.getBedCoordinates(self) |
---|
[9984] | 82 | |
---|
[13314] | 83 | @property |
---|
| 84 | def maint_payment_made(self): |
---|
| 85 | try: |
---|
| 86 | if len(self.student['payments']): |
---|
| 87 | for ticket in self.student['payments'].values(): |
---|
| 88 | if ticket.p_category == 'hostel_maintenance' and \ |
---|
| 89 | ticket.p_session == self.booking_session and \ |
---|
| 90 | ticket.p_state == 'paid': |
---|
| 91 | return True |
---|
| 92 | except TypeError: # in unit tests |
---|
| 93 | pass |
---|
| 94 | return False |
---|
| 95 | |
---|
[8735] | 96 | def writeLogMessage(self, view, message): |
---|
| 97 | return self.__parent__.__parent__.writeLogMessage(view, message) |
---|
| 98 | |
---|
[6994] | 99 | def getSessionString(self): |
---|
| 100 | return academic_sessions_vocab.getTerm( |
---|
| 101 | self.booking_session).title |
---|
| 102 | |
---|
[9984] | 103 | BedTicket = attrs_to_fields(BedTicket, omit=['display_coordinates']) |
---|
[6992] | 104 | |
---|
[9984] | 105 | |
---|
[6992] | 106 | # Bed tickets must be importable. So we might need a factory. |
---|
| 107 | class BedTicketFactory(grok.GlobalUtility): |
---|
| 108 | """A factory for bed tickets. |
---|
| 109 | """ |
---|
| 110 | grok.implements(IFactory) |
---|
| 111 | grok.name(u'waeup.BedTicket') |
---|
| 112 | title = u"Create a new bed ticket.", |
---|
| 113 | description = u"This factory instantiates new bed ticket instances." |
---|
| 114 | |
---|
| 115 | def __call__(self, *args, **kw): |
---|
| 116 | return BedTicket() |
---|
| 117 | |
---|
| 118 | def getInterfaces(self): |
---|
[7811] | 119 | return implementedBy(BedTicket) |
---|