source: main/waeup.kofa/trunk/src/waeup/kofa/hostels/container.py @ 13440

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

Add page to release all expired allocations.

  • Property svn:keywords set to Id
File size: 3.0 KB
Line 
1## $Id: container.py 13319 2015-10-14 17:07:35Z henrik $
2##
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"""
19Containers which contain hostels.
20"""
21import grok
22import pytz
23from datetime import datetime
24from zope.catalog.interfaces import ICatalog
25from zope.component import queryUtility
26from waeup.kofa.hostels.interfaces import IHostelsContainer, IHostel
27from waeup.kofa.utils.logger import Logger
28from waeup.kofa.utils.helpers import attrs_to_fields
29
30class HostelsContainer(grok.Container, Logger):
31    """This is a container for all kind of hostels.
32    """
33    grok.implements(IHostelsContainer)
34    grok.provides(IHostelsContainer)
35
36    def __init__(self):
37        super(HostelsContainer, self).__init__()
38        return
39
40    def addHostel(self, hostel):
41        """Add a hostel.
42        """
43        if not IHostel.providedBy(hostel):
44            raise TypeError(
45                'HostelsContainers contain only IHostel instances')
46        self[hostel.hostel_id] = hostel
47        return
48
49    def clearAllHostels(self):
50        """Clear all hostels.
51        """
52        for hostel in self.values():
53            hostel.clearHostel()
54        return
55
56    def releaseExpiredAllocations(self, n=7):
57        """Release bed if bed allocation has expired. Allocation expires
58        after `n` days if maintenance fee has not been paid.
59        """
60        cat = queryUtility(ICatalog, name='beds_catalog')
61        results = cat.searchResults(owner=(None, None))
62        released = []
63        for bed in results:
64            student_id = bed.releaseBedIfMaintenanceNotPaid(n=n)
65            if student_id:
66                released.append('%s (%s)' % (bed.bed_id,student_id))
67        return released
68
69    @property
70    def expired(self):
71        # Check if application has started ...
72        if not self.startdate or (
73            self.startdate > datetime.now(pytz.utc)):
74            return True
75        # ... or ended
76        if not self.enddate or (
77            self.enddate < datetime.now(pytz.utc)):
78            return True
79        return False
80
81    logger_name = 'waeup.kofa.${sitename}.hostels'
82    logger_filename = 'hostels.log'
83
84    def writeLogMessage(self, view, message):
85        ob_class = view.__implemented__.__name__.replace('waeup.kofa.','')
86        self.logger.info(
87            '%s - %s - %s' % (ob_class, self.__name__, message))
88        return
89
90HostelsContainer = attrs_to_fields(HostelsContainer)
Note: See TracBrowser for help on using the repository browser.