|
|
|
|
@ -1,8 +1,10 @@ |
|
|
|
|
# -*- 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 |
|
|
|
|
from .models import Banner, BannerGroup, URL |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def click(request, banner_id, key): |
|
|
|
|
@ -25,22 +27,100 @@ def get_client_ip(request): |
|
|
|
|
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', '/') |
|
|
|
|
theme = request.GET.get('theme') |
|
|
|
|
country = request.GET.get('country') |
|
|
|
|
city = request.GET.get('city') |
|
|
|
|
tag = request.GET.get('tag') |
|
|
|
|
ip = get_client_ip(request) |
|
|
|
|
params = {'url': url, |
|
|
|
|
'theme': theme, |
|
|
|
|
'tag': tag, |
|
|
|
|
'country': country, |
|
|
|
|
'city': city, |
|
|
|
|
'ip': ip} |
|
|
|
|
b = Banner.objects.get(id=1) |
|
|
|
|
result = [{'url': b.url, 'id': 'expo_b_%d'%b.id, 'is_html': b.html, |
|
|
|
|
'is_flash': b.flash, 'is_img': True, 'html': b.text, 'img': b.img.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') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|