From 1302512e2ea488682d008cd9889661f8a82a8667 Mon Sep 17 00:00:00 2001 From: Matthias Kestenholz Date: Thu, 4 Jul 2024 22:47:04 +0200 Subject: [PATCH] Split views into forms and views --- projects/forms.py | 140 ++++++++++++++++++++++++++++++++++++++++++++++ projects/views.py | 136 +------------------------------------------- 2 files changed, 142 insertions(+), 134 deletions(-) create mode 100644 projects/forms.py diff --git a/projects/forms.py b/projects/forms.py new file mode 100644 index 0000000..173d65e --- /dev/null +++ b/projects/forms.py @@ -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( + '{}', + 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", "anonymous@user.tld"), + ) + 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() diff --git a/projects/views.py b/projects/views.py index fdfe93d..f18b3cb 100644 --- a/projects/views.py +++ b/projects/views.py @@ -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( - '{}', - 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", "anonymous@user.tld"), - ) - 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: