コード例 #1
0
ファイル: views.py プロジェクト: kant/freesound
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})
コード例 #2
0
ファイル: views.py プロジェクト: MTG/freesound
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})