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.accesscodes import get_access_code |
---|
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 | @property |
---|
40 | def local_roles(cls): |
---|
41 | return ['waeup.local.ApplicationsOfficer'] |
---|
42 | |
---|
43 | class ApplicantsPlugin(grok.GlobalUtility): |
---|
44 | """A WAeUPSIRPPlugin that creates an applicants root in portal. |
---|
45 | |
---|
46 | This plugin should be called by a typical |
---|
47 | `waeup.sirp.app.Universtiy` instance on creation time. The |
---|
48 | :meth:`update` method normally can also be triggered manually over |
---|
49 | the main site configuration. |
---|
50 | |
---|
51 | Implements :class:`waeup.sirp.interfaces.IWAeUPSIRPPluggable` |
---|
52 | """ |
---|
53 | grok.name('applicants') |
---|
54 | grok.implements(IWAeUPSIRPPluggable) |
---|
55 | log_prefix = 'ApplicantsPlugin' |
---|
56 | |
---|
57 | def setup(self, site, name, logger): |
---|
58 | """Create a new :class:`ApplicantsRoot` instance in `site`. |
---|
59 | """ |
---|
60 | site['applicants'] = ApplicantsRoot() |
---|
61 | logger.info( |
---|
62 | '%s: Installed applicants root.' % (self.log_prefix,) |
---|
63 | ) |
---|
64 | return |
---|
65 | |
---|
66 | def update(self, site, name, logger): |
---|
67 | """Update site wide ``applicants`` root. |
---|
68 | |
---|
69 | If the site already contains a suitable ``applicants`` root, |
---|
70 | leave it that way. If not create one and delete the old one if |
---|
71 | appropriate. |
---|
72 | """ |
---|
73 | app_folder = site.get('applicants', None) |
---|
74 | site_name = getattr(site, '__name__', '<Unnamed Site>') |
---|
75 | if IApplicantsRoot.providedBy(app_folder): |
---|
76 | # Applicants up to date. Return. |
---|
77 | logger.info( |
---|
78 | '%s: Updating site at %s: Nothing to do.' % ( |
---|
79 | self.log_prefix, site_name,) |
---|
80 | ) |
---|
81 | return |
---|
82 | elif app_folder is not None: |
---|
83 | # Applicants need update. Remove old instance. |
---|
84 | logger.warn( |
---|
85 | '%s: Outdated applicants folder detected at site %s.' |
---|
86 | 'Removing it.' % (self.log_prefix, site_name) |
---|
87 | ) |
---|
88 | del site['applicants'] |
---|
89 | # Add new applicants. |
---|
90 | logger.info( |
---|
91 | '%s: Updating site at %s. Installing ' |
---|
92 | 'applicants.' % (self.log_prefix, site_name,) |
---|
93 | ) |
---|
94 | self.setup(site, name, logger) |
---|
95 | return |
---|
96 | |
---|
97 | def get_applicant_data(identifier): |
---|
98 | """Get applicant data associated with `identifier`. |
---|
99 | |
---|
100 | Returns the applicant object if successful and ``None`` else. |
---|
101 | |
---|
102 | As `identifier` we expect an access code in format |
---|
103 | like ``PREFIX-XXX-YYYYYYYY`` where ``PREFIX`` is something like |
---|
104 | ``APP`` or ``PUDE``, ``XXX`` the access code series and |
---|
105 | ``YYYYYYYYYY`` the real accesscode number. |
---|
106 | |
---|
107 | This function requires a fully blown setup as it does catalog |
---|
108 | lookups for finding applicants. |
---|
109 | """ |
---|
110 | query = getUtility(IQuery) |
---|
111 | results = list(query.searchResults( |
---|
112 | Eq(('applicants_catalog', 'access_code'), identifier) |
---|
113 | )) |
---|
114 | if len(results) == 0: |
---|
115 | return None |
---|
116 | return results[0] |
---|
117 | |
---|
118 | def application_exists(identifier): |
---|
119 | """Check whether an application for the given identifier already |
---|
120 | exists. |
---|
121 | |
---|
122 | `identifier` will normally be an access code. |
---|
123 | """ |
---|
124 | applicant = get_applicant_data(identifier) |
---|
125 | if applicant is None: |
---|
126 | return False |
---|
127 | return True |
---|