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.
 
 
 
 
 
 

185 lines
6.0 KiB

# -*- coding: utf-8 -*-
import re
import random
import datetime
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_referer_view(request, default=None):
'''
Return the referer view of the current request
Example:
def some_view(request):
...
referer_view = get_referer_view(request)
return HttpResponseRedirect(referer_view, '/accounts/login/')
'''
# if the user typed the url directly in the browser's address bar
referer = request.META.get('HTTP_REFERER')
if not referer:
return default
# remove the protocol and split the url at the slashes
referer = re.sub('^https?:\/\/', '', referer).split('/')
if referer[0] != request.META.get('SERVER_NAME'):
return default
# add the slash at the relative path's view and finished
referer = u'/' + u'/'.join(referer[1:])
return referer
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 set_cookie(response, key, value, days_expire = 7):
if days_expire is None:
max_age = 365 * 24 * 60 * 60 #one year
else:
max_age = days_expire * 24 * 60 * 60
expires = datetime.datetime.strftime(datetime.datetime.utcnow() + datetime.timedelta(seconds=max_age), "%a, %d-%b-%Y %H:%M:%S GMT")
response.set_cookie(key, value, max_age=max_age, expires=expires)
return response
def get_banner_by_params(banners_list, urls, params, request):
thematic_banners = []
url_banners = []
for banner in banners_list:
# check popups
if banner.popup:
cookie = request.COOKIES.get(banner.cookie)
if cookie:
continue
# check by theme
banner_theme_ids = [str(theme.id) for theme in banner.theme.all()]
if banner_theme_ids:
if params.get('theme'):
theme = params['theme']
flag = False
for th in theme:
if th in banner_theme_ids:
flag = True
if flag:
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
if thematic_banners:
result = thematic_banners
elif url_banners:
result = url_banners
else:
result = []
if result:
sort_result = get_by_sort(result)
# return random.choice(sort_result)
return random_choice_banners(sort_result)
else:
return None
def random_choice_banners(banners):
'''
Include weight of customers model.
'''
choice_from = {}
for i, banner in enumerate(banners):
key = 'customer{pk}'.format(pk=banner.customer_id) if banner.customer_id else i
choice_from.setdefault(key, []).append(banner)
pre_choiced = random.choice(choice_from.values())
return random.choice(pre_choiced)
def get_top_events(tops, params, request):
catalog = params.get('catalog')
country = params.get('country', '')
theme = params.get('theme', [])
city = params.get('city', '')
tag = params.get('tag', '')
month = params.get('month', '')
year = request.GET.get('year', '')
catalog_tops = [item for item in tops if item.catalog == catalog]
good_tops = []
for top in catalog_tops:
if catalog == 'places' and not country and not city and top.base_catalog:
good_tops.append(top)
continue
country_ids = [str(item.id) for item in top.country.all()]
theme_ids = [str(item.id) for item in top.theme.all()]
city_ids = [str(item.id) for item in top.cities.all()]
excluded_tags_ids = [str(item.id) for item in top.excluded_tags.all()]
excluded_cities_ids = [str(item.id) for item in top.excluded_cities.all()]
if not any([country_ids, theme_ids, city_ids]) and not catalog == 'places':
# universal top
good_tops.append(top)
continue
# check country
if country in country_ids and city not in excluded_cities_ids :
good_tops.append(top)
continue
# check city
if all([city, city in city_ids, not theme]):
# check for top of month or top of year
if (month and not month in top.months) or (not month and year and year != str(top.years)):
continue
good_tops.append(top)
continue
# check theme
if tag in excluded_tags_ids:
continue
flag = False
for th in theme:
if th in theme_ids:
flag = True
continue
if flag:
good_tops.append(top)
sorted_top = sorted(good_tops, key=lambda x: x.position)
events = []
for top in sorted_top:
event = top.get_event()
if event:
top.link.log(request, 1)
events.append(event)
return events