-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
142 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
import copy | ||
|
||
from django import forms | ||
from django.conf import settings | ||
from django.contrib import messages | ||
from django.utils.html import format_html | ||
from django.utils.timezone import localtime | ||
from django.utils.translation import gettext_lazy as _, ngettext | ||
|
||
from projects import translators | ||
|
||
|
||
ENTRIES_PER_PAGE = 20 | ||
|
||
|
||
class FilterForm(forms.Form): | ||
pending = forms.BooleanField(label=_("Pending"), required=False) | ||
query = forms.CharField( | ||
label="", | ||
required=False, | ||
widget=forms.TextInput(attrs={"placeholder": _("Query")}), | ||
) | ||
start = forms.IntegerField( | ||
label=_("Start"), widget=forms.HiddenInput, required=False | ||
) | ||
|
||
|
||
def _help_text(msgid, language_code): | ||
if settings.DEEPL_AUTH_KEY: | ||
return format_html( | ||
'<a href="#" data-suggest="{}" data-language-code="{}"><small>{}</small></a>', | ||
msgid, | ||
language_code, | ||
_("Suggest"), | ||
) | ||
return "" | ||
|
||
|
||
class EntriesForm(forms.Form): | ||
def __init__(self, *args, **kwargs): | ||
self.entries = kwargs.pop("entries") | ||
self.language_code = kwargs.pop("language_code") | ||
super().__init__(*args, **kwargs) | ||
|
||
self.entry_rows = [] | ||
|
||
for index, entry in enumerate(self.entries): | ||
self.fields[f"msgid_{index}"] = forms.CharField( | ||
widget=forms.HiddenInput, | ||
initial=entry.msgid_with_context, | ||
) | ||
self.fields[f"fuzzy_{index}"] = forms.BooleanField( | ||
label="Fuzzy", | ||
initial=entry.fuzzy, | ||
required=False, | ||
) | ||
|
||
self.entry_rows.append({ | ||
"entry": entry, | ||
"msgid": self[f"msgid_{index}"], | ||
"msgstr": [], | ||
"fuzzy": self[f"fuzzy_{index}"], | ||
}) | ||
|
||
if entry.msgid_plural: | ||
for count, msgstr in sorted(entry.msgstr_plural.items()): | ||
name = f"msgstr_{index}:{count}" | ||
self.fields[name] = forms.CharField( | ||
label=_("With {count} items").format(count=count), | ||
widget=forms.Textarea(attrs={"rows": 3}), | ||
initial=msgstr, | ||
required=False, | ||
help_text=_help_text(entry.msgid_plural, self.language_code), | ||
) | ||
self.entry_rows[-1]["msgstr"].append(self[name]) | ||
else: | ||
name = f"msgstr_{index}" | ||
self.fields[f"msgstr_{index}"] = forms.CharField( | ||
label="", | ||
widget=forms.Textarea(attrs={"rows": 3}), | ||
initial=entry.msgstr, | ||
required=False, | ||
help_text=_help_text(entry.msgid, self.language_code), | ||
) | ||
self.entry_rows[-1]["msgstr"].append(self[name]) | ||
|
||
def update(self, po, *, request): | ||
updates = 0 | ||
|
||
for index in range(ENTRIES_PER_PAGE): | ||
msgid_with_context = self.cleaned_data.get(f"msgid_{index}") | ||
msgstr = self.cleaned_data.get(f"msgstr_{index}", "") | ||
fuzzy = self.cleaned_data.get(f"fuzzy_{index}") | ||
|
||
if not msgid_with_context: | ||
continue | ||
|
||
for entry in po: | ||
if entry.msgid_with_context == msgid_with_context: | ||
old = copy.deepcopy(entry) | ||
entry.msgstr = translators.fix_nls(entry.msgid, msgstr) | ||
if entry.msgid_plural: | ||
for count in entry.msgstr_plural: | ||
entry.msgstr_plural[count] = translators.fix_nls( | ||
entry.msgid_plural, | ||
self.cleaned_data.get(f"msgstr_{index}:{count}", ""), | ||
) | ||
if fuzzy and not entry.fuzzy: | ||
entry.fuzzy = True | ||
if not fuzzy and entry.fuzzy: | ||
entry.fuzzy = False | ||
|
||
if old != entry: | ||
updates += 1 | ||
break | ||
|
||
if updates: | ||
po.metadata["Last-Translator"] = "{} {} <{}>".format( | ||
getattr(request.user, "first_name", "Anonymous"), | ||
getattr(request.user, "last_name", "User"), | ||
getattr(request.user, "email", "[email protected]"), | ||
) | ||
po.metadata["X-Translated-Using"] = "traduire 0.0.1" | ||
po.metadata["PO-Revision-Date"] = localtime().strftime("%Y-%m-%d %H:%M%z") | ||
|
||
messages.success( | ||
request, | ||
ngettext( | ||
"Successfully updated {count} message.", | ||
"Successfully updated {count} messages.", | ||
updates, | ||
).format(count=updates), | ||
) | ||
else: | ||
messages.info(request, _("No changes detected.")) | ||
|
||
|
||
class SuggestForm(forms.Form): | ||
language_code = forms.CharField() | ||
msgid = forms.CharField() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,15 @@ | ||
import copy | ||
|
||
import polib | ||
from django import forms, http | ||
from django import http | ||
from django.conf import settings | ||
from django.contrib import messages | ||
from django.contrib.auth.decorators import login_required | ||
from django.shortcuts import get_object_or_404, render | ||
from django.template.defaulttags import query_string | ||
from django.utils.html import format_html | ||
from django.utils.timezone import localtime | ||
from django.utils.translation import gettext_lazy as _, ngettext | ||
from django.views.decorators.csrf import csrf_exempt | ||
from django.views.decorators.http import require_POST | ||
|
||
from form_rendering import adapt_rendering | ||
from projects import translators | ||
from projects.forms import EntriesForm, FilterForm, SuggestForm | ||
from projects.models import Catalog, Project | ||
|
||
|
||
|
@@ -36,128 +31,6 @@ def project(request, slug): | |
return render(request, "projects/project.html", {"project": project}) | ||
|
||
|
||
class FilterForm(forms.Form): | ||
pending = forms.BooleanField(label=_("Pending"), required=False) | ||
query = forms.CharField( | ||
label="", | ||
required=False, | ||
widget=forms.TextInput(attrs={"placeholder": _("Query")}), | ||
) | ||
start = forms.IntegerField( | ||
label=_("Start"), widget=forms.HiddenInput, required=False | ||
) | ||
|
||
|
||
def _help_text(msgid, language_code): | ||
if settings.DEEPL_AUTH_KEY: | ||
return format_html( | ||
'<a href="#" data-suggest="{}" data-language-code="{}"><small>{}</small></a>', | ||
msgid, | ||
language_code, | ||
_("Suggest"), | ||
) | ||
return "" | ||
|
||
|
||
class EntriesForm(forms.Form): | ||
def __init__(self, *args, **kwargs): | ||
self.entries = kwargs.pop("entries") | ||
self.language_code = kwargs.pop("language_code") | ||
super().__init__(*args, **kwargs) | ||
|
||
self.entry_rows = [] | ||
|
||
for index, entry in enumerate(self.entries): | ||
self.fields[f"msgid_{index}"] = forms.CharField( | ||
widget=forms.HiddenInput, | ||
initial=entry.msgid_with_context, | ||
) | ||
self.fields[f"fuzzy_{index}"] = forms.BooleanField( | ||
label="Fuzzy", | ||
initial=entry.fuzzy, | ||
required=False, | ||
) | ||
|
||
self.entry_rows.append({ | ||
"entry": entry, | ||
"msgid": self[f"msgid_{index}"], | ||
"msgstr": [], | ||
"fuzzy": self[f"fuzzy_{index}"], | ||
}) | ||
|
||
if entry.msgid_plural: | ||
for count, msgstr in sorted(entry.msgstr_plural.items()): | ||
name = f"msgstr_{index}:{count}" | ||
self.fields[name] = forms.CharField( | ||
label=_("With {count} items").format(count=count), | ||
widget=forms.Textarea(attrs={"rows": 3}), | ||
initial=msgstr, | ||
required=False, | ||
help_text=_help_text(entry.msgid_plural, self.language_code), | ||
) | ||
self.entry_rows[-1]["msgstr"].append(self[name]) | ||
else: | ||
name = f"msgstr_{index}" | ||
self.fields[f"msgstr_{index}"] = forms.CharField( | ||
label="", | ||
widget=forms.Textarea(attrs={"rows": 3}), | ||
initial=entry.msgstr, | ||
required=False, | ||
help_text=_help_text(entry.msgid, self.language_code), | ||
) | ||
self.entry_rows[-1]["msgstr"].append(self[name]) | ||
|
||
def update(self, po, *, request): | ||
updates = 0 | ||
|
||
for index in range(ENTRIES_PER_PAGE): | ||
msgid_with_context = self.cleaned_data.get(f"msgid_{index}") | ||
msgstr = self.cleaned_data.get(f"msgstr_{index}", "") | ||
fuzzy = self.cleaned_data.get(f"fuzzy_{index}") | ||
|
||
if not msgid_with_context: | ||
continue | ||
|
||
for entry in po: | ||
if entry.msgid_with_context == msgid_with_context: | ||
old = copy.deepcopy(entry) | ||
entry.msgstr = translators.fix_nls(entry.msgid, msgstr) | ||
if entry.msgid_plural: | ||
for count in entry.msgstr_plural: | ||
entry.msgstr_plural[count] = translators.fix_nls( | ||
entry.msgid_plural, | ||
self.cleaned_data.get(f"msgstr_{index}:{count}", ""), | ||
) | ||
if fuzzy and not entry.fuzzy: | ||
entry.fuzzy = True | ||
if not fuzzy and entry.fuzzy: | ||
entry.fuzzy = False | ||
|
||
if old != entry: | ||
updates += 1 | ||
break | ||
|
||
if updates: | ||
po.metadata["Last-Translator"] = "{} {} <{}>".format( | ||
getattr(request.user, "first_name", "Anonymous"), | ||
getattr(request.user, "last_name", "User"), | ||
getattr(request.user, "email", "[email protected]"), | ||
) | ||
po.metadata["X-Translated-Using"] = "traduire 0.0.1" | ||
po.metadata["PO-Revision-Date"] = localtime().strftime("%Y-%m-%d %H:%M%z") | ||
|
||
messages.success( | ||
request, | ||
ngettext( | ||
"Successfully updated {count} message.", | ||
"Successfully updated {count} messages.", | ||
updates, | ||
).format(count=updates), | ||
) | ||
else: | ||
messages.info(request, _("No changes detected.")) | ||
|
||
|
||
@login_required | ||
def catalog(request, project, language_code, domain): | ||
catalog = get_object_or_404( | ||
|
@@ -230,11 +103,6 @@ def catalog(request, project, language_code, domain): | |
) | ||
|
||
|
||
class SuggestForm(forms.Form): | ||
language_code = forms.CharField() | ||
msgid = forms.CharField() | ||
|
||
|
||
@require_POST | ||
def suggest(request): | ||
if not request.user.is_authenticated: | ||
|