def after_download_modal(request): """ This view checks if a modal should be shown after the user has downloaded a sound, and returns either the contents of the modal if needed. """ response_content = None # Default content of the response set to None (no modal) sound_name = request.GET.get( 'sound_name', 'this sound') # Gets some data sent by the client def modal_shown_timestamps_cache_key(user): return 'modal_shown_timestamps_donations_shown_%i' % user.id if DonationsModalSettings.get_donation_modal_settings().enabled: # Get timestamps of last times modal was shown from cache modal_shown_timestamps = cache.get( modal_shown_timestamps_cache_key(request.user), []) # Iterate over timestamps, keep only the ones in last 24 hours and do the counting modal_shown_timestamps = [ item for item in modal_shown_timestamps if item > (time.time() - 24 * 3600) ] if should_suggest_donation(request.user, len(modal_shown_timestamps)): logger.info('Showing after download donate modal (%s)' % json.dumps({'user_id': request.user.id})) modal_shown_timestamps.append(time.time()) cache.set(modal_shown_timestamps_cache_key(request.user), modal_shown_timestamps) template = loader.get_template( 'sounds/after_download_modal_donation.html') response_content = template.render({'sound_name': sound_name}) return JsonResponse({'content': response_content})
def after_download_modal(request): """ This view checks if a modal should be shown after the user has downloaded a sound, and returns either the contents of the modal if needed. """ response_content = None # Default content of the response set to None (no modal) sound_name = request.GET.get('sound_name', 'this sound') # Gets some data sent by the client def modal_shown_timestamps_cache_key(user): return 'modal_shown_timestamps_donations_shown_%i' % user.id if DonationsModalSettings.get_donation_modal_settings().enabled: # Get timestamps of last times modal was shown from cache modal_shown_timestamps = cache.get(modal_shown_timestamps_cache_key(request.user), []) # Iterate over timestamps, keep only the ones in last 24 hours and do the counting modal_shown_timestamps = [item for item in modal_shown_timestamps if item > (time.time() - 24 * 3600)] if should_suggest_donation(request.user, len(modal_shown_timestamps)): logger.info('Showing after download donate modal (%s)' % json.dumps({'user_id': request.user.id})) modal_shown_timestamps.append(time.time()) cache.set(modal_shown_timestamps_cache_key(request.user), modal_shown_timestamps, 60 * 60 * 24) # 24 lifetime cache template = loader.get_template('sounds/after_download_modal_donation.html') response_content = template.render({'sound_name': sound_name}) return JsonResponse({'content': response_content})
def should_suggest_donation(user, times_shown_in_last_day): """ This method indicates when we should display the donation modal to the user. This will be based on 3 settings indicating how many days after a donation we show the modal again, after how many downloads we display the modal and for how long. The modal will be shown a maximum number of times per day. """ donation_modal_settings = DonationsModalSettings.get_donation_modal_settings( ) if times_shown_in_last_day >= donation_modal_settings.max_times_display_a_day: # If modal has been shown more than settings.DONATION_MODAL_DISPLAY_TIMES_DAY times, don't show it again today return False if donation_modal_settings.never_show_modal_to_uploaders: if user.profile.num_sounds > 0: # Never show modal to users that have uploaded sounds return False donation_period = datetime.datetime.now() - datetime.timedelta( days=donation_modal_settings.days_after_donation) last_donation = user.donation_set.order_by('created').last() if not last_donation or last_donation.created < donation_period: # If there has never been a donation or last donation is older than settings.DONATION_MODAL_DAYS_AFTER_DONATION, # check if the number of downloads in the last settings.DONATION_MODAL_DOWNLOAD_DAYS days if bigger than # settings.DONATION_MODAL_DOWNLOADS_IN_PERIOD. If that is the case, show the modal. num_downloads_in_period = Download.objects.filter( user=user, created__gt=datetime.datetime.now() - datetime.timedelta( days=donation_modal_settings.download_days)).count() if num_downloads_in_period > donation_modal_settings.downloads_in_period: if random.random() <= donation_modal_settings.display_probability: return True return False
def should_suggest_donation(user, times_shown_in_last_day): """ This method indicates when we should display the donation modal to the user. This will be based on 3 settings indicating how many days after a donation we show the modal again, after how many downloads we display the modal and for how long. The modal will be shown a maximum number of times per day. """ donation_modal_settings = DonationsModalSettings.get_donation_modal_settings() if times_shown_in_last_day >= donation_modal_settings.max_times_display_a_day: # If modal has been shown more than settings.DONATION_MODAL_DISPLAY_TIMES_DAY times, don't show it again today return False if donation_modal_settings.never_show_modal_to_uploaders: if user.profile.num_sounds > 0: # Never show modal to users that have uploaded sounds return False donation_period = datetime.datetime.now() - datetime.timedelta(days=donation_modal_settings.days_after_donation) last_donation = user.donation_set.order_by('created').last() if not last_donation or last_donation.created < donation_period: # If there has never been a donation or last donation is older than settings.DONATION_MODAL_DAYS_AFTER_DONATION, # check if the number of downloads in the last settings.DONATION_MODAL_DOWNLOAD_DAYS days if bigger than # settings.DONATION_MODAL_DOWNLOADS_IN_PERIOD. If that is the case, show the modal. num_sound_downloads = Download.objects.filter( user=user, created__gt=datetime.datetime.now() - datetime.timedelta(days=donation_modal_settings.download_days) ).count() num_pack_downloads = PackDownload.objects.filter( user=user, created__gt=datetime.datetime.now() - datetime.timedelta(days=donation_modal_settings.download_days) ).count() num_downloads_in_period = num_sound_downloads + num_pack_downloads if num_downloads_in_period > donation_modal_settings.downloads_in_period: if random.random() <= donation_modal_settings.display_probability: return True return False