root / snf-astakos-app / astakos / im / templatetags / astakos_tags.py @ 4f78c22c
History | View | Annotate | Download (2.2 kB)
1 |
from django import template |
---|---|
2 |
from django.core.urlresolvers import reverse, resolve |
3 |
from django.conf import settings |
4 |
|
5 |
register = template.Library() |
6 |
|
7 |
MESSAGES_VIEWS_MAP = getattr(settings, 'ASTAKOS_MESSAGES_VIEWS_MAP', { |
8 |
'astakos.im.views.index': 'LOGIN_MESSAGES', |
9 |
'astakos.im.views.logout': 'LOGIN_MESSAGES', |
10 |
'astakos.im.views.login': 'LOGIN_MESSAGES', |
11 |
'astakos.im.views.signup': 'SIGNUP_MESSAGES', |
12 |
'astakos.im.views.edit_profile': 'PROFILE_MESSAGES', |
13 |
'astakos.im.views.change_password': 'PROFILE_MESSAGES', |
14 |
'astakos.im.views.invite': 'PROFILE_MESSAGES', |
15 |
'astakos.im.views.feedback': 'PROFILE_MESSAGES', |
16 |
}) |
17 |
|
18 |
@register.tag(name='display_messages') |
19 |
def display_messages(parser, token): |
20 |
return MessagesNode()
|
21 |
|
22 |
class DummyMessage(object): |
23 |
def __init__(self, type, msg): |
24 |
self.message = msg
|
25 |
self.tags = type |
26 |
|
27 |
def __repr__(self): |
28 |
return "%s: %s" % (self.tags, self.message) |
29 |
|
30 |
class MessagesNode(template.Node): |
31 |
|
32 |
def get_view_messages(self, context): |
33 |
messages = list(context['GLOBAL_MESSAGES']) |
34 |
try:
|
35 |
view = resolve(context['request'].get_full_path())[0] |
36 |
view_name = "%s.%s" % (view.__module__, view.func_name)
|
37 |
messages += context[MESSAGES_VIEWS_MAP.get(view_name)] |
38 |
return messages
|
39 |
except Exception, e: |
40 |
return messages
|
41 |
|
42 |
def render(self, context): |
43 |
if self not in context.render_context: |
44 |
messages = list(context['messages']) |
45 |
if context['EXTRA_MESSAGES_SET']: |
46 |
view_messages = self.get_view_messages(context)
|
47 |
for msg_object in view_messages: |
48 |
messages.append(DummyMessage(msg_object[0], msg_object[1])) |
49 |
|
50 |
if not messages: |
51 |
return "" |
52 |
|
53 |
cls = messages[-1].tags
|
54 |
content = '<div class="top-msg active %s">' % cls
|
55 |
for msg in messages: |
56 |
content += '<div class="msg %s">%s</div>' % (msg.tags, msg.message)
|
57 |
|
58 |
content += '<a href="#" title="close" class="close">X</a>'
|
59 |
content += '</div>'
|
60 |
context.render_context[self] = content
|
61 |
|
62 |
return context.render_context[self] |