def pack_download(request, username, pack_id): if not request.user.is_authenticated: return HttpResponseRedirect('%s?next=%s' % (reverse("accounts-login"), reverse("pack", args=[username, pack_id]))) pack = get_object_or_404(Pack, id=pack_id) if pack.user.username.lower() != username.lower(): raise Http404 if 'HTTP_RANGE' not in request.META: ''' Download managers and some browsers use the range header to download files in multiple parts. We have observed that all clients first make a GET with no range header (to get the file length) and then make multiple other requests. We ignore all requests that have range header because we assume that a first query has already been made. We additionally guard against users clicking on download multiple times by storing a sentinel in the cache for 5 minutes. ''' cache_key = 'pdwn_%s_%d' % (pack_id, request.user.id) if cache.get(cache_key, None) is None: pd = PackDownload.objects.create(user=request.user, pack=pack) pds = [] for sound in pack.sounds.all(): pds.append(PackDownloadSound(sound=sound, license=sound.license, pack_download=pd)) PackDownloadSound.objects.bulk_create(pds) cache.set(cache_key, True, 60 * 5) # Don't save downloads for the same user/pack in the next 5 minutes licenses_url = (reverse('pack-licenses', args=[username, pack_id])) return download_sounds(licenses_url, pack)
def pack_download(request, username, pack_id): if not request.user.is_authenticated: return HttpResponseRedirect( '%s?next=%s' % (reverse("accounts-login"), reverse("pack", args=[username, pack_id]))) pack = get_object_or_404(Pack, id=pack_id) if pack.user.username.lower() != username.lower(): raise Http404 if 'HTTP_RANGE' not in request.META: ''' Download managers and some browsers use the range header to download files in multiple parts. We have observed that all clients first make a GET with no range header (to get the file length) and then make multiple other requests. We ignore all requests that have range header because we assume that a first query has already been made. We additionally guard against users clicking on download multiple times by storing a sentinel in the cache for 5 minutes. ''' cache_key = 'pdwn_%s_%d' % (pack_id, request.user.id) if cache.get(cache_key, None) is None: pd = PackDownload.objects.create(user=request.user, pack=pack) pds = [] for sound in pack.sounds.all(): pds.append( PackDownloadSound(sound=sound, license=sound.license, pack_download=pd)) PackDownloadSound.objects.bulk_create(pds) cache.set( cache_key, True, 60 * 5 ) # Don't save downloads for the same user/pack in the next 5 minutes licenses_url = (reverse('pack-licenses', args=[username, pack_id])) return download_sounds(licenses_url, pack)
def pack_download(request, username, pack_id): if not request.user.is_authenticated: return HttpResponseRedirect( '%s?next=%s' % (reverse("accounts-login"), reverse("pack", args=[username, pack_id]))) pack = get_object_or_404(Pack, id=pack_id) if pack.user.username.lower() != username.lower(): raise Http404 Download.objects.get_or_create(user=request.user, pack=pack) licenses_url = (reverse('pack-licenses', args=[username, pack_id])) return download_sounds(licenses_url, pack)
def pack_download(request, username, pack_id): if not request.user.is_authenticated(): return HttpResponseRedirect('%s?next=%s' % (reverse("accounts-login"), reverse("pack", args=[username, pack_id]))) if settings.LOG_CLICKTHROUGH_DATA: click_log(request, click_type='packdownload', pack_id=pack_id) pack = get_object_or_404(Pack, id=pack_id) if pack.user.username.lower() != username.lower(): raise Http404 Download.objects.get_or_create(user=request.user, pack=pack) pack_sounds = pack.sound_set.filter(processing_state="OK", moderation_state="OK").select_related('user', 'license') return download_sounds(pack_sounds, reverse('pack', args=[username, pack.id]))
def pack_download(request, username, pack_id): if not request.user.is_authenticated: return HttpResponseRedirect( '%s?next=%s' % (reverse("accounts-login"), reverse("pack", args=[username, pack_id]))) pack = get_object_or_404(Pack, id=pack_id) if pack.user.username.lower() != username.lower(): raise Http404 sentry_logger.info('Download pack', exc_info=True, extra={ 'request': request, 'pack_id': pack_id, }) if not Download.objects.filter(user=request.user, pack=pack).exists(): Download.objects.create(user=request.user, pack=pack) licenses_url = (reverse('pack-licenses', args=[username, pack_id])) return download_sounds(licenses_url, pack)