Changeset 12433 for main/waeup.kofa


Ignore:
Timestamp:
9 Jan 2015, 16:06:44 (10 years ago)
Author:
Henrik Bettermann
Message:

Add rest2dict helper function.

Location:
main/waeup.kofa/trunk/src/waeup/kofa/utils
Files:
2 edited

Legend:

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

    r12393 r12433  
    832832                contents=text)
    833833    return elements
     834
     835def rest2dict(value=None,portal_language='en'):
     836    """Transforms a localized REST text string into a dictionary.
     837
     838    Different languages must be separated by `>>xy<<` whereas
     839    xy is the language code. Text parts without correct leading
     840    language separator - usually the first part has no language
     841    descriptor - are interpreted as texts in the portal's language.
     842    The latter can be configured in waeup.srp.utils.utils.IkobaUtils.
     843    """
     844    try:
     845        parts = value.split('>>')
     846    except:
     847        return {}
     848    elements = {}
     849    lang = portal_language
     850    for part in parts:
     851        if part[2:4] == u'<<':
     852            lang = str(part[0:2].lower())
     853            text = part[4:]
     854            elements[lang] = renderElement(u'div id="rest"',
     855                contents=ReST2HTML(text))
     856        else:
     857            text = part
     858            elements[lang] = renderElement(u'div id="rest"',
     859                contents=ReST2HTML(text))
     860    return elements
  • main/waeup.kofa/trunk/src/waeup/kofa/utils/tests/test_helpers.py

    r12231 r12433  
    595595            'de': u'<div id="html">Hallo Welt</div id="html">',
    596596            'en': u'<div id="html">Hello world</div id="html">'}
     597
     598
     599class Rest2dictTestCase(unittest.TestCase):
     600
     601    def test_rest2dict(self):
     602        assert helpers.rest2dict(None) == {}
     603        assert helpers.rest2dict() == {}
     604        assert helpers.rest2dict(9) == {}
     605        assert helpers.rest2dict('Hello world') == {
     606            'en': u'<div id="rest"><div class="document">\n\n\n<p>Hello world'
     607                   '</p>\n</div></div id="rest">'}
     608        assert helpers.rest2dict('Hello world>>de<<Hallo Welt') == {
     609            'de': u'<div id="rest"><div class="document">\n\n\n<p>Hallo Welt'
     610                   '</p>\n</div></div id="rest">',
     611            'en': u'<div id="rest"><div class="document">\n\n\n<p>Hello world'
     612                   '</p>\n</div></div id="rest">'}
    597613
    598614
     
    617633        GetMemInfoTestCase,
    618634        Html2dictTestCase,
     635        Rest2dictTestCase,
    619636        ]:
    620637        suite.addTests(
Note: See TracChangeset for help on using the changeset viewer.