diff options
Diffstat (limited to 'gentoo_ads')
-rw-r--r-- | gentoo_ads/ads/views.py | 26 | ||||
-rw-r--r-- | gentoo_ads/example_settings.py | 8 |
2 files changed, 30 insertions, 4 deletions
diff --git a/gentoo_ads/ads/views.py b/gentoo_ads/ads/views.py index 3ec28d9..1fb1bcd 100644 --- a/gentoo_ads/ads/views.py +++ b/gentoo_ads/ads/views.py @@ -8,7 +8,26 @@ import logging def serve_ads(request): sample = _weighted_random_sample(settings.ADS_STRUCT) - return render_to_response('ads.html', {'ads': sample, 'MEDIA_URL': settings.MEDIA_URL,}) + ads = [_trans_ad(a, request.META['HTTP_ACCEPT_LANGUAGE'].split(',')[0]) for a in sample] + return render_to_response('ads.html', {'ads': ads, 'MEDIA_URL': settings.MEDIA_URL,}) + +def _trans_ad(ad, lang): + + for k in settings.TRANS_KEYS: + if k not in ad: + continue + if isinstance(ad[k], dict): + if lang in ad[k]: + ad[k] = ad[k][lang] + + elif lang.split('-')[0] in ad[k]: + s = lang.split('-')[0] + ad[k] = ad[k][s] + + else: + ad[k] = ad[k][settings.DEFAULT_ADS_LANG] + + return ad def _weighted_random_sample(ads): ads_out = [] @@ -24,10 +43,11 @@ def _weighted_random_sample(ads): cur_total += ad['weight'] if r < cur_total: - ads_out.append(ad) + ads_out.append(ad.copy()) ads = ads[:ad_i] + ads[ad_i + 1:] break ads_log_data_message = ','.join([a['name'] for a in ads_out]) logging.info(ads_log_data_message) - return ads_out
\ No newline at end of file + return ads_out + diff --git a/gentoo_ads/example_settings.py b/gentoo_ads/example_settings.py index 47bda69..d2ef621 100644 --- a/gentoo_ads/example_settings.py +++ b/gentoo_ads/example_settings.py @@ -27,7 +27,7 @@ CONFIG_PATH = '/some/path/towards/the/file/gentoo-ads/example_ads.py' ads_module = load_source('ads_module', CONFIG_PATH,) ## sets the number of ads to be displayed -ADS_LENGTH = 6 +ADS_LENGTH = 3 ## why is this not above ADS_LENGTH? ## list of dictionaries we use in the view, i,e, the advertisements. @@ -37,6 +37,12 @@ ADS_STRUCT = ads_module.ads ## also eliminating the `import os` above ADS_IMAGES_DIR = os.path.join(os.path.dirname(CONFIG_PATH), 'images') +## These are the translateable keys in the ads_struct. +TRANS_KEYS = ('html', 'title', 'text') + +#default language for ads +DEFAULT_ADS_LANG = 'en' + DEBUG = True TEMPLATE_DEBUG = DEBUG |