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

Last change on this file since 13222 was 13216, checked in by Henrik Bettermann, 10 years ago

Extend and use container_code attribute to distinguish used and unused records.

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