root / snf-astakos-app / astakos / im / templatetags / astakos_tags.py @ 279d6e51
History | View | Annotate | Download (2.2 kB)
1 |
from django import template |
---|---|
2 |
from django.core.urlresolvers import 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 |
|
19 |
@register.tag(name='display_messages') |
20 |
def display_messages(parser, token): |
21 |
return MessagesNode()
|
22 |
|
23 |
|
24 |
class DummyMessage(object): |
25 |
def __init__(self, type, msg): |
26 |
self.message = msg
|
27 |
self.tags = type |
28 |
|
29 |
def __repr__(self): |
30 |
return "%s: %s" % (self.tags, self.message) |
31 |
|
32 |
|
33 |
class MessagesNode(template.Node): |
34 |
|
35 |
def get_view_messages(self, context): |
36 |
messages = list(context['GLOBAL_MESSAGES']) |
37 |
try:
|
38 |
view = resolve(context['request'].get_full_path())[0] |
39 |
view_name = "%s.%s" % (view.__module__, view.func_name)
|
40 |
messages += context[MESSAGES_VIEWS_MAP.get(view_name)] |
41 |
return messages
|
42 |
except Exception, e: |
43 |
return messages
|
44 |
|
45 |
def render(self, context): |
46 |
if self not in context.render_context: |
47 |
messages = list(context['messages']) |
48 |
if context['EXTRA_MESSAGES_SET']: |
49 |
view_messages = self.get_view_messages(context)
|
50 |
for msg_object in view_messages: |
51 |
messages.append(DummyMessage(msg_object[0], msg_object[1])) |
52 |
|
53 |
if not messages: |
54 |
return "" |
55 |
|
56 |
cls = messages[-1].tags
|
57 |
content = '<div class="top-msg active %s">' % cls
|
58 |
for msg in messages: |
59 |
content += '<div class="msg %s">%s</div>' % (
|
60 |
msg.tags, msg.message) |
61 |
|
62 |
content += '<a href="#" title="close" class="close">X</a>'
|
63 |
content += '</div>'
|
64 |
context.render_context[self] = content
|
65 |
|
66 |
return context.render_context[self] |
67 |
|
68 |
|
69 |
@register.simple_tag
|
70 |
def olga(v): |
71 |
return v+'a' |