[8846] | 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 | """ |
---|
| 19 | Containers which contain mandate objects. |
---|
| 20 | """ |
---|
| 21 | import grok |
---|
| 22 | from datetime import datetime |
---|
| 23 | from grok import index |
---|
[11949] | 24 | from waeup.ikoba.interfaces import IIkobaPluggable |
---|
| 25 | from waeup.ikoba.mandates.interfaces import IMandatesContainer, IMandate |
---|
[8846] | 26 | |
---|
| 27 | class MandatesContainer(grok.Container): |
---|
| 28 | """This is a container for all kind of mandates. |
---|
| 29 | """ |
---|
| 30 | grok.implements(IMandatesContainer) |
---|
| 31 | grok.provides(IMandatesContainer) |
---|
| 32 | |
---|
[14221] | 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 | |
---|
[8846] | 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 | """ |
---|
[8910] | 53 | num_deleted = 0 |
---|
[14221] | 54 | keys = list(self.keys()) |
---|
| 55 | for mandate in keys: |
---|
[8846] | 56 | if self[mandate].expires < datetime.utcnow(): |
---|
| 57 | del self[mandate] |
---|
[8910] | 58 | num_deleted += 1 |
---|
[14221] | 59 | grok.getSite().logger.info('%s mandates purged' % num_deleted) |
---|
[8910] | 60 | return num_deleted |
---|
[8846] | 61 | |
---|
| 62 | class MandatesPlugin(grok.GlobalUtility): |
---|
[11954] | 63 | """A plugin that creates container for mandates inside a company. |
---|
[8846] | 64 | """ |
---|
[11949] | 65 | grok.implements(IIkobaPluggable) |
---|
[8846] | 66 | grok.name('mandates') |
---|
| 67 | |
---|
| 68 | def setup(self, site, name, logger): |
---|
| 69 | if 'mandates' in site.keys(): |
---|
[11949] | 70 | logger.warn('Could not create container for mandates in Ikoba.') |
---|
[8846] | 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) |
---|