source: main/waeup.kofa/trunk/src/waeup/kofa/applicants/root.py @ 8525

Last change on this file since 8525 was 8412, checked in by Henrik Bettermann, 13 years ago

We do not need to set new attributes in instances of classes when using attrs_to_fields. It's only commented out because I'd like to leave this here for a while to remember this alternative solution.

  • Property svn:keywords set to Id
File size: 4.4 KB
Line 
1## $Id: root.py 8412 2012-05-10 18:54:28Z 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"""
19The root for applicants.
20"""
21import grok
22from hurry.query import Eq
23from hurry.query.interfaces import IQuery
24from zope.component import getUtility
25from zope.schema import getFields
26from waeup.kofa.interfaces import IKofaPluggable
27from waeup.kofa.applicants.interfaces import IApplicantsRoot
28from waeup.kofa.utils.logger import Logger
29from waeup.kofa.utils.helpers import attrs_to_fields
30
31class ApplicantsRoot(grok.Container, Logger):
32    """The root of applicants-related components. It contains only
33    containers for applicants.
34    """
35    grok.implements(IApplicantsRoot)
36
37    local_roles = []
38
39    #: A dictionary to hold per language translations of description string.
40    description_dict = {}
41
42    logger_name = 'waeup.kofa.${sitename}.applicants'
43    logger_filename = 'applicants.log'
44
45    def logger_info(self, target, ob_class, comment=None):
46        """Get the logger's info method.
47        """
48        self.logger.info('%s - %s - %s' % (
49                target, ob_class, comment))
50        return
51
52ApplicantsRoot = attrs_to_fields(ApplicantsRoot)
53
54class ApplicantsPlugin(grok.GlobalUtility):
55    """A KofaPlugin that creates an applicants root in portal.
56
57    This plugin should be called by a typical
58    `waeup.kofa.app.Universtiy` instance on creation time. The
59    :meth:`update` method normally can also be triggered manually over
60    the main site configuration.
61
62    Implements :class:`waeup.kofa.interfaces.IKofaPluggable`
63    """
64    grok.name('applicants')
65    grok.implements(IKofaPluggable)
66    log_prefix = 'ApplicantsPlugin'
67
68    def setup(self, site, name, logger):
69        """Create a new :class:`ApplicantsRoot` instance in `site`.
70        """
71        site['applicants'] = ApplicantsRoot()
72        logger.info(
73            '%s: Installed applicants root.' % (self.log_prefix,)
74            )
75        return
76
77    def update(self, site, name, logger):
78        """Update site wide ``applicants`` root.
79
80        If the site already contains a suitable ``applicants`` root,
81        leave it that way. If not create one and delete the old one if
82        appropriate.
83        """
84        app_folder = site.get('applicants', None)
85        site_name = getattr(site, '__name__', '<Unnamed Site>')
86        if IApplicantsRoot.providedBy(app_folder):
87            items = getFields(IApplicantsRoot).items()
88            nothing_to_do = True
89            #for i in items:
90            #    if not hasattr(app_folder,i[0]):
91            #        nothing_to_do = False
92            #        setattr(app_folder,i[0],i[1].missing_value)
93            #        logger.info(
94            #            '%s: %s added to root.' % (self.log_prefix,i[0]))
95            # can be removed after upgrading uniben
96            #if not hasattr(app_folder, 'description_dict'):
97            #    nothing_to_do = False
98            #    setattr(app_folder,'description_dict',{})
99            #    logger.info(
100            #        '%s: description_dict added to root.' % self.log_prefix)
101            if nothing_to_do:
102                logger.info(
103                    '%s: Updating site at %s: Nothing to do.' % (
104                        self.log_prefix, site_name,)
105                    )
106            return
107        elif app_folder is not None:
108            # Applicants need update. Remove old instance.
109            logger.warn(
110                '%s: Outdated applicants folder detected at site %s.'
111                'Removing it.' % (self.log_prefix, site_name)
112                    )
113            del site['applicants']
114        # Add new applicants.
115        logger.info(
116            '%s: Updating site at %s. Installing '
117            'applicants.' % (self.log_prefix, site_name,)
118            )
119        self.setup(site, name, logger)
120        return
Note: See TracBrowser for help on using the repository browser.