1 | ## |
---|
2 | ## root.py |
---|
3 | ## Login : <uli@pu.smp.net> |
---|
4 | ## Started on Thu Jan 20 04:17:59 2011 Uli Fouquet |
---|
5 | ## $Id$ |
---|
6 | ## |
---|
7 | ## Copyright (C) 2011 Uli Fouquet |
---|
8 | ## This program is free software; you can redistribute it and/or modify |
---|
9 | ## it under the terms of the GNU General Public License as published by |
---|
10 | ## the Free Software Foundation; either version 2 of the License, or |
---|
11 | ## (at your option) any later version. |
---|
12 | ## |
---|
13 | ## This program is distributed in the hope that it will be useful, |
---|
14 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | ## GNU General Public License for more details. |
---|
17 | ## |
---|
18 | ## You should have received a copy of the GNU General Public License |
---|
19 | ## along with this program; if not, write to the Free Software |
---|
20 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
21 | ## |
---|
22 | """ |
---|
23 | The root for applicants. |
---|
24 | """ |
---|
25 | import grok |
---|
26 | from hurry.query import Eq |
---|
27 | from hurry.query.interfaces import IQuery |
---|
28 | from zope.component import getUtility |
---|
29 | from waeup.sirp.interfaces import IWAeUPSIRPPluggable |
---|
30 | from waeup.sirp.utils.logger import ISiteLoggers |
---|
31 | from waeup.sirp.applicants.interfaces import IApplicantsRoot |
---|
32 | |
---|
33 | class ApplicantsRoot(grok.Container): |
---|
34 | """The root of applicants-related components. It contains only |
---|
35 | containers for applicants. |
---|
36 | """ |
---|
37 | grok.implements(IApplicantsRoot) |
---|
38 | |
---|
39 | local_roles = ['waeup.ApplicationsOfficer'] |
---|
40 | |
---|
41 | @property |
---|
42 | def logger(self): |
---|
43 | """Get a logger for applicants. |
---|
44 | """ |
---|
45 | loggers = getUtility(ISiteLoggers).getLoggers() |
---|
46 | if 'waeup.sirp.%s.applicants' not in loggers.keys(): |
---|
47 | loggers.register('waeup.sirp.%s.applicants') |
---|
48 | return loggers.getLogger('waeup.sirp.%s.applicants') |
---|
49 | |
---|
50 | class ApplicantsPlugin(grok.GlobalUtility): |
---|
51 | """A WAeUPSIRPPlugin that creates an applicants root in portal. |
---|
52 | |
---|
53 | This plugin should be called by a typical |
---|
54 | `waeup.sirp.app.Universtiy` instance on creation time. The |
---|
55 | :meth:`update` method normally can also be triggered manually over |
---|
56 | the main site configuration. |
---|
57 | |
---|
58 | Implements :class:`waeup.sirp.interfaces.IWAeUPSIRPPluggable` |
---|
59 | """ |
---|
60 | grok.name('applicants') |
---|
61 | grok.implements(IWAeUPSIRPPluggable) |
---|
62 | log_prefix = 'ApplicantsPlugin' |
---|
63 | |
---|
64 | def setup(self, site, name, logger): |
---|
65 | """Create a new :class:`ApplicantsRoot` instance in `site`. |
---|
66 | """ |
---|
67 | site['applicants'] = ApplicantsRoot() |
---|
68 | logger.info( |
---|
69 | '%s: Installed applicants root.' % (self.log_prefix,) |
---|
70 | ) |
---|
71 | return |
---|
72 | |
---|
73 | def update(self, site, name, logger): |
---|
74 | """Update site wide ``applicants`` root. |
---|
75 | |
---|
76 | If the site already contains a suitable ``applicants`` root, |
---|
77 | leave it that way. If not create one and delete the old one if |
---|
78 | appropriate. |
---|
79 | """ |
---|
80 | app_folder = site.get('applicants', None) |
---|
81 | site_name = getattr(site, '__name__', '<Unnamed Site>') |
---|
82 | if IApplicantsRoot.providedBy(app_folder): |
---|
83 | # Applicants up to date. Return. |
---|
84 | logger.info( |
---|
85 | '%s: Updating site at %s: Nothing to do.' % ( |
---|
86 | self.log_prefix, site_name,) |
---|
87 | ) |
---|
88 | return |
---|
89 | elif app_folder is not None: |
---|
90 | # Applicants need update. Remove old instance. |
---|
91 | logger.warn( |
---|
92 | '%s: Outdated applicants folder detected at site %s.' |
---|
93 | 'Removing it.' % (self.log_prefix, site_name) |
---|
94 | ) |
---|
95 | del site['applicants'] |
---|
96 | # Add new applicants. |
---|
97 | logger.info( |
---|
98 | '%s: Updating site at %s. Installing ' |
---|
99 | 'applicants.' % (self.log_prefix, site_name,) |
---|
100 | ) |
---|
101 | self.setup(site, name, logger) |
---|
102 | return |
---|
103 | |
---|
104 | def get_applicant_data(identifier): |
---|
105 | """Get applicant data associated with `identifier`. |
---|
106 | |
---|
107 | Returns the applicant object if successful and ``None`` else. |
---|
108 | |
---|
109 | As `identifier` we expect an access code in format |
---|
110 | like ``PREFIX-XXX-YYYYYYYY`` where ``PREFIX`` is something like |
---|
111 | ``APP`` or ``PUDE``, ``XXX`` the access code series and |
---|
112 | ``YYYYYYYYYY`` the real accesscode number. |
---|
113 | |
---|
114 | This function requires a fully blown setup as it does catalog |
---|
115 | lookups for finding applicants. |
---|
116 | """ |
---|
117 | query = getUtility(IQuery) |
---|
118 | results = list(query.searchResults( |
---|
119 | Eq(('applicants_catalog', 'access_code'), identifier) |
---|
120 | )) |
---|
121 | if len(results) == 0: |
---|
122 | return None |
---|
123 | return results[0] |
---|
124 | |
---|
125 | def application_exists(identifier): |
---|
126 | """Check whether an application for the given identifier already |
---|
127 | exists. |
---|
128 | |
---|
129 | `identifier` will normally be an access code. |
---|
130 | """ |
---|
131 | applicant = get_applicant_data(identifier) |
---|
132 | if applicant is None: |
---|
133 | return False |
---|
134 | return True |
---|