<s> from django . core . urlresolvers import reverse <EOL> from django . http import HttpResponseRedirect <EOL> from django . shortcuts import render_to_response <EOL> from django . template import RequestContext <EOL> import os <EOL> import tempfile <EOL> import uuid <EOL> import shutil <EOL> from forms import UploadFileForm <EOL> from cardimporter . importer import AnkiImporter <EOL> from os import path , removedirs <EOL> from tsune . settings . base import PROJECT_DIR <EOL> TEMP_DIR = PROJECT_DIR + "<STR_LIT>" <EOL> def upload_file ( request ) : <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = UploadFileForm ( request . POST , request . FILES ) <EOL> if form . is_valid ( ) : <EOL> handle_uploaded_file ( request . FILES [ '<STR_LIT:file>' ] , request . user ) <EOL> return HttpResponseRedirect ( reverse ( "<STR_LIT>" ) ) <EOL> else : <EOL> form = UploadFileForm ( ) <EOL> return render_to_response ( '<STR_LIT>' , RequestContext ( request , { '<STR_LIT>' : form } ) ) <EOL> def handle_uploaded_file ( f , user ) : <EOL> temp_path = tempfile . mkdtemp ( ) <EOL> file_path = temp_path + "<STR_LIT:/>" + f . name <EOL> with open ( file_path , '<STR_LIT:w>' ) as destination : <EOL> for chunk in f . chunks ( ) : <EOL> destination . write ( chunk ) <EOL> anki = AnkiImporter ( ) <EOL> try : <EOL> anki . importCollection ( file_path , user ) <EOL> finally : <EOL> shutil . rmtree ( temp_path , ignore_errors = True ) </s>
