mirror of
https://github.com/eitchtee/WYGIWYH.git
synced 2026-04-25 10:08:36 +02:00
feat: monkey patch get_format to return usersettings
This commit is contained in:
31
app/apps/common/functions/format.py
Normal file
31
app/apps/common/functions/format.py
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
from apps.common.middleware.thread_local import get_current_user
|
||||||
|
from django.utils.formats import get_format as original_get_format
|
||||||
|
|
||||||
|
|
||||||
|
def get_format(format_type=None, lang=None, use_l10n=None):
|
||||||
|
user = get_current_user()
|
||||||
|
|
||||||
|
if user and user.is_authenticated and hasattr(user, "settings"):
|
||||||
|
user_settings = user.settings
|
||||||
|
if format_type == "THOUSAND_SEPARATOR":
|
||||||
|
number_format = getattr(user_settings, "number_format", None)
|
||||||
|
if number_format == "DC":
|
||||||
|
return "."
|
||||||
|
elif number_format == "CD":
|
||||||
|
return ","
|
||||||
|
elif format_type == "DECIMAL_SEPARATOR":
|
||||||
|
number_format = getattr(user_settings, "number_format", None)
|
||||||
|
if number_format == "DC":
|
||||||
|
return ","
|
||||||
|
elif number_format == "CD":
|
||||||
|
return "."
|
||||||
|
elif format_type == "SHORT_DATE_FORMAT":
|
||||||
|
date_format = getattr(user_settings, "date_format", None)
|
||||||
|
if date_format and date_format != "SHORT_DATE_FORMAT":
|
||||||
|
return date_format
|
||||||
|
elif format_type == "SHORT_DATETIME_FORMAT":
|
||||||
|
datetime_format = getattr(user_settings, "datetime_format", None)
|
||||||
|
if datetime_format and datetime_format != "SHORT_DATETIME_FORMAT":
|
||||||
|
return datetime_format
|
||||||
|
|
||||||
|
return original_get_format(format_type, lang, use_l10n)
|
||||||
@@ -1,14 +1,17 @@
|
|||||||
import zoneinfo
|
import zoneinfo
|
||||||
|
|
||||||
|
from django.utils import formats
|
||||||
from django.utils import timezone, translation
|
from django.utils import timezone, translation
|
||||||
from django.utils.translation import activate
|
from django.utils.functional import lazy
|
||||||
|
|
||||||
|
from apps.common.functions.format import get_format as custom_get_format
|
||||||
from apps.users.models import UserSettings
|
from apps.users.models import UserSettings
|
||||||
|
|
||||||
|
|
||||||
class LocalizationMiddleware:
|
class LocalizationMiddleware:
|
||||||
def __init__(self, get_response):
|
def __init__(self, get_response):
|
||||||
self.get_response = get_response
|
self.get_response = get_response
|
||||||
|
self.patch_get_format()
|
||||||
|
|
||||||
def __call__(self, request):
|
def __call__(self, request):
|
||||||
tz = request.COOKIES.get("mytz")
|
tz = request.COOKIES.get("mytz")
|
||||||
@@ -33,9 +36,14 @@ class LocalizationMiddleware:
|
|||||||
timezone.activate(zoneinfo.ZoneInfo("UTC"))
|
timezone.activate(zoneinfo.ZoneInfo("UTC"))
|
||||||
|
|
||||||
if user_language and user_language != "auto":
|
if user_language and user_language != "auto":
|
||||||
activate(user_language)
|
translation.activate(user_language)
|
||||||
else:
|
else:
|
||||||
detected_language = translation.get_language_from_request(request)
|
detected_language = translation.get_language_from_request(request)
|
||||||
activate(detected_language)
|
translation.activate(detected_language)
|
||||||
|
|
||||||
return self.get_response(request)
|
return self.get_response(request)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def patch_get_format():
|
||||||
|
formats.get_format = custom_get_format
|
||||||
|
formats.get_format_lazy = lazy(custom_get_format, str, list, tuple)
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
from django import template
|
from django import template
|
||||||
from django.utils.formats import get_format
|
|
||||||
|
|
||||||
|
from apps.common.functions.format import get_format
|
||||||
|
|
||||||
register = template.Library()
|
register = template.Library()
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ import datetime
|
|||||||
|
|
||||||
from django.forms import widgets
|
from django.forms import widgets
|
||||||
from django.utils import formats, translation, dates
|
from django.utils import formats, translation, dates
|
||||||
from django.utils.formats import get_format
|
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
from apps.common.utils.django import (
|
from apps.common.utils.django import (
|
||||||
@@ -10,6 +9,7 @@ from apps.common.utils.django import (
|
|||||||
django_to_airdatepicker_datetime,
|
django_to_airdatepicker_datetime,
|
||||||
django_to_airdatepicker_datetime_separated,
|
django_to_airdatepicker_datetime_separated,
|
||||||
)
|
)
|
||||||
|
from apps.common.functions.format import get_format
|
||||||
|
|
||||||
|
|
||||||
class AirDatePickerInput(widgets.DateInput):
|
class AirDatePickerInput(widgets.DateInput):
|
||||||
@@ -41,12 +41,6 @@ class AirDatePickerInput(widgets.DateInput):
|
|||||||
if self.format:
|
if self.format:
|
||||||
return self.format
|
return self.format
|
||||||
|
|
||||||
if self.user and hasattr(self.user, "settings"):
|
|
||||||
user_format = self.user.settings.date_format
|
|
||||||
if user_format == "SHORT_DATE_FORMAT":
|
|
||||||
return get_format("SHORT_DATE_FORMAT", use_l10n=True)
|
|
||||||
return user_format
|
|
||||||
|
|
||||||
return get_format("SHORT_DATE_FORMAT", use_l10n=True)
|
return get_format("SHORT_DATE_FORMAT", use_l10n=True)
|
||||||
|
|
||||||
def build_attrs(self, base_attrs, extra_attrs=None):
|
def build_attrs(self, base_attrs, extra_attrs=None):
|
||||||
@@ -120,12 +114,6 @@ class AirDateTimePickerInput(widgets.DateTimeInput):
|
|||||||
if self.format:
|
if self.format:
|
||||||
return self.format
|
return self.format
|
||||||
|
|
||||||
if self.user and hasattr(self.user, "settings"):
|
|
||||||
user_format = self.user.settings.datetime_format
|
|
||||||
if user_format == "SHORT_DATETIME_FORMAT":
|
|
||||||
return get_format("SHORT_DATETIME_FORMAT", use_l10n=True)
|
|
||||||
return user_format
|
|
||||||
|
|
||||||
return get_format("SHORT_DATETIME_FORMAT", use_l10n=True)
|
return get_format("SHORT_DATETIME_FORMAT", use_l10n=True)
|
||||||
|
|
||||||
def build_attrs(self, base_attrs, extra_attrs=None):
|
def build_attrs(self, base_attrs, extra_attrs=None):
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
from decimal import Decimal, InvalidOperation
|
from decimal import Decimal, InvalidOperation
|
||||||
|
|
||||||
from django import forms
|
from django import forms
|
||||||
from django.utils.formats import get_format, number_format
|
from django.utils.formats import number_format
|
||||||
|
|
||||||
|
from apps.common.functions.format import get_format
|
||||||
|
|
||||||
|
|
||||||
def convert_to_decimal(value: str):
|
def convert_to_decimal(value: str):
|
||||||
|
|||||||
Reference in New Issue
Block a user