Ignore:
Timestamp:
18 Jul 2012, 06:47:21 (12 years ago)
Author:
Henrik Bettermann
Message:

ObjectHistory? method, helper function and view to modify (manipulate) student history messages.

Location:
main/waeup.kofa/trunk/src/waeup/kofa
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/src/waeup/kofa/objecthistory.py

    r8758 r9012  
    7878        self._annotations[self.history_key] = msgs
    7979        return
     80
     81    def modifyMessages(self, old, new):
     82        """Replaces the message (history entry) in msg.
     83
     84        Substring 'old' .will be replaced by 'new' in all
     85        messages.
     86        """
     87        old_msgs = self._getMessages()
     88        new_msgs = PersistentList()
     89        for msg in old_msgs:
     90            new_msg = msg.replace(old, new)
     91            new_msgs.append(new_msg)
     92        self._annotations[self.history_key] = new_msgs
     93        return
  • main/waeup.kofa/trunk/src/waeup/kofa/tests/test_objecthistory.py

    r8234 r9012  
    9191        self.assertMatches('<YYYY-MM-DD hh:mm:ss> UTC - blah by Bob', result)
    9292
     93    def test_modify_messages(self):
     94        principal = Principal('bob')
     95        principal.title = 'Bob'
     96        newInteraction(Participation(principal)) # set current user
     97        hist = IObjectHistory(self.obj)
     98        hist.addMessage('blah')
     99        hist.modifyMessages('blah', 'blow')
     100        result = ''.join(hist.messages)
     101        self.assertMatches('<YYYY-MM-DD hh:mm:ss> UTC - blow by Bob', result)
     102
    93103    def test_messages(self):
    94104        # we get messages as a persistent list of strings
  • main/waeup.kofa/trunk/src/waeup/kofa/utils/browser.py

    r9011 r9012  
    2323from zope.component import queryUtility, getUtility, createObject
    2424from waeup.kofa.browser.layout import UtilityView
     25from waeup.kofa.interfaces import IObjectHistory
    2526
    2627from waeup.kofa.interfaces import IUniversity
     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
    2735
    2836class ReindexPage(UtilityView, grok.View):
     
    5563        self.redirect(self.url(self.context, '@@index'))
    5664        return
     65
     66class ModifyAllStudentHistory(UtilityView, grok.View):
     67    """ View to modify all student histories.
     68
     69    """
     70    grok.context(IUniversity)
     71    grok.name('modify_student_history')
     72    grok.require('waeup.managePortal')
     73
     74    def update(self,old=None, new=None):
     75        if None in (old, new):
     76            self.flash('Syntax: /modify_student_history?old=[old string]&new=[new string]')
     77            return
     78        replaceStudentMessages(old, new)
     79        self.context.logger.info(
     80            "'%s' replaced by '%s' in all student histories." % (old, new))
     81        self.flash('Finished')
     82        return
     83
     84    def render(self):
     85        self.redirect(self.url(self.context, '@@index'))
     86        return
  • main/waeup.kofa/trunk/src/waeup/kofa/utils/tests/test_browser.py

    r9011 r9012  
    3333from waeup.kofa.testing import FunctionalLayer, FunctionalTestCase
    3434
     35from waeup.kofa.utils.browser import replaceStudentMessages
    3536from waeup.kofa.students.tests.test_browser import StudentsFullSetup
    3637
    3738class UtilsUITests(StudentsFullSetup):
    38     # Tests for Student class views and pages
    3939
    4040    layer = FunctionalLayer
     41
     42    def test_replace_student_messages(self):
     43        self.assertTrue('Student record created by system' in
     44            self.student.history.messages[0])
     45        replaceStudentMessages('system', 'me')
     46        self.assertTrue('Student record created by me' in
     47            self.student.history.messages[0])
     48
     49    def test_modify_all_student_history(self):
     50        self.assertTrue('Student record created by system' in
     51            self.student.history.messages[0])
     52        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
     53        self.browser.open('http://localhost/app/modify_student_history')
     54        self.assertTrue(
     55            'Syntax: /modify_student_history?old=[old string]&new=[new string]'
     56            in self.browser.contents)
     57        self.browser.open(
     58            'http://localhost/app/modify_student_history?old=by system&new=by me')
     59        self.assertTrue('Finished' in self.browser.contents)
     60        self.assertTrue('Student record created by me' in
     61            self.student.history.messages[0])
    4162
    4263    def test_reindex(self):
Note: See TracChangeset for help on using the changeset viewer.