source: main/waeup.kofa/branches/henrik-transcript-workflow/src/waeup/kofa/utils/browser.py @ 17664

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

Improve department and certificate movers. Add utility view for moving certificates.

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