source: main/waeup.kofa/trunk/src/waeup/kofa/configuration.py @ 17769

Last change on this file since 17769 was 17755, checked in by Henrik Bettermann, 6 months ago

Add exporter and importer for session configuration objects (tests will follow).

  • Property svn:keywords set to Id
File size: 5.4 KB
Line 
1## $Id: configuration.py 17755 2024-05-10 09:11:48Z 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"""
19Components for portal configuration.
20"""
21import grok
22from zope.component.interfaces import IFactory
23from zope.interface import implementedBy, Interface
24from waeup.kofa.utils.batching import ExporterBase
25from waeup.kofa.interfaces import ICSVExporter
26from waeup.kofa.interfaces import (
27    ISessionConfiguration, IConfigurationContainer, ISessionConfigurationAdd,
28    IBatchProcessor,
29    academic_sessions_vocab)
30from waeup.kofa.utils.helpers import attrs_to_fields, iface_names
31from waeup.kofa.utils.batching import BatchProcessor
32from waeup.kofa.interfaces import MessageFactory as _
33
34class ConfigurationContainer(grok.Container):
35    """
36    The node containing the session configuration models
37    """
38
39    grok.implements(IConfigurationContainer)
40
41    def addSessionConfiguration(self, sessionconfiguration):
42        """Add a session configuration object.
43        """
44        if not ISessionConfiguration.providedBy(sessionconfiguration):
45            raise TypeError(
46                'ConfigurationContainers contain only '
47                'ISessionConfiguration instances')
48        code = unicode(sessionconfiguration.academic_session)
49        self[code] = sessionconfiguration
50        return
51
52ConfigurationContainer = attrs_to_fields(ConfigurationContainer)
53
54class SessionConfiguration(grok.Model):
55    """
56    Session configuration model
57    """
58
59    grok.implements(ISessionConfiguration, ISessionConfigurationAdd)
60
61    def getSessionString(self):
62        """Return the session string from the vocabulary.
63        """
64        return academic_sessions_vocab.getTerm(self.academic_session).title
65
66SessionConfiguration = attrs_to_fields(SessionConfiguration)
67
68class SessionConfigurationFactory(grok.GlobalUtility):
69    """A factory for session configuration objects.
70    """
71    grok.implements(IFactory)
72    grok.name(u'waeup.SessionConfiguration')
73    title = u"Create a new session configuration object.",
74    description = u"This factory instantiates new session configurations."
75
76    def __call__(self, *args, **kw):
77        return SessionConfiguration(*args, **kw)
78
79    def getInterfaces(self):
80        return implementedBy(SessionConfiguration)
81
82class ConfigurationExporter(grok.GlobalUtility, ExporterBase):
83    """The Configuration Exporter exports all configuration data. It iterates over all
84    objects of the ``configuration`` container.
85    """
86    grok.implements(ICSVExporter)
87    grok.name('configurations')
88
89    title = _(u'Session Configurations')
90
91    fields = tuple(sorted(iface_names(ISessionConfiguration)))
92
93    def export(self, configurations, filepath=None):
94        """Export `configurations`, an iterable, as CSV file.
95
96        If `filepath` is ``None``, a raw string with CSV data is returned.
97        """
98        writer, outfile = self.get_csv_writer(filepath)
99        for configuration in configurations:
100            self.write_item(configuration, writer)
101        return self.close_outfile(filepath, outfile)
102
103    def export_all(self, site, filepath=None):
104        """Export configurations into filepath as CSV data.
105
106        If `filepath` is ``None``, a raw string with CSV data is returned.
107        """
108        configurations = site.get('configuration', {})
109        return self.export(configurations.values(), filepath)
110
111class ConfigurationProcessor(BatchProcessor):
112    """The Configuration Processor processes session configuration objects in
113    the ``configuration`` container.
114
115    """
116    grok.implements(IBatchProcessor)
117    grok.provides(IBatchProcessor)
118    grok.context(Interface)
119    util_name = 'configurationprocessor'
120    grok.name(util_name)
121
122    name = u'SessionConfiguration Processor'
123    iface = ISessionConfiguration
124    factory_name = 'waeup.SessionConfiguration'
125
126    mode = None
127
128    def parentsExist(self, row, site):
129        return 'configuration' in site.keys()
130
131    def entryExists(self, row, site):
132        return row['academic_session'] in site['configuration'].keys()
133
134    def getParent(self, row, site):
135        return site['configuration']
136
137    def getEntry(self, row, site):
138        if not self.entryExists(row, site):
139            return None
140        parent = self.getParent(row, site)
141        return parent.get(row['academic_session'])
142
143    def addEntry(self, obj, row, site):
144        parent = self.getParent(row, site)
145        parent.addSessionConfiguration(obj)
146        return
147
148    def delEntry(self, row, site):
149        configuration = self.getEntry(row, site)
150        if user is not None:
151            parent = self.getParent(row, site)
152            grok.getSite().logger.info(
153                '%s - %s - Session configuration removed' % (self.name, row['academic_session']))
154            del parent[configuration.academic_session]
155        pass
156
Note: See TracBrowser for help on using the repository browser.