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.
73 lines
2.3 KiB
73 lines
2.3 KiB
# -*- coding: utf-8 -*-
|
|
import random
|
|
from django.db import connection
|
|
|
|
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
|
|
|
|
|
|
def get_banner_by_params(banners_list, urls, params):
|
|
#print('START. NUMBER of queries = %d'%len(connection.queries))
|
|
thematic_banners = []
|
|
url_banners = []
|
|
|
|
for banner in banners_list:
|
|
#print('-------------------------')
|
|
#print('number of queries = %d'%len(connection.queries))
|
|
|
|
# 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:
|
|
thematic_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:
|
|
thematic_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:
|
|
url_banners.append(banner)
|
|
continue
|
|
print('-------------------------')
|
|
if thematic_banners:
|
|
return random.choice(thematic_banners)
|
|
if url_banners:
|
|
return random.choice(url_banners)
|
|
return None
|
|
|
|
#print('END. NUMBER of queries = %d'%len(connection.queries))
|
|
|