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

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

Add some basic and customizable components for the calculation of hostels statistics.

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