From a4d63ec04e9ee357e5b2d6035868d31001e1efae Mon Sep 17 00:00:00 2001 From: Alexander Burdeinyi Date: Fri, 9 Dec 2016 14:36:41 +0200 Subject: [PATCH] m --- settings/templatetags/tempalte_tags.py | 119 +++++++++++++------------ 1 file changed, 63 insertions(+), 56 deletions(-) diff --git a/settings/templatetags/tempalte_tags.py b/settings/templatetags/tempalte_tags.py index d93134a7..aec8ab0a 100644 --- a/settings/templatetags/tempalte_tags.py +++ b/settings/templatetags/tempalte_tags.py @@ -1,11 +1,15 @@ # -*- coding: utf-8 -*- import datetime +from logging import getLogger + from django import template from django.utils.translation import ugettext as _ from expobanner.models import BannerGroup, URL from expobanner.utils import get_banner_by_params, get_client_ip, get_referer_view + +log = getLogger('mail') register = template.Library() @@ -98,65 +102,68 @@ def set_var(parser, token): @register.inclusion_tag('client/includes/banners/top_head_banner.html', takes_context=True) def get_top_banner(context): # fill parameters dict - request = context.get('request') - - params = {'theme': context.get('themes', []), - 'tag': context.get('tag', []), - 'country': context.get('country', []), - 'city': context.get('city', []), - 'ip': get_client_ip(request)} - good_urls = [] - urls = URL.cached.all() - url = get_referer_view(request, default='/') - for u in urls: - if u.regex: - try: - url_re = re.compile(u.url) - except: - continue - if url_re.findall(url): - good_urls.append(u) - elif url == u.url: - good_urls.append(u) - - group_banners = BannerGroup.cached.group_banners() - result = {} - cookie = None - places = ['top_page_banner'] - for group, banners in group_banners.iteritems(): - if group not in places: - continue - banner = get_banner_by_params(banners, good_urls, params, request) - # import pdb; pdb.set_trace() # on this page there is no such group - if banner: - if banner.html: - text = banner.text - img = '' - alt = '' - is_img = False - else: - text = '' + try: + request = context.get('request') + + params = {'theme': context.get('themes', []), + 'tag': context.get('tag', []), + 'country': context.get('country', []), + 'city': context.get('city', []), + 'ip': get_client_ip(request)} + good_urls = [] + urls = URL.cached.all() + url = get_referer_view(request, default='/') + for u in urls: + if u.regex: try: - img = banner.img.url - except ValueError: + url_re = re.compile(u.url) + except: continue - alt = banner.alt - is_img = True - result = {'id': group, - 'url': banner.get_click_link(), - 'is_html': banner.html, - 'is_flash': banner.flash, - 'is_img': is_img, - 'is_popup': banner.popup, - 'img': img, - 'alt': alt, - 'text': text - } - if banner.popup: - cookie = banner.cookie - # add view log - banner.log(request, 1) + if url_re.findall(url): + good_urls.append(u) + elif url == u.url: + good_urls.append(u) + group_banners = BannerGroup.cached.group_banners() + result = {} + cookie = None + places = ['top_page_banner'] + for group, banners in group_banners.iteritems(): + if group not in places: + continue + banner = get_banner_by_params(banners, good_urls, params, request) + # import pdb; pdb.set_trace() # on this page there is no such group + if banner: + if banner.html: + text = banner.text + img = '' + alt = '' + is_img = False + else: + text = '' + try: + img = banner.img.url + except (ValueError, ) as e: + log.log(e) + continue + alt = banner.alt + is_img = True + result = {'id': group, + 'url': banner.get_click_link(), + 'is_html': banner.html, + 'is_flash': banner.flash, + 'is_img': is_img, + 'is_popup': banner.popup, + 'img': img, + 'alt': alt, + 'text': text + } + if banner.popup: + cookie = banner.cookie + # add view log + banner.log(request, 1) + except (Exception, ) as e: + log.log(e) return result