Login

Severity codes in user messages

Author:
cconnell
Posted:
January 17, 2009
Language:
Python
Version:
1.0
Score:
0 (after 0 ratings)

This is the code for a template tag. Put this code in your template to render your messages:

{% for message in messages %}
  {% render_user_message message %}
{% endfor %}

When you're adding a message to the user's message set, follow these rules: If you want a message to appear as an error, append "0001" to the end of it. To appear as a notice, append "0002" to it. To appear as a happy message, appear "0000" to it. If no code is present, it will default to displaying as an error. This makes use of the classes "error", "notice", and "success", so you need to define these in your CSS.

For help with custom template tags, see the Django docs

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from django import template
from django.template import Variable

register = template.Library()

class UserMessageNode(template.Node):
  """The template processor uses this to hold a user message"""
  def __init__(self, message):
    self.message = Variable(message)

  def render(self, context):
    try:
      message = self.message.resolve(context)
      if message[-4:].isdigit():
        error_code = message[-4:]
        message = message[:-4]
      else:
        error_code = ''

      if error_code == '0000':
        return u'<div class="success">' + message + u'</div>'
      if error_code == '0001':
        return u'<div class="error">' + message + u'</div>'
      if error_code == '0002':
        return u'<div class="notice">' + message + u'</div>'
      else:
        return u'<div class="error">' + message + u'</div>'

    except template.VariableDoesNotExist:
      return ''

@register.tag(name='render_user_message')
def render_user_message(parser, token):
  """A tag that renders a user message differently depending on the severity of it."""
  try:
    message = token.split_contents()[1]
  except ValueError:
    raise template.TemplateSyntaxError, "%r tag requires a single argument" % token.contents.split()[0]
  return UserMessageNode(message)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

david_bgk (on January 19, 2009):

Why do you use those cryptic error codes instead of prepended SUCCESS:, ERROR: and so on?

#

cconnell (on February 3, 2009):

Using the numbers makes it easy to know which characters are the error code, since there are always 4 characters plus the two brackets around them. Using that knowledge I just count back from the end of the string. I suppose one could use string codes, but I was being a little lazy.

#

willhardy (on February 10, 2009):

Ahh, I said the same thing as david_bgk on your blog, but I'll post here too. Strings are a good idea, david_bgk's uppercase chars are a better ide... your test could then use isupper()

Also, it would be more "pythonic" to use a dict for the CSS class mapping instead of a string of if elif elif elif statements.

Eg

CODES = {'ERROR': 'error', 'SUCCESS': 'success', 'NOTICE': 'notice' }
MARKUP = u'<div class="%s">%s</div>'

message_part, error_code = message.strip().rsplit(' ',1)

try:
  return MARKUP % (CODES.get(error_code), message_part)
except KeyError:
  return MARKUP % ('error', message)

(NB You'll have to cover the possible ValueError from the rsplit if there is no space in message :-)

#

Please login first before commenting.