[8846] | 1 | ## $Id: container.py 8846 2012-06-29 08:19:47Z 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 |
---|
| 24 | from waeup.kofa.interfaces import IKofaPluggable |
---|
| 25 | from waeup.kofa.mandates.interfaces import IMandatesContainer, IMandate |
---|
| 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 | |
---|
| 33 | def addMandate(self, mandate): |
---|
| 34 | if not IMandate.providedBy(mandate): |
---|
| 35 | raise TypeError( |
---|
| 36 | 'MandateContainers contain only IMandate instances') |
---|
| 37 | self[mandate.mandate_id] = mandate |
---|
| 38 | return |
---|
| 39 | |
---|
| 40 | def removeExpired(self): |
---|
| 41 | """Remove all expired mandates. |
---|
| 42 | """ |
---|
| 43 | for mandate in self.keys(): |
---|
| 44 | if self[mandate].expires < datetime.utcnow(): |
---|
| 45 | del self[mandate] |
---|
| 46 | return |
---|
| 47 | |
---|
| 48 | class MandatesPlugin(grok.GlobalUtility): |
---|
| 49 | """A plugin that creates container for mandates inside a university. |
---|
| 50 | """ |
---|
| 51 | grok.implements(IKofaPluggable) |
---|
| 52 | grok.name('mandates') |
---|
| 53 | |
---|
| 54 | def setup(self, site, name, logger): |
---|
| 55 | if 'mandates' in site.keys(): |
---|
| 56 | logger.warn('Could not create container for faculties in Kofa.') |
---|
| 57 | return |
---|
| 58 | site['mandates'] = MandatesContainer() |
---|
| 59 | logger.info('Container for mandates created') |
---|
| 60 | return |
---|
| 61 | |
---|
| 62 | def update(self, site, name, logger): |
---|
| 63 | if not 'mandates' in site.keys(): |
---|
| 64 | self.setup(site, name, logger) |
---|