Changeset 10605


Ignore:
Timestamp:
10 Sep 2013, 14:24:31 (11 years ago)
Author:
uli
Message:

Add a smarter message box handling.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.cas/trunk/waeup/cas/server.py

    r10602 r10605  
    44import os
    55import random
     6import re
    67import time
    78try:
     
    2829            'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    2930            '01234567789-')
     31
     32#: A regular expression that matches a div tag around a MSG_TEXT
     33RE_MSG_TAG = re.compile('\<div id="msg".*MSG_TEXT[^<]*</div>', re.DOTALL)
    3034
    3135
     
    233237    # show logged-in screen
    234238    html = get_template('login_successful.html')
    235     html = html.replace('MSG_TEXT', msg)
     239    html = set_message(msg, html)
    236240    resp = Response(html)
    237241    if not sso:
    238242        resp = set_session_cookie(db, resp)
    239243    return resp
     244
     245
     246def set_message(msg, html):
     247    """Insert a message box in html template.
     248
     249    If the message is empty, not only the string `MSG_TEXT` is removed
     250    from `html`, but also any encapsulating ``<div>`` tag with id
     251    ``msg``.
     252
     253    This makes it possible to give message boxes certain additional
     254    styles that will not show up if there is no message to display.
     255    """
     256    if not msg:
     257        if not '<div id="msg"' in html:
     258            return html.replace('MSG_TEXT', '')
     259        return RE_MSG_TAG.sub('', html)
     260    return html.replace('MSG_TEXT', msg)
    240261
    241262
     
    325346        html = html.replace('LT_VALUE', lt.ticket)
    326347        html = html.replace('SERVICE_FIELD_VALUE', service_field)
    327         html = html.replace('MSG_TEXT', msg)
     348        html = set_message(msg, html)
    328349        return Response(html)
    329350
Note: See TracChangeset for help on using the changeset viewer.