source: main/waeup.kofa/trunk/src/waeup/kofa/utils/browser.py @ 11089

Last change on this file since 11089 was 9127, checked in by Henrik Bettermann, 12 years ago

Quick shot: Remove single history messages through URL. All views in the utils.browser module should be replaced by proper form pages.

  • Property svn:keywords set to Id
File size: 6.6 KB
RevLine 
[9011]1## $Id: browser.py 9127 2012-08-30 08:55:31Z 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
[9012]25from waeup.kofa.interfaces import IObjectHistory
[9011]26
27from waeup.kofa.interfaces import IUniversity
28
[9012]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
[9122]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
[9127]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
[9011]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'))
[9012]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'))
[9122]118        return
119
[9127]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
[9122]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'))
[9127]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'))
[9011]200        return
Note: See TracBrowser for help on using the repository browser.