-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathutils.py
62 lines (51 loc) · 1.98 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#
# Copyright © Michal Čihař <[email protected]>
#
# This file is part of Weblate <https://weblate.org/>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
from __future__ import annotations
from typing import TYPE_CHECKING
from django.conf import settings
from django.contrib import messages
from django.http import HttpRequest
from django.urls import reverse
from django.utils.translation import gettext, override
if TYPE_CHECKING:
from django.contrib.auth.models import User
from django.forms import Form
PAYMENTS_ORIGIN = "https://weblate.org/donate/process/"
FOSDEM_ORIGIN = "https://weblate.org/fosdem/"
def get_site_url(name: str, *, strip_language: bool = True, **kwargs) -> str:
if strip_language:
with override("en"):
url = reverse(name, kwargs=kwargs)
url = url.removeprefix("/en")
else:
url = reverse(name, kwargs=kwargs)
return f"{settings.SITE_URL}{url}"
class AuthenticatedHttpRequest(HttpRequest):
user: User
def show_form_errors(request: HttpRequest, form: Form) -> None:
"""Show all form errors as a message."""
for error in form.non_field_errors():
messages.error(request, str(error))
for field in form:
for error in field.errors:
messages.error(
request,
gettext("Error in parameter %(field)s: %(error)s")
% {"field": field.name, "error": error},
)