-
Notifications
You must be signed in to change notification settings - Fork 2
/
newman_frontend_tags.py
70 lines (57 loc) · 2.18 KB
/
newman_frontend_tags.py
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
68
69
70
from django import template
from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from ella.newman import config as newman_config
from ella.core.models.publishable import HitCount
from ella.positions.models import Position
register = template.Library()
def get_newman_url(obj):
""" Assembles edit-object url for Newman admin. """
ct = ContentType.objects.get_for_model(obj)
url = '%(base)s#/%(app)s/%(model)s/%(pk)d/' % {
'base': newman_config.BASE_URL,
'app': ct.app_label,
'model': ct.model,
'pk': obj.pk
}
return url
@register.inclusion_tag('newman/tpl_tags/newman_frontend_admin.html', takes_context=True)
def newman_frontend_admin(context):
user = context['user']
vars = {}
if not user or not user.is_staff:
return vars
#vars['logout_url'] = reverse('newman:logout')
obj = context.get('object')
if 'gallery' in context:
obj = context.get('gallery')
placement = context.get('placement')
vars['user'] = user
vars['STATIC_URL'] = context.get('STATIC_URL')
vars['NEWMAN_MEDIA_URL'] = context.get('NEWMAN_MEDIA_URL')
vars['placement'] = placement
vars['category'] = context.get('category')
vars['newman_index_url'] = newman_config.BASE_URL
category = vars['category']
if not category or not category.pk:
return vars
from django.db.models import Q
import datetime
now = datetime.datetime.now()
lookup = (Q(active_from__isnull=True) | Q(active_from__lte=now)) & (Q(active_till__isnull=True) | Q(active_till__gt=now))
positions = Position.objects.filter(lookup, category=category.pk, disabled=False, target_id__isnull=False)
#print positions.query
vars['positions'] = positions
if obj:
vars['object'] = obj
vars['newman_object_url'] = get_newman_url(obj)
if placement:
vars['hitcount'] = HitCount.objects.get(placement=placement.pk)
return vars
logout_url = newman_config.BASE_URL + 'logout/'
class NewmanLogoutNode(template.Node):
def render(self, context):
return logout_url
@register.tag
def newman_frontend_logout(parser, token):
return NewmanLogoutNode()