source: main/waeup.ikoba/trunk/src/waeup/ikoba/mandates/container.py @ 17664

Last change on this file since 17664 was 14221, checked in by Henrik Bettermann, 8 years ago

Count mandates on configuration page and provide 'Purge' button.

  • Property svn:keywords set to Id
File size: 2.7 KB
Line 
1## $Id: container.py 14221 2016-10-20 21:13:17Z henrik $
2##
3## Copyright (C) 2012 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 mandate objects.
20"""
21import grok
22from datetime import datetime
23from grok import index
24from waeup.ikoba.interfaces import IIkobaPluggable
25from waeup.ikoba.mandates.interfaces import IMandatesContainer, IMandate
26
27class MandatesContainer(grok.Container):
28    """This is a container for all kind of mandates.
29    """
30    grok.implements(IMandatesContainer)
31    grok.provides(IMandatesContainer)
32
33    @property
34    def count(self):
35        """Count active and expired mandates.
36        """
37        now = datetime.utcnow()
38        total = len(self)
39        expired = len([i for i in self.values() if i.expires < now])
40        active = total - expired
41        return active, expired, total
42
43    def addMandate(self, mandate):
44        if not IMandate.providedBy(mandate):
45            raise TypeError(
46                'MandateContainers contain only IMandate instances')
47        self[mandate.mandate_id] = mandate
48        return
49
50    def removeExpired(self):
51        """Remove all expired mandates.
52        """
53        num_deleted = 0
54        keys = list(self.keys())
55        for mandate in keys:
56            if self[mandate].expires < datetime.utcnow():
57                del self[mandate]
58                num_deleted += 1
59        grok.getSite().logger.info('%s mandates purged' % num_deleted)
60        return num_deleted
61
62class MandatesPlugin(grok.GlobalUtility):
63    """A plugin that creates container for mandates inside a company.
64    """
65    grok.implements(IIkobaPluggable)
66    grok.name('mandates')
67
68    def setup(self, site, name, logger):
69        if 'mandates' in site.keys():
70            logger.warn('Could not create container for mandates in Ikoba.')
71            return
72        site['mandates'] = MandatesContainer()
73        logger.info('Container for mandates created')
74        return
75
76    def update(self, site, name, logger):
77        if not 'mandates' in site.keys():
78            self.setup(site, name, logger)
Note: See TracBrowser for help on using the repository browser.