def add_document_view(request, node, thread_type): """ create new document (and related Thread) """ def _create_doc(user, post, file_data): document = Document.objects.create( author=user, thread=post.thread ) parsed_file_name = os.path.splitext( force_unicode(file_data.name, strings_only=True, errors="ignore") ) file_name = parsed_file_name[0].lower() suffix = parsed_file_name[1].replace(".", "").lower() return document.revisions.create( file_data=file_data, original_filename=file_name, suffix=suffix, filename_slug=sanitize_file_name(file_name), author=user, ) def _is_zip_file_extended(name, path): """ test if file on path is zip archive, test for special extension is simple test for exclude zip like files (docx, xlsx, ...) """ ZIP_FILES_EXT = ["zip"] return (name.split(".")[-1].lower() in ZIP_FILES_EXT) and zipfile.is_zipfile(path) def recursive_process_dir(directory): """ recursive read directory content and create Documents from all files on any level of direcotry tree. Final structure is flat. """ for file_name in os.listdir(directory): _path = os.path.join(directory, file_name) if os.path.isdir(_path): recursive_process_dir(_path) else: title = force_unicode(file_name, strings_only=True, errors="ignore") _post = user.post_thread(**{ "title": "%s: %s" % (form.cleaned_data['title'], title), "body_text": "", "timestamp": timestamp, "node": node, "thread_type": thread_type, "category": category, "external_access": form.cleaned_data["allow_external_access"], }) with codecs.open(_path, "r", errors="ignore") as file_content: _create_doc(user, _post, SimpleUploadedFile(title, file_content.read())) ################################################################################ if request.method == 'POST': form = DocumentForm(request.REQUEST, request.FILES, node=node, user=request.user) if form.is_valid(): timestamp = datetime.datetime.now() text = form.cleaned_data['text'] category = form.cleaned_data['thread_category'] if request.user.is_authenticated(): drafts = models.DraftQuestion.objects.filter( author=request.user ) drafts.delete() user = request.user try: _data = { "title": form.cleaned_data['title'], "body_text": text, "timestamp": timestamp, "node": node, "thread_type": thread_type, "category": category, "external_access": form.cleaned_data["allow_external_access"], } post = user.post_thread(**_data) del _data file_data = form.cleaned_data["file_data"] if file_data: # create Document from uploaded file dr = _create_doc(user, post, file_data) # if uploaded file is zip archive, create documents from all files in. if _is_zip_file_extended(dr.file_data.name, dr.file_data.path): # extract zip to temp directory temp_dir = tempfile.mkdtemp() with zipfile.ZipFile(dr.file_data.path, "r") as zf: zf.extractall(temp_dir) # recursive process all files in all directories of zip file # create flat structure from directory tree recursive_process_dir(temp_dir) # clear shutil.rmtree(temp_dir) request.user.message_set.create(message=_('Document has been successfully added.')) return HttpResponseRedirect(post.thread.get_absolute_url()) except exceptions.PermissionDenied, e: request.user.message_set.create(message=unicode(e)) return HttpResponseRedirect(reverse('index'))
def add_document_view(request, node, thread_type): """ create new document (and related Thread) """ def _create_doc(user, post, file_data): document = Document.objects.create(author=user, thread=post.thread) parsed_file_name = os.path.splitext( force_unicode(file_data.name, strings_only=True, errors="ignore")) file_name = parsed_file_name[0].lower() suffix = parsed_file_name[1].replace(".", "").lower() return document.revisions.create( file_data=file_data, original_filename=file_name, suffix=suffix, filename_slug=sanitize_file_name(file_name), author=user, ) def _is_zip_file_extended(name, path): """ test if file on path is zip archive, test for special extension is simple test for exclude zip like files (docx, xlsx, ...) """ ZIP_FILES_EXT = ["zip"] return (name.split(".")[-1].lower() in ZIP_FILES_EXT) and zipfile.is_zipfile(path) def recursive_process_dir(directory): """ recursive read directory content and create Documents from all files on any level of direcotry tree. Final structure is flat. """ for file_name in os.listdir(directory): _path = os.path.join(directory, file_name) if os.path.isdir(_path): recursive_process_dir(_path) else: title = force_unicode(file_name, strings_only=True, errors="ignore") _post = user.post_thread( **{ "title": "%s: %s" % (form.cleaned_data['title'], title), "body_text": "", "timestamp": timestamp, "node": node, "thread_type": thread_type, "category": category, "external_access": form.cleaned_data["allow_external_access"], }) with codecs.open(_path, "r", errors="ignore") as file_content: _create_doc(user, _post, SimpleUploadedFile(title, file_content.read())) ################################################################################ if request.method == 'POST': form = DocumentForm(request.REQUEST, request.FILES, node=node, user=request.user) if form.is_valid(): timestamp = datetime.datetime.now() text = form.cleaned_data['text'] category = form.cleaned_data['thread_category'] if request.user.is_authenticated(): drafts = models.DraftQuestion.objects.filter( author=request.user) drafts.delete() user = request.user try: _data = { "title": form.cleaned_data['title'], "body_text": text, "timestamp": timestamp, "node": node, "thread_type": thread_type, "category": category, "external_access": form.cleaned_data["allow_external_access"], } post = user.post_thread(**_data) del _data file_data = form.cleaned_data["file_data"] if file_data: # create Document from uploaded file dr = _create_doc(user, post, file_data) # if uploaded file is zip archive, create documents from all files in. if _is_zip_file_extended(dr.file_data.name, dr.file_data.path): # extract zip to temp directory temp_dir = tempfile.mkdtemp() with zipfile.ZipFile(dr.file_data.path, "r") as zf: zf.extractall(temp_dir) # recursive process all files in all directories of zip file # create flat structure from directory tree recursive_process_dir(temp_dir) # clear shutil.rmtree(temp_dir) request.user.message_set.create( message=_('Document has been successfully added.')) return HttpResponseRedirect(post.thread.get_absolute_url()) except exceptions.PermissionDenied, e: request.user.message_set.create(message=unicode(e)) return HttpResponseRedirect(reverse('index'))
# recursive process all files in all directories of zip file # create flat structure from directory tree recursive_process_dir(temp_dir) # clear shutil.rmtree(temp_dir) request.user.message_set.create(message=_('Document has been successfully added.')) return HttpResponseRedirect(post.thread.get_absolute_url()) except exceptions.PermissionDenied, e: request.user.message_set.create(message=unicode(e)) return HttpResponseRedirect(reverse('index')) elif request.method == 'GET': form = DocumentForm(node=node, user=request.user) draft_title = '' draft_text = '' draft_tagnames = '' if request.user.is_authenticated(): drafts = models.DraftQuestion.objects.filter(author=request.user) if len(drafts) > 0: draft = drafts[0] draft_title = draft.title draft_text = draft.text draft_tagnames = draft.tagnames form.initial = { 'title': request.REQUEST.get('title', draft_title), 'text': request.REQUEST.get('text', draft_text),
# recursive process all files in all directories of zip file # create flat structure from directory tree recursive_process_dir(temp_dir) # clear shutil.rmtree(temp_dir) request.user.message_set.create( message=_('Document has been successfully added.')) return HttpResponseRedirect(post.thread.get_absolute_url()) except exceptions.PermissionDenied, e: request.user.message_set.create(message=unicode(e)) return HttpResponseRedirect(reverse('index')) elif request.method == 'GET': form = DocumentForm(node=node, user=request.user) draft_title = '' draft_text = '' draft_tagnames = '' if request.user.is_authenticated(): drafts = models.DraftQuestion.objects.filter(author=request.user) if len(drafts) > 0: draft = drafts[0] draft_title = draft.title draft_text = draft.text draft_tagnames = draft.tagnames form.initial = { 'title': request.REQUEST.get('title', draft_title), 'text': request.REQUEST.get('text', draft_text),