Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stops multiple galley image files with the same name from being loaded in the edit galley interface. #4032

Merged
merged 9 commits into from
Mar 15, 2024
33 changes: 29 additions & 4 deletions src/journal/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,16 @@ def identifier_figure(request, identifier_type, identifier, file_name):
if not galley:
raise Http404

figure = get_object_or_404(galley.images, original_filename=file_name)
# Use a filter with .first() here to avoid an error when two images with
# the same name are present.
figure = galley.images.filter(
original_filename=file_name
).order_by(
'-last_modified',
).first()

if not figure:
raise Http404

return files.serve_file(request, figure, figure_article)

Expand All @@ -946,9 +955,25 @@ def article_figure(request, article_id, galley_id, file_name):
:param file_name: an File object name
:return: a streaming file response or a 404 if not found
"""
figure_article = get_object_or_404(submission_models.Article, pk=article_id)
galley = get_object_or_404(core_models.Galley, pk=galley_id, article=figure_article)
figure = get_object_or_404(galley.images, original_filename=file_name)
figure_article = get_object_or_404(
submission_models.Article,
pk=article_id,
)
galley = get_object_or_404(
core_models.Galley,
pk=galley_id,
article=figure_article,
)
# Use a filter with .first() here to avoid an error when two images with
# the same name are present.
figure = galley.images.filter(
original_filename=file_name
).order_by(
'-last_modified',
).first()

if not figure:
raise Http404

return files.serve_file(request, figure, figure_article)

Expand Down
74 changes: 65 additions & 9 deletions src/production/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
from core import files, models as core_models
from copyediting import models as copyediting_models
from utils import render_template
from utils.logger import get_logger

logger = get_logger(__name__)


def get_production_managers(article):
Expand Down Expand Up @@ -173,26 +176,79 @@ def replace_galley_file(article, request, galley, uploaded_file):
messages.add_message(request, messages.WARNING, 'No file was selected.')


def save_galley_image(galley, request, uploaded_file, label="Galley Image", fixed=False):
def save_galley_image(
galley,
request,
uploaded_file,
label="Galley Image",
fixed=False,
check_for_existing_images=False,
):
filename = uploaded_file.name
# Check if an image with this name already exists for this galley.
if galley.images.filter(
original_filename=filename,
).exists():
# First, if check_for_existing_images is set, we check and warn the
# user, no overwriting takes place. Currently all workflow changes set
# this to True.
if check_for_existing_images:
messages.add_message(
request,
messages.WARNING,
f'An image called {filename} already exists. Use the '
f'replace file function to upload a new version.',
)
return
else:
if galley.article:
image_to_overwrite = galley.images.filter(
original_filename=filename,
).first()
replacement_file = files.overwrite_file(
uploaded_file,
image_to_overwrite,
('articles', galley.article.pk),
)
messages.add_message(
request,
messages.INFO,
f'An image called {filename} already exists. It has been '
f'overwritten with your uploaded file.'
)
return replacement_file
else:
messages.add_message(
request,
messages.ERROR,
f"Galley {galley.pk} is not linked to an article. "
f"Please contact your Janeway administrator.",
)
logger.warning(
f"Galley {galley.pk} is not linked to an article.",
)
return

if fixed:
filename = request.POST.get('file_name')
uploaded_file_mime = files.check_in_memory_mime(uploaded_file)
expected_mime = files.guess_mime(filename)

if not uploaded_file_mime == expected_mime:
messages.add_message(request, messages.WARNING, 'The file you uploaded does not match the mime of the '
'file expected.')
new_file_mime = files.guess_mime(filename)

if not uploaded_file_mime == new_file_mime:
messages.add_message(
request,
messages.WARNING,
f'The file you uploaded does not have the expected '
f'type: {new_file_mime}.'
)

new_file = files.save_file_to_article(uploaded_file, galley.article, request.user)
new_file.is_galley = False
new_file.label = label

if fixed:
new_file.original_filename = request.POST.get('file_name')
new_file.original_filename = filename

new_file.save()

galley.images.add(new_file)

return new_file
Expand Down
2 changes: 2 additions & 0 deletions src/production/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,7 @@ def edit_galley(request, galley_id, typeset_id=None, article_id=None):
uploaded_file,
label,
fixed=True,
check_for_existing_images=True,
)

if 'image-upload' in request.POST:
Expand All @@ -927,6 +928,7 @@ def edit_galley(request, galley_id, typeset_id=None, article_id=None):
uploaded_file,
label,
fixed=False,
check_for_existing_images=True,
)

elif 'css-upload' in request.POST:
Expand Down