diff --git a/wagtail_localize/models.py b/wagtail_localize/models.py index 07071622..722e8155 100644 --- a/wagtail_localize/models.py +++ b/wagtail_localize/models.py @@ -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 diff --git a/wagtail_localize/views/edit_translation.py b/wagtail_localize/views/edit_translation.py index ad45ccb6..e690f2e8 100644 --- a/wagtail_localize/views/edit_translation.py +++ b/wagtail_localize/views/edit_translation.py @@ -1,4 +1,5 @@ import json +import os import tempfile from collections import defaultdict @@ -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"] @@ -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: