Ignore:
Timestamp:
6 Nov 2015, 05:43:37 (9 years ago)
Author:
Henrik Bettermann
Message:

Implement portal maintenance mode.

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

Legend:

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

    r13354 r13394  
    337337
    338338    def update(self, SUBMIT=None, camefrom=None):
     339        blocker = grok.getSite()['configuration'].maintmode_enabled_by
     340        if blocker:
     341            self.flash(_('The portal is in maintenance mode '
     342                        'and authentication (login) temporarily disabled.'),
     343                       type='warning')
    339344        self.camefrom = camefrom
    340345        if SUBMIT is not None:
     
    363368                    return
    364369                self.redirect(self.camefrom)
     370                return
     371            # Display second warning message and remind user that
     372            # s/he can't login.
     373            if blocker:
     374                self.flash(_('The portal is in maintenance mode. '
     375                            'You can\'t login!'),
     376                           type='danger')
    365377                return
    366378            # Display appropriate flash message if credentials are correct
     
    10011013    form_fields = grok.AutoFields(IConfigurationContainer).omit(
    10021014        'frontpage_dict', 'next_matric_integer', 'next_matric_integer_2')
     1015    form_fields['maintmode_enabled_by'].for_display = True
    10031016
    10041017    @action(_('Save'), style='primary')
     
    13541367
    13551368    def update(self, filename=None, select=None, cancel=None):
     1369        if not grok.getSite()['configuration'].maintmode_enabled_by and \
     1370            not self.request.principal.id == 'admin':
     1371            self.flash(
     1372                _('Portal must be in maintenance mode for data import.'),
     1373                type='warning')
     1374            self.redirect(self.url(self.context))
     1375            return
    13561376        if cancel is not None:
    13571377            self.redirect(self.url(self.context))
     
    13621382            session['import_filename'] = select
    13631383            self.redirect(self.url(self.context, '@@import2'))
     1384        return
    13641385
    13651386class DatacenterImportStep2(KofaPage):
     
    16391660            self.mode)
    16401661
     1662        # Disable maintenance mode if set.
     1663        if grok.getSite()['configuration'].maintmode_enabled_by:
     1664            grok.getSite()['configuration'].maintmode_enabled_by = None
     1665            self.flash(_('Maintenance mode disabled.'))
     1666
    16411667        if self.warn_num:
    16421668            self.flash(_('Processing of ${a} rows failed.',
     
    18231849            '%s - downloaded: %s, job_id=%s' % (ob_class, filename, job_id))
    18241850        return result
     1851
     1852class SwitchMaintModePage(UtilityView, grok.View):
     1853    """Import managers must disable authentication for all other users
     1854    before starting an import. This pages switches maintenance mode
     1855    on and off.
     1856    """
     1857    grok.context(IDataCenter)
     1858    grok.name('switchmaintmode')
     1859    grok.require('waeup.importData')
     1860
     1861    def update(self):
     1862        ob_class = self.__implemented__.__name__.replace('waeup.kofa.','')
     1863        if grok.getSite()['configuration'].maintmode_enabled_by:
     1864            grok.getSite()['configuration'].maintmode_enabled_by = None
     1865            self.context.logger.info('%s - maintmode disabled' % ob_class)
     1866            self.flash(_('Maintenance mode disabled.'))
     1867        else:
     1868            grok.getSite()['configuration'].maintmode_enabled_by = unicode(
     1869                self.request.principal.id)
     1870            self.context.logger.info('%s - maintmode enabled' % ob_class)
     1871            self.flash(_('Maintenance mode enabled.'), type='warning')
     1872        self.redirect(self.url(self.context))
     1873        return
     1874
     1875    def render(self):
     1876        return
    18251877
    18261878#
  • main/waeup.kofa/trunk/src/waeup/kofa/browser/templates/configurationmanagepage.pt

    r11254 r13394  
    1818        </tbody>
    1919      </table>
     20      <br />
    2021      <div tal:condition="view/availableActions">
    2122        <span tal:repeat="action view/actions" tal:omit-tag="">
  • main/waeup.kofa/trunk/src/waeup/kofa/browser/tests/test_browser.py

    r13199 r13394  
    2222import tempfile
    2323import os
     24import grok
    2425from zc.async.testing import wait_for_result
    2526from zope.component import createObject, getUtility
     
    506507        self.browser.getControl(name="form.password").value = 'secret'
    507508        self.browser.getControl("Login").click()
    508         # Yeah, office logged in.
     509        # Yeah, officer logged in.
    509510        self.assertMatches(
    510511            '...You logged in...', self.browser.contents)
     
    531532        return
    532533
     534    def test_maintenance_mode(self):
     535        config = grok.getSite()['configuration']
     536        self.app['users'].addUser(
     537            'officer', 'secret', title='Bob Officer', email='bob@abcd.ng')
     538        self.browser.open('http://localhost/app/login')
     539        self.browser.getControl(name="form.login").value = 'officer'
     540        self.browser.getControl(name="form.password").value = 'secret'
     541        self.browser.getControl("Login").click()
     542        # Officer logged in.
     543        self.assertMatches('...You logged in...', self.browser.contents)
     544        self.assertTrue("Bob Officer" in self.browser.contents)
     545        # If maintenance mode is enabled, officer is immediately logged out.
     546        config.maintmode_enabled_by = u'any_user'
     547        self.assertRaises(
     548            Unauthorized, self.browser.open, 'http://localhost/app/faculties')
     549        self.browser.open('http://localhost/app/login')
     550        self.assertMatches(
     551            '...The portal is in maintenance mode...',
     552            self.browser.contents)
     553        # Officers really can't login if maintenance mode is enabled.
     554        self.browser.getControl(name="form.login").value = 'officer'
     555        self.browser.getControl(name="form.password").value = 'secret'
     556        self.browser.getControl("Login").click()
     557        # A second warning is raised.
     558        self.assertMatches(
     559            '...The portal is in maintenance mode. You can\'t login!...',
     560            self.browser.contents)
     561        # Offficer can login if s/he is the blocker.
     562        config.maintmode_enabled_by = u'officer'
     563        self.browser.getControl(name="form.login").value = 'officer'
     564        self.browser.getControl(name="form.password").value = 'secret'
     565        self.browser.getControl("Login").click()
     566        self.assertTrue('You logged in' in self.browser.contents)
     567        self.assertTrue('Logout' in self.browser.contents)
     568        return
     569
    533570    def test_sources_overview(self):
    534571        self.browser.open('http://localhost/app/sources')
  • main/waeup.kofa/trunk/src/waeup/kofa/browser/viewlets.py

    r13119 r13394  
    628628    text = _('View processed files')
    629629
     630class SwitchMaintMode(ActionButton):
     631    grok.context(IDataCenter)
     632    grok.require('waeup.importData')
     633    grok.view(DatacenterPage)
     634    grok.order(7)
     635    icon = 'actionicon_stop.png'
     636    target = 'switchmaintmode'
     637    text = _('Switch maintenance mode')
     638
     639    @property
     640    def onclick(self):
     641        if grok.getSite()['configuration'].maintmode_enabled_by:
     642            return
     643        return "return window.confirm(%s);" % _(
     644            "'In maintenance mode no other user can login, and "
     645            "already logged-in users will be automatically logged out. "
     646            "You will be the only logged-in user and you can safely start "
     647            "any import. Please wait a few seconds before starting the import "
     648            "so that all running processes are finished. "
     649            "If the import is done, maintenance mode will "
     650            "be automatically disabled. \\n\\n"
     651            "You really want to enable maintenance mode?'")
     652
    630653#
    631654# Primary navigation tabs (in upper left navigation bar)...
Note: See TracChangeset for help on using the changeset viewer.