You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
126 lines
4.0 KiB
126 lines
4.0 KiB
# -*- coding: utf-8 -*-
|
|
import json
|
|
import re
|
|
import random
|
|
from django.http import HttpResponse
|
|
from django.shortcuts import redirect, get_object_or_404
|
|
from .models import Banner, BannerGroup, URL
|
|
|
|
|
|
def click(request, banner_id, key):
|
|
banner = get_object_or_404(Banner, pk=banner_id)
|
|
banner.log(request, 2, key)
|
|
return redirect(banner.url)
|
|
|
|
|
|
def view(request, banner_id, key):
|
|
banner = get_object_or_404(Banner, pk=banner_id)
|
|
banner.log(request, 1, key)
|
|
return redirect(banner.img.url)
|
|
|
|
|
|
def get_client_ip(request):
|
|
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
|
|
if x_forwarded_for:
|
|
ip = x_forwarded_for.split(',')[0]
|
|
else:
|
|
ip = request.META.get('REMOTE_ADDR')
|
|
return ip
|
|
|
|
def get_by_sort(banner_list):
|
|
max_sort = 0
|
|
for banner in banner_list:
|
|
sort = banner.sort
|
|
if sort > max_sort:
|
|
max_sort = sort
|
|
result = [banner for banner in banner_list if banner.sort == max_sort]
|
|
return result
|
|
|
|
from django.db import connection
|
|
def get_banner_by_params(banners_list, urls, params):
|
|
print('START. NUMBER of queries = %d'%len(connection.queries))
|
|
good_banners = []
|
|
|
|
for banner in banners_list:
|
|
print('-------------------------')
|
|
print('number of queries = %d'%len(connection.queries))
|
|
print(banner)
|
|
# check by theme
|
|
banner_theme_ids = [str(theme.id) for theme in banner.theme.all()]
|
|
print('number of queries = %d'%len(connection.queries))
|
|
|
|
if banner_theme_ids:
|
|
if params.get('theme'):
|
|
theme = params['theme']
|
|
if theme in banner_theme_ids:
|
|
good_banners.append(banner)
|
|
continue
|
|
# check by country
|
|
banner_country_ids = [str(country.id) for country in banner.country.all()]
|
|
print('number of queries = %d'%len(connection.queries))
|
|
if banner_country_ids:
|
|
if params.get('country'):
|
|
|
|
country = params['country']
|
|
if country in banner_country_ids:
|
|
good_banners.append(banner)
|
|
continue
|
|
|
|
# check by url
|
|
if urls:
|
|
banner_urls = banner.urls.all()
|
|
print('number of queries = %d'%len(connection.queries))
|
|
|
|
if banner_urls:
|
|
|
|
banner_urls = set(banner_urls)
|
|
common_urls = set(urls).intersection(banner_urls)
|
|
|
|
if common_urls:
|
|
good_banners.append(banner)
|
|
continue
|
|
print('-------------------------')
|
|
good_banners = get_by_sort(good_banners)
|
|
|
|
print('END. NUMBER of queries = %d'%len(connection.queries))
|
|
|
|
|
|
if good_banners:
|
|
return random.choice(good_banners)
|
|
return []
|
|
|
|
def get_banners(request):
|
|
# get urls by current url
|
|
url = request.GET.get('url', '/')
|
|
urls = URL.cached.all()
|
|
good_urls = []
|
|
for u in urls:
|
|
if u.regex:
|
|
url_re = re.compile(u.url)
|
|
if url_re.findall(url):
|
|
good_urls.append(u)
|
|
elif url == u.url:
|
|
good_urls.append(u)
|
|
# fill parameters dict
|
|
params = {'theme': request.GET.get('theme'),
|
|
'tag': request.GET.get('tag'),
|
|
'country': request.GET.get('country'),
|
|
'city': request.GET.get('city'),
|
|
'ip': get_client_ip(request)}
|
|
|
|
group_banners = BannerGroup.cached.group_banners()
|
|
result = []
|
|
for group, banners in group_banners.iteritems():
|
|
banner = get_banner_by_params(banners, good_urls, params)
|
|
result.append({'id': group,
|
|
'url': banner.url,
|
|
'is_html': banner.html,
|
|
'is_flash': banner.flash,
|
|
'is_img': True,
|
|
'html': banner.text,
|
|
'img': banner.img.url
|
|
})
|
|
|
|
return HttpResponse(json.dumps(result, indent=4), content_type='application/json')
|
|
|
|
|
|
|