source: main/waeup.kofa/branches/uli-fix-zc-async/src/waeup/kofa/utils/browser.py @ 17952

Last change on this file since 17952 was 16028, checked in by Henrik Bettermann, 5 years ago

Use Uli's updateIndexes method when reinding catalogs.

  • Property svn:keywords set to Id
File size: 8.4 KB
Line 
1## $Id: browser.py 16028 2020-03-06 20:40:04Z 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"""UI components for utilities and helpers.
19"""
20
21import grok
22from zope.catalog.interfaces import ICatalog
23from zope.component import queryUtility, getUtility, createObject
24from waeup.kofa.browser.layout import UtilityView
25from waeup.kofa.interfaces import IObjectHistory
26from waeup.kofa.interfaces import IUniversity
27from waeup.kofa.university.interfaces import IDepartment, ICertificate
28from waeup.kofa.utils.helpers import reindex_cat
29
30def replaceStudentMessages(old, new):
31    students = grok.getSite()['students']
32    for student in students.values():
33        history = IObjectHistory(student)
34        history.modifyMessages(old, new)
35    return
36
37def replaceApplicantMessages(old, new):
38    applicants = grok.getSite()['applicants']
39    for container in applicants.values():
40        for applicant in container.values():
41            history = IObjectHistory(applicant)
42            history.modifyMessages(old, new)
43    return
44
45def removeStudentMessage(student_id, number):
46    students = grok.getSite()['students']
47    student = students.get(student_id, None)
48    if student:
49        history = IObjectHistory(student)
50        success, text = history.removeMessage(number)
51    return success, text
52
53def removeApplicantMessage(applicant_id, number):
54    applicants = grok.getSite()['applicants']
55    try:
56        container, application_number = applicant_id.split('_')
57    except:
58        return False, 'applicant_id is wrong'
59    container = applicants.get(container, None)
60    if not container:
61        return False, 'No such container'
62    applicant = container.get(application_number, None)
63    if applicant is None:
64        return False, 'No such applicant'
65    history = IObjectHistory(applicant)
66    success, text = history.removeMessage(number)
67    return success, text
68
69class ReindexPage(UtilityView, grok.View):
70    """ Reindex view.
71
72    Reindexes a catalog. For managers only.
73    """
74    grok.context(IUniversity)
75    grok.name('reindex')
76    grok.require('waeup.managePortal')
77
78    def update(self,ctlg=None):
79        if ctlg is None:
80            self.flash('No catalog name provided.')
81            return
82        cat = queryUtility(ICatalog, name='%s_catalog' % ctlg)
83        if cat is None:
84            self.flash('%s_catalog does not exist' % ctlg)
85            return
86        self.context.logger.info(
87            'Catalog `%s_catalog` re-indexing started.' % ctlg)
88        # Use Uli's updateIndexes method.
89        # cat.updateIndexes()
90        reindex_cat(cat)
91        no_of_entries = cat.values()[0].documentCount()
92        self.flash('%d %s re-indexed.' % (no_of_entries,ctlg))
93        self.context.logger.info(
94            'Re-indexing of %d objects finished.' % no_of_entries)
95        return
96
97    def render(self):
98        self.redirect(self.url(self.context, '@@index'))
99        return
100
101class ModifyAllStudentHistory(UtilityView, grok.View):
102    """ View to modify all student histories.
103
104    """
105    grok.context(IUniversity)
106    grok.name('modify_student_history')
107    grok.require('waeup.managePortal')
108
109    def update(self,old=None, new=None):
110        if None in (old, new):
111            self.flash('Syntax: /modify_student_history?old=[old string]&new=[new string]')
112            return
113        replaceStudentMessages(old, new)
114        self.context.logger.info(
115            "'%s' replaced by '%s' in all student histories." % (old, new))
116        self.flash('Finished')
117        return
118
119    def render(self):
120        self.redirect(self.url(self.context, '@@index'))
121        return
122
123class RemoveStudentHistoryMessage(UtilityView, grok.View):
124    """ View to remove a single student history entry.
125
126    """
127    grok.context(IUniversity)
128    grok.name('remove_student_history_message')
129    grok.require('waeup.managePortal')
130
131    def update(self,student_id=None, number=None):
132        if None in (student_id, number):
133            self.flash('Syntax: /remove_student_history_message?student_id=[id]&number=[line number, starting with 0]')
134            return
135        try:
136            number=int(number)
137        except:
138            self.flash('Error')
139            return
140        success, text = removeStudentMessage(student_id, number)
141        if not success:
142            self.flash('Error: %s' % text)
143            return
144        self.context.logger.info(
145            "line '%s' removed in %s history" % (text, student_id))
146        self.flash('Finished')
147        return
148
149    def render(self):
150        self.redirect(self.url(self.context, '@@index'))
151        return
152
153class ModifyAllApplicantHistory(UtilityView, grok.View):
154    """ View to modify all student histories.
155
156    """
157    grok.context(IUniversity)
158    grok.name('modify_applicant_history')
159    grok.require('waeup.managePortal')
160
161    def update(self,old=None, new=None):
162        if None in (old, new):
163            self.flash('Syntax: /modify_applicant_history?old=[old string]&new=[new string]')
164            return
165        replaceApplicantMessages(old, new)
166        self.context.logger.info(
167            "'%s' replaced by '%s' in all applicant histories." % (old, new))
168        self.flash('Finished')
169        return
170
171    def render(self):
172        self.redirect(self.url(self.context, '@@index'))
173        return
174
175class RemoveApplicantHistoryMessage(UtilityView, grok.View):
176    """ View to remove a single applicant history entry.
177
178    """
179    grok.context(IUniversity)
180    grok.name('remove_applicant_history_message')
181    grok.require('waeup.managePortal')
182
183    def update(self,applicant_id=None, number=None):
184        if None in (applicant_id, number):
185            self.flash('Syntax: /remove_applicant_history_message?applicant_id=[id]&number=[line number, starting with 0]')
186            return
187        try:
188            number=int(number)
189        except:
190            self.flash('Error')
191            return
192        success, text = removeApplicantMessage(applicant_id, number)
193        if not success:
194            self.flash('Error: %s' % text)
195            return
196        self.context.logger.info(
197            "line '%s' removed in %s history" % (text, applicant_id))
198        self.flash('Finished')
199        return
200
201    def render(self):
202        self.redirect(self.url(self.context, '@@index'))
203        return
204
205
206class MoveDepartment(UtilityView, grok.View):
207    """ View to move a department.
208
209    """
210    grok.context(IDepartment)
211    grok.name('move_department')
212    grok.require('waeup.managePortal')
213
214    def update(self,fac=None, dep=None):
215        if None in (fac, dep):
216            self.flash('Syntax: /move_department?fac=[new faculty code]&dep=[new department code]')
217            return
218        oldcode = self.context.code
219        try:
220            self.context.moveDepartment(fac, dep)
221        except:
222            self.flash('Error')
223            return
224        grok.getSite().logger.info(
225            "Department %s moved to %s/%s" % (oldcode, fac, dep))
226        self.flash('Finished')
227        return
228
229    def render(self):
230        self.redirect(self.url(self.context, '@@index'))
231        return
232
233class MoveCertificate(UtilityView, grok.View):
234    """ View to move a certificate.
235
236    """
237    grok.context(ICertificate)
238    grok.name('move_certificate')
239    grok.require('waeup.managePortal')
240
241    def update(self,fac=None, dep=None, cert=None):
242        if None in (fac, dep, cert):
243            self.flash('Syntax: /move_certificate?fac=[new faculty code]&dep=[new department code]&cert=[new certificate code]')
244            return
245        oldcode = self.context.code
246        try:
247            self.context.moveCertificate(fac, dep, cert)
248        except:
249            self.flash('Error')
250            return
251        grok.getSite().logger.info(
252            "Certificate %s moved to %s/%s/%s" % (oldcode, fac, dep, cert))
253        self.flash('Finished')
254        return
255
256    def render(self):
257        self.redirect(self.url(self.context, '@@index'))
258        return
Note: See TracBrowser for help on using the repository browser.