<s> from django . contrib . auth . decorators import login_required <EOL> from django . shortcuts import get_object_or_404 , render_to_response <EOL> from django . http import HttpResponseForbidden <EOL> from django . db . models import Q <EOL> from django . template import RequestContext <EOL> from sendfile import sendfile <EOL> from . models import Download <EOL> def download ( request , download_id ) : <EOL> download = get_object_or_404 ( Download , pk = download_id ) <EOL> if download . is_public : <EOL> return sendfile ( request , download . file . path ) <EOL> return _auth_download ( request , download ) <EOL> @ login_required <EOL> def _auth_download ( request , download ) : <EOL> if not download . is_user_allowed ( request . user ) : <EOL> return HttpResponseForbidden ( '<STR_LIT>' ) <EOL> return sendfile ( request , download . file . path ) <EOL> def download_list ( request ) : <EOL> downloads = Download . objects . all ( ) <EOL> if request . user . is_authenticated ( ) : <EOL> downloads = downloads . filter ( Q ( is_public = True ) | Q ( users = request . user ) ) <EOL> else : <EOL> downloads = downloads . filter ( is_public = True ) <EOL> return render_to_response ( '<STR_LIT>' , <EOL> { '<STR_LIT>' : downloads } , <EOL> context_instance = RequestContext ( request ) ) </s>
