def replace_article_file(request, identifier_type, identifier, file_id): """ Renders the page to replace an article file :param request: the request associated with this call :param identifier_type: the identifier type for the article :param identifier: the identifier for the article :param file_id: the file ID to replace :return: a rendered template to replace the file """ article_to_replace = submission_models.Article.get_article(request.journal, identifier_type, identifier) file_to_replace = get_object_or_404(core_models.File, pk=file_id) error = None if request.GET.get('delete', False): file_delete(request, article_to_replace.pk, file_to_replace.pk) return redirect(reverse('submit_files', kwargs={'article_id': article_to_replace.id})) if request.POST: if 'replacement' in request.POST: uploaded_file = request.FILES.get('replacement-file') files.overwrite_file(uploaded_file, article_to_replace, file_to_replace) return redirect(request.GET.get('return', 'core_dashboard')) template = "journal/replace_file.html" context = { 'article': article_to_replace, 'old_file': file_to_replace, 'error': error, } return render(request, template, context)
def replace_galley_file(article, request, galley, uploaded_file): if uploaded_file: files.overwrite_file( uploaded_file, galley.file, ('articles', article.pk), ) else: messages.add_message(request, messages.WARNING, 'No file was selected.')
def sync_galleys(self, article, metadata): """currently only supporting pdf galleys""" for document in metadata["documents"]: if document["mime_type"] in files.PDF_MIMETYPES: file_data = document["files"][0] # PDFs have always one file response = requests.get(file_data["uri"], stream=True) validate_response(response) file_ = SimpleUploadedFile( file_data["filename"], response.content, document["mime_type"], ) if article.pdfs: if len(article.pdfs) > 1: raise RuntimeError("Article has multiple PDF galleys") files.overwrite_file( file_, article.pdfs[0].file, 'articles', article.pk, ) else: saved_file = files.save_file_to_article( file_, article, owner=None, label="pdf", is_galley=True, ) galley = Galley.objects.create( article=article, file=saved_file, type="pdf", label="PDF", ) article.galley_set.add(galley) obj, created = ImportedArticleGalley.objects.get_or_create( article=article, eprint_id=document["eprintid"], version=document["rev_number"], uri=file_data["uri"], ) if created: self.stdout.write("Imported galley: %s " % obj)
def handle_article_thumb_image_file(uploaded_file, article, request): if not article.thumbnail_image_file: new_file = article_file(uploaded_file, article, request) article.thumbnail_image_file = new_file article.save() else: new_file = files.overwrite_file(uploaded_file, article, article.thumbnail_image_file) article.thumbnail_image_file = new_file article.save()
def handle_article_large_image_file(uploaded_file, article, request): if not article.large_image_file: new_file = article_file(uploaded_file, article, request) article.large_image_file = new_file article.save() else: new_file = files.overwrite_file(uploaded_file, article, article.large_image_file) article.large_image_file = new_file article.save() resize_and_crop(new_file.self_article_path(), [750, 324], 'middle')
def test_overwrite_file(self): file_to_replace, path_parts = helpers.create_test_file( self, self.test_file_one, ) files.overwrite_file( self.test_file_two, file_to_replace, path_parts=path_parts, ) file_path = os.path.join( settings.BASE_DIR, 'files', 'articles', self.pk_string, file_to_replace.uuid_filename, ) with open(file_path, 'rb') as file: content = file.read() self.assertEqual(content, b'content')
def handle_zipped_galley_images(zip_file, galley, request): with zipfile.ZipFile(zip_file, 'r') as zf: for finfo in zf.infolist(): zipped_file = zf.open(finfo) content_file = ContentFile(zipped_file.read()) content_file.name = zipped_file.name if zipped_file.name in galley.has_missing_image_files(): new_file = files.save_file_to_article( content_file, galley.article, request.user, ) new_file.is_galley = False new_file.label = "Galley Image" new_file.save() galley.images.add(new_file) elif zipped_file.name in galley.all_images(): try: file = galley.images.get( original_filename=zipped_file.name, ) updated_file = files.overwrite_file( content_file, file, ('articles', galley.article.pk)) updated_file.original_filename = zipped_file.name updated_file.save() messages.add_message( request, messages.SUCCESS, 'New version of {} saved.'.format(zipped_file.name)) except core_models.File.DoesNotExist: messages.add_message( request, messages.ERROR, 'A file was found in XML and Zip but no corresponding' 'File object could be found. {}'.format( zipped_file.name, )) messages.add_message(request, messages.SUCCESS, '') else: messages.add_message( request, messages.WARNING, 'File {} not found in XML'.format(zipped_file.name)) return
def set_article_image(request, article): from core import logic as core_logic if 'delete_image' in request.POST: delete_id = request.POST.get('delete_image') file_to_delete = get_object_or_404(core_models.File, pk=delete_id, article_id=article.pk) if file_to_delete == article.large_image_file and request.user.is_staff or request.user == file_to_delete.owner: file_to_delete.delete() article.fixedpubcheckitems.select_article_image = False article.fixedpubcheckitems.save() if request.POST and request.FILES: uploaded_file = request.FILES.get('image_file') if not article.large_image_file: new_file = files.save_file_to_article(uploaded_file, article, request.user) new_file.label = 'Banner image' new_file.description = 'Banner image' new_file.privacy = 'public' new_file.save() article.large_image_file = new_file article.save() messages.add_message(request, messages.SUCCESS, 'New file loaded') else: new_file = files.overwrite_file( uploaded_file, article.large_image_file, ('articles', article.pk), ) article.large_image_file = new_file article.save() messages.add_message(request, messages.SUCCESS, 'File overwritten.') article.fixedpubcheckitems.select_article_image = True article.fixedpubcheckitems.save() core_logic.resize_and_crop(new_file.self_article_path(), [750, 324], 'middle')
def replace_file(self, other): new_file = files.overwrite_file(other, self.file, self.path_parts) self.file = new_file self.save()