Changeset 7511


Ignore:
Timestamp:
25 Jan 2012, 14:25:17 (13 years ago)
Author:
uli
Message:

Make formatted_text a bit smarter regarding to encodings. It now
returns a unicode string instead of raw strings. I stumbled
over some broken PDF generations when names of students contained
umlauts. Hope, this will fix this without breaking other things. At
least reportlab is able to cope with unicode strings.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.sirp/trunk/src/waeup/sirp/students/utils.py

    r7369 r7511  
    5555    return tag1 + '%s:</font>'
    5656
    57 def formatted_text(color=FONT_COLOR, size=FONT_SIZE):
    58     tag1 ='<font color=%s size=%d>' % (color, size)
    59     return tag1 + '%s</font>'
     57def formatted_text(text, color=FONT_COLOR, size=FONT_SIZE):
     58    """Turn `text`, `color` and `size` into an HTML snippet.
     59
     60    The snippet is suitable for use with reportlab and generating PDFs.
     61    Wraps the `text` into a ``<font>`` tag with passed attributes.
     62
     63    Also non-strings are converted. Raw strings are expected to be
     64    utf-8 encoded (usually the case for widgets etc.).
     65
     66    The returned snippet is unicode type.
     67    """
     68    if not isinstance(text, unicode):
     69        if isinstance(text, basestring):
     70            text = text.decode('utf-8')
     71        else:
     72            text = unicode(text)
     73    tag1 = u'<font color="%s" size="%d">' % (color, size)
     74    return tag1 + u'%s</font>' % text
    6075
    6176def generate_student_id(students,letter):
     
    104119        f_label = formatted_label() % widget.label.strip()
    105120        f_label = Paragraph(f_label, style["Normal"])
    106         f_text = formatted_text() % widget()
     121        f_text = formatted_text(widget())
    107122        f_text = Paragraph(f_text, style["Normal"])
    108123        data_right.append([f_label,f_text])
     
    120135    style = getSampleStyleSheet()
    121136    for element in tableheader:
    122         field = formatted_text(color='white') % element[0]
     137        field = formatted_text(element[0], color='white')
    123138        field = Paragraph(field, style["Normal"])
    124139        line.append(field)
     
    127142        line = []
    128143        for element in tableheader:
    129               field = formatted_text() % str(getattr(ticket,element[1],u' '))
     144              field = formatted_text(getattr(ticket,element[1],u' '))
    130145              field = Paragraph(field, style["Normal"])
    131146              line.append(field)
     
    261276            f_label = formatted_label() % widget.label.strip()
    262277            f_label = Paragraph(f_label, style["Normal"])
    263             f_text = formatted_text() % str(widget())
     278            f_text = formatted_text(widget())
    264279            f_text = Paragraph(f_text, style["Normal"])
    265280            data.append([f_label,f_text])
     
    291306                    data.append([f_label,doc_img])
    292307                else:
    293                     f_text = formatted_text() % viewlet.title
     308                    f_text = formatted_text(viewlet.title)
    294309                    f_text = Paragraph(f_text, style["Normal"])
    295310                    data.append([f_label,f_text])
Note: See TracChangeset for help on using the changeset viewer.