caldf
caldf

Reputation: 71

Django messages formatting

does anyone understand this code and how it would be changed to show "sender wrote : message" on the same line instead of having it split on different lines. As you see in the image below. I really dont understand why they've gone for this formatting.

enter image description here

Code in utils.py

def format_quote(sender, body):
    """
    Wraps text at 55 chars and prepends each
    line with `> `.
    Used for quoting messages in replies.
    """
    lines = wrap(body, 55).split('\n')
    for i, line in enumerate(lines):
        lines[i] = "> %s" % line
    quote = '\n'.join(lines)
    return ugettext(u"%(sender)s wrote:\n%(body)s") % {
        'sender': sender,
        'body': quote
    }

this is the messaging app im using: https://github.com/arneb/django-messages

It just look horrendous as it is and i can figure out how to make it look nicer, any help would be hugely appreciated!

Upvotes: 0

Views: 468

Answers (1)

wpedrak
wpedrak

Reputation: 687

This piece of code just takes some text, and name of sender:

  1. wraps it (it tries to add some '\n to make rather column of text than line)
  2. makes list of lines
  3. prepend '> ' to every line
  4. makes text from list
  5. prepends 'someone wrote:\n' to this text

some copypaste from python:

>>> message = '1234567890 ' * 7
>>> message
'1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 '
>>> sender = 'john'
>>> sender
'john'
>>> wrap(message, 55)
'1234567890 1234567890 1234567890 1234567890 1234567890\n1234567890 1234567890 '

notice \n

>>> lines = wrap(message, 55).split('\n')
>>> lines
['1234567890 1234567890 1234567890 1234567890 1234567890', '1234567890 1234567890 ']
>>> for i, line in enumerate(lines):
...         lines[i] = "> %s" % line
...
>>> lines
['> 1234567890 1234567890 1234567890 1234567890 1234567890', '> 1234567890 1234567890 ']
>>> quote = '\n'.join(lines)
>>> quote
'> 1234567890 1234567890 1234567890 1234567890 1234567890\n> 1234567890 1234567890 '
>>> ret = u"%(sender)s wrote:\n%(body)s" % {
...         'sender': sender,
...         'body': quote
...         }
>>> print(ret)
john wrote:
> 1234567890 1234567890 1234567890 1234567890 1234567890
> 1234567890 1234567890

I think this code isn't problem itself. Probably function is called bunch of times producing such ugly output.

Upvotes: 1

Related Questions