coding utf-8 from django import template register template Library fro

 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# -*- coding: utf-8
from django import template
register = template.Library()
from django.contrib.contenttypes.models import ContentType
@register.tag
def permissions(parser, token):
user_field = 'user'
self_protect = ''
try:
length = len(token.split_contents())
if length == 3:
tag_name, instance, action = token.split_contents()
elif length == 4:
tag_name, instance, action, user_field = token.split_contents()
elif length == 5:
tag_name, instance, action, user_field, self_protect = token.split_contents()
except ValueError:
raise template.TemplateSyntaxError, "%r tag requires minimum of two arguments" % token.contents.split()[0]
if not (action[0] == action[-1] and action[0] in ('"', "'")):
raise template.TemplateSyntaxError, "%r tag's argument should be in quotes" % tag_name
nodelist = parser.parse(('endpermissions',))
parser.delete_first_token()
return PermissionsNode(nodelist, instance, action[1:-1], user_field[1:-1], self_protect[1:-1])
class PermissionsNode(template.Node):
def __init__(self, nodelist, instance, action, user_field, self_protect):
self.nodelist = nodelist
self.instance = template.Variable(instance)
self.action = action
self.self_protect = self_protect
self.user_field = user_field
def render(self, context):
print "zalupa"
output = ''
if self.action == 'delete' and context['user'].is_authenticated():
instance = self.instance.resolve(context)
content_type = ContentType.objects.get_for_model(instance.__class__)
if instance.user == context['user']:
output = self.nodelist.render(context)
elif content_type in context['user'].moderator.sections.all() or context['user'].is_staff:
output = self.nodelist.render(context)
if self.action == 'edit' and context['user'].is_authenticated():
instance = self.instance.resolve(context)
content_type = ContentType.objects.get_for_model(instance.__class__)
if instance.user == context['user']:
output = self.nodelist.render(context)
elif content_type in context['user'].moderator.sections.all() or context['user'].is_staff:
output = self.nodelist.render(context)
if self.action == 'approve' and context['user'].is_authenticated():
instance = self.instance.resolve(context)
content_type = ContentType.objects.get_for_model(instance.__class__)
if content_type in context['user'].moderator.sections.all() or context['user'].is_staff:
output = self.nodelist.render(context)
if self.action == 'create' and context['user'].is_authenticated():
print "hello world"
instance = self.instance.resolve(context)
content_type = ContentType.objects.get_for_model(instance.__class__)
if self.self_protect and not getattr(instance, user_field) == context['user']:
output = self.nodelist.render(context)
elif content_type in context['user'].moderator.sections.all() or context['user'].is_staff:
output = self.nodelist.render(context)
return output