Skip to content

Commit

Permalink
Fix #1: Merge catalogs instead of clobbering the old one if an existi…
Browse files Browse the repository at this point in the history
…ng catalog exists already
  • Loading branch information
matthiask committed Jul 4, 2024
1 parent ca2bd40 commit a3089e8
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 4 deletions.
73 changes: 73 additions & 0 deletions projects/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,77 @@ def test_smoke(self):
)
self.assertEqual(r.status_code, 404)

r = self.client.put(
"/api/pofile/fr/djangojs/",
headers={"x-project-token": p.token},
data=b"""\
#: conf/strings.js frontend/intro/intro.js frontend/people/person.js
msgid "Continue"
msgstr "Blub"
#: conf/strings.js
msgid "Copied code!"
msgstr ""
msgid "Hello World"
msgstr "Blab"
""",
)

c.refresh_from_db()

self.assertIn(
"""\
msgid "Continue"
msgstr "Continuer"
""",
c.pofile,
)

self.assertIn(
"""\
msgid "Hello World"
msgstr ""
""",
c.pofile,
)

# Different language!
r = self.client.put(
"/api/pofile/de/djangojs/",
headers={"x-project-token": p.token},
data=b"""\
#: conf/strings.js frontend/intro/intro.js frontend/people/person.js
msgid "Continue"
msgstr "Blub"
#: conf/strings.js
msgid "Copied code!"
msgstr ""
msgid "Hello World"
msgstr "Blab"
""",
)

c = p.catalogs.order_by("id").last()

self.assertIn(
"""\
msgid "Continue"
msgstr "Blub"
""",
c.pofile,
)

self.assertIn(
"""\
msgid "Hello World"
msgstr "Blab"
""",
c.pofile,
)

# print(c.pofile)
# print(list(c.po))
# print(r, r.content.decode("utf-8"))
1 change: 0 additions & 1 deletion projects/tests.py

This file was deleted.

12 changes: 9 additions & 3 deletions projects/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,12 +305,18 @@ def pofile(request, language_code, domain):
return http.HttpResponseNotFound()

if request.method == "PUT":
po = polib.pofile(request.body.decode("utf-8"))
project.catalogs.update_or_create(
new = polib.pofile(request.body.decode("utf-8"))
old, created = project.catalogs.get_or_create(
language_code=language_code,
domain=domain,
defaults={"pofile": str(po)},
defaults={"pofile": ""},
)
if created:
old.pofile = str(new)
else:
old.po.merge(new)
old.pofile = str(old.po)
old.save()
return http.HttpResponse(status=202) # Accepted

return http.HttpResponse(status=405) # Method Not Allowed

0 comments on commit a3089e8

Please sign in to comment.