2013-11-24 4 views
0

이것은 정말로 나를 죽이고 있습니다. 나는 이것을 며칠 동안 다루어왔다.django에서 요청 당 메일 보내기

사용자가 내 django 웹 앱에서 파일을 다운로드 할 때 업 로더에게 파일을 메일로 보내어 다운로드했음을 알리려고합니다. 문제는, low file size (489kb)을 다운로드해야한다면 mail once to the uploader입니다. 하지만 file size of 3mb or above을 다운로드해야한다면 more than one mail to the uploader이 전송됩니다.

다운로드 당 하나의 메일 알림을 업로드자에게 보내기를 원합니다.

전망 :

@login_required 
def document_view(request,emov_id): 
    fileload = Emov.objects.get(id=emov_id) 
    filename = fileload.mov_file.name.split('/')[-1] 
    filesize=fileload.mov_file.size 
    response = HttpResponse(fileload.mov_file, content_type='') 
    response['Content-Disposition'] = 'attachment; filename=%s' % filename 
    response['Content-Length'] = filesize  
    send_mail('Your file has just been downloaded',loader.get_template('download.txt').render(Context({'fileload':fileload})),'[email protected]',[fileload.email,]) 
    return response 

download.txt 나는 다운로드 요청에 따라 메일을 보낼 수있는 방법

'Your file {{ fileload.name}} have been downloaded!' 

? 나는 다른 접근 방식을 제안

+1

은 별도의 함수에서 센드 메일 함수 호출을 넣어 시도하고 ..이보기에서 나는이 때문에'범위 request'의 생각 –

+0

를 함수를 호출합니다. 상태 코드를 206 (부분 내용)으로 설정하십시오. 예 : '응답 = HttpResponse (fileload.mov_file, content_type = '', 상태 = 206)' – sha256

답변

1

... 누군가가 파일, 로그 데이터베이스의 테이블에 이벤트를 다운로드

.
세션 ID, 파일 이름, 사용자 이름을 씁니다.
session_id + file_name + user_name이 고유 키인지 확인하십시오.
이렇게하면 나중에 도움이 될 수있는 더 많은 정보를 얻을 수 있습니다.

나중에 (crontab 배치 또는 저장 리스너로서) 전자 메일을 보내십시오.
일일/주간 보고서를 보낼 수도 있습니다.

1

"장고로 파일을 보내지 마십시오."라는 모범 사례를 따르면이 문제를 해결할 수 있습니다.

대신 응답에서 X-Sendfile HTTP 헤더를 사용하고 웹 서버가 파일을 수신하고 파일을 제공하도록 구성하십시오. Apache를 사용하는 경우 this을 참조하십시오. 다음과 같이

그런 응답을 만들 :

response = HttpResponse() 
response['X-Sendfile'] = unicode(filename).encode('utf-8') 
response['Content-Type'] = 'application/octet-stream' 
response['Content-Disposition'] = 'attachment; filename="%s"' % filename 
response['Content-length'] = filesize # Optional 
return response 
관련 문제