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

Last change on this file since 17787 was 17787, checked in by Henrik Bettermann, 4 months ago

Add SessionConfigurationProcessor.
Add ConfigurationContainerProcessor.
Add ConfigurationContainerExporter.

  • Property svn:keywords set to Id
File size: 8.3 KB
RevLine 
[7193]1## $Id: configuration.py 17787 2024-05-15 06:42:58Z henrik $
2##
[6907]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"""
[11477]19Components for portal configuration.
[6907]20"""
21import grok
[6916]22from zope.component.interfaces import IFactory
[17755]23from zope.interface import implementedBy, Interface
24from waeup.kofa.utils.batching import ExporterBase
25from waeup.kofa.interfaces import ICSVExporter
[7811]26from waeup.kofa.interfaces import (
[6918]27    ISessionConfiguration, IConfigurationContainer, ISessionConfigurationAdd,
[17755]28    IBatchProcessor,
[6918]29    academic_sessions_vocab)
[17755]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 _
[6907]33
34class ConfigurationContainer(grok.Container):
35    """
36    The node containing the session configuration models
37    """
38
39    grok.implements(IConfigurationContainer)
40
[17787]41    frontpage_dict = dict()
42
[6907]43    def addSessionConfiguration(self, sessionconfiguration):
44        """Add a session configuration object.
45        """
46        if not ISessionConfiguration.providedBy(sessionconfiguration):
47            raise TypeError(
[7314]48                'ConfigurationContainers contain only '
49                'ISessionConfiguration instances')
[6916]50        code = unicode(sessionconfiguration.academic_session)
51        self[code] = sessionconfiguration
[6907]52        return
53
54ConfigurationContainer = attrs_to_fields(ConfigurationContainer)
[6916]55
[17787]56
57class ConfigurationContainerFactory(grok.GlobalUtility):
58    """A factory for configuration container. This factory is only needed
59    for import.
60    """
61    grok.implements(IFactory)
62    grok.name(u'waeup.ConfigurationContainer')
63
64    def __call__(self, *args, **kw):
65        return ConfigurationContainer(*args, **kw)
66
67    def getInterfaces(self):
68        return implementedBy(ConfigurationContainer)
69
[6916]70class SessionConfiguration(grok.Model):
71    """
72    Session configuration model
73    """
74
75    grok.implements(ISessionConfiguration, ISessionConfigurationAdd)
76
[6918]77    def getSessionString(self):
[13118]78        """Return the session string from the vocabulary.
79        """
[6918]80        return academic_sessions_vocab.getTerm(self.academic_session).title
81
[6916]82SessionConfiguration = attrs_to_fields(SessionConfiguration)
83
84class SessionConfigurationFactory(grok.GlobalUtility):
85    """A factory for session configuration objects.
86    """
87    grok.implements(IFactory)
88    grok.name(u'waeup.SessionConfiguration')
89    title = u"Create a new session configuration object.",
90    description = u"This factory instantiates new session configurations."
91
92    def __call__(self, *args, **kw):
93        return SessionConfiguration(*args, **kw)
94
95    def getInterfaces(self):
[7314]96        return implementedBy(SessionConfiguration)
[17755]97
[17782]98class ConfigurationContainerExporter(grok.GlobalUtility, ExporterBase):
99    """The Configuration Container Exporter exports all configuration base data.
100    It also exports the last used student id.
101    """
102    grok.implements(ICSVExporter)
103    grok.name('base_configuration')
104
105    title = _(u'Base Configuration')
[17787]106    fields = tuple(sorted(iface_names(
107        IConfigurationContainer, omit=['captcha',]))) + ('curr_stud_id',)
[17782]108
109    def mangle_value(self, value, name, context=None):
110        if name == 'curr_stud_id':
111            value = context.__parent__['students']._curr_stud_id
112        return super(
113            ConfigurationContainerExporter, self).mangle_value(
114            value, name, context=context)
115
116    def export_all(self, site, filepath=None):
117        """Export base configuration into filepath as CSV data.
118        If `filepath` is ``None``, a raw string with CSV data is returned.
119        """
120        writer, outfile = self.get_csv_writer(filepath)
121        configuration = site.get('configuration')
122        self.write_item(configuration, writer)
123        return self.close_outfile(filepath, outfile)
124
[17787]125class SessionConfigurationExporter(grok.GlobalUtility, ExporterBase):
126    """The Session Configuration Exporter exports all configuration data.
127    It iterates over all objects of the ``configuration`` container.
[17755]128    """
129    grok.implements(ICSVExporter)
[17787]130    grok.name('sessionconfigurations')
[17755]131
132    title = _(u'Session Configurations')
133
134    fields = tuple(sorted(iface_names(ISessionConfiguration)))
135
136    def export(self, configurations, filepath=None):
137        """Export `configurations`, an iterable, as CSV file.
138
139        If `filepath` is ``None``, a raw string with CSV data is returned.
140        """
141        writer, outfile = self.get_csv_writer(filepath)
142        for configuration in configurations:
143            self.write_item(configuration, writer)
144        return self.close_outfile(filepath, outfile)
145
146    def export_all(self, site, filepath=None):
[17787]147        """Export session configurations into filepath as CSV data.
[17755]148
149        If `filepath` is ``None``, a raw string with CSV data is returned.
150        """
151        configurations = site.get('configuration', {})
152        return self.export(configurations.values(), filepath)
153
[17787]154class ConfigurationContainerProcessor(BatchProcessor):
155    """The Configuration Container Processor processes processes the
156    portal's base confoiguration data. This container exists in the portal.
157    Thus only the update method is allowed.
[17755]158
159    """
160    grok.implements(IBatchProcessor)
161    grok.provides(IBatchProcessor)
162    grok.context(Interface)
[17787]163    util_name = 'configurationcontainerupdater'
[17755]164    grok.name(util_name)
165
[17787]166    name = u'ConfigurationConainer Processor (update only)'
167    iface = IConfigurationContainer
168    factory_name = 'waeup.ConfigurationContainer'
169
170    @property
171    def available_fields(self):
172        return tuple(
173            sorted(iface_names(
174                IConfigurationContainer,
175                omit=['captcha',]))) + ('curr_stud_id',)
176
177    def parentsExist(self, row, site):
178        return True
179
180    def entryExists(self, row, site):
181        return True
182
183    def getParent(self, row, site):
184        return site
185
186    def getEntry(self, row, site):
187        return site['configuration']
188
189    def updateEntry(self, obj, row, site, filename):
190        """Update obj to the values given in row.
191        """
192        if 'curr_stud_id' in row:
193            studid = row.get('curr_stud_id')
194            site['students']._curr_stud_id = int(studid)
195        row.pop('curr_stud_id')
196        super(ConfigurationContainerProcessor, self).updateEntry(
197            obj, row, site, filename)
198        return
199
200class SessionConfigurationProcessor(BatchProcessor):
201    """The (Session) Configuration Processor processes session configuration
202    objects in the ``configuration`` container.
203    """
204    grok.implements(IBatchProcessor)
205    grok.provides(IBatchProcessor)
206    grok.context(Interface)
207    util_name = 'sessionconfigurationprocessor'
208    grok.name(util_name)
209
[17755]210    name = u'SessionConfiguration Processor'
211    iface = ISessionConfiguration
212    factory_name = 'waeup.SessionConfiguration'
213
214    def parentsExist(self, row, site):
215        return 'configuration' in site.keys()
216
217    def entryExists(self, row, site):
218        return row['academic_session'] in site['configuration'].keys()
219
220    def getParent(self, row, site):
221        return site['configuration']
222
223    def getEntry(self, row, site):
224        if not self.entryExists(row, site):
225            return None
226        parent = self.getParent(row, site)
227        return parent.get(row['academic_session'])
228
229    def addEntry(self, obj, row, site):
230        parent = self.getParent(row, site)
231        parent.addSessionConfiguration(obj)
232        return
233
234    def delEntry(self, row, site):
235        configuration = self.getEntry(row, site)
236        if user is not None:
237            parent = self.getParent(row, site)
238            grok.getSite().logger.info(
239                '%s - %s - Session configuration removed' % (self.name, row['academic_session']))
240            del parent[configuration.academic_session]
241        pass
242
[17787]243
Note: See TracBrowser for help on using the repository browser.