Skip to content

Commit

Permalink
Wagtail localize fixes (#757, #758) (#781)
Browse files Browse the repository at this point in the history
* Resolve case insensitivity issues
* Resolve windows PO imports permissionerrors
  • Loading branch information
Nigel2392 authored and zerolab committed Feb 25, 2024
1 parent d45a051 commit 06b3cfe
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
5 changes: 4 additions & 1 deletion wagtail_localize/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1252,8 +1252,11 @@ def import_po(

for index, entry in enumerate(po):
try:
# Filter by hash instead to avoid case sensitivity issues
# https://github.com/wagtail/wagtail-localize/issues/758
string = String.objects.get(
locale_id=self.source.locale_id, data=entry.msgid
locale_id=self.source.locale_id,
data_hash=String._get_data_hash(entry.msgid),
)
context = TranslationContext.objects.get(
object_id=self.source.object_id, path=entry.msgctxt
Expand Down
27 changes: 19 additions & 8 deletions wagtail_localize/views/edit_translation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import os
import tempfile

from collections import defaultdict
Expand Down Expand Up @@ -1328,20 +1329,25 @@ def upload_pofile(request, translation_id):

do_import = True

with tempfile.NamedTemporaryFile() as f:
# Set delete to false. This fixes some windows errors when creating tempfiles.
# This is due to the Windows OS locking temporary files when open.
# https://github.com/wagtail/wagtail-localize/issues/757
with tempfile.NamedTemporaryFile(delete=False) as f:
# Note: polib.pofile accepts either a filename or contents. We cannot pass the
# contents directly into polib.pofile or users could upload a file containing
# a filename and this will be read by polib!
f.write(request.FILES["file"].read())
f.flush()

try:
po = polib.pofile(f.name)
# Move indentation back, we should open the file outside of the with statement.
# Delete must be set to false, otherwise the file will be deleted before we can open it.
try:
po = polib.pofile(f.name)

except (OSError, UnicodeDecodeError):
# Annoyingly, POLib uses OSError for parser exceptions...
messages.error(request, _("Please upload a valid PO file."))
do_import = False
except (OSError, UnicodeDecodeError):
# Annoyingly, POLib uses OSError for parser exceptions...
messages.error(request, _("Please upload a valid PO file."))
do_import = False

if do_import:
translation_id = po.metadata["X-WagtailLocalize-TranslationID"]
Expand All @@ -1356,9 +1362,14 @@ def upload_pofile(request, translation_id):

if do_import:
translation.import_po(po, user=request.user, tool_name="PO File")

messages.success(request, _("Successfully imported translations from PO File."))

# Delete the created tempfile
try:
os.unlink(f.name)
except OSError:
pass

# Work out where to redirect to
next_url = get_valid_next_url_from_request(request)
if not next_url:
Expand Down

0 comments on commit 06b3cfe

Please sign in to comment.