Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 5 additions & 27 deletions django/cohiva/formats/de/formats.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,8 @@
"""
Custom date/time format overrides for Swiss German (de-ch).

This file is used when LANGUAGE_CODE is set to 'de-ch' but allows
overriding Django's default de_CH formats if needed.
Currently we're using Django's built-in de_CH locale which already
has the correct Swiss German formats.
This module kept the Swiss German formats under the "de" path for historical
reasons. It now delegates to the canonical `de_ch` module so both
`cohiva.formats.de` and `cohiva.formats.de_ch` work.
"""

# Import Django's de_CH formats as base
from django.conf.locale.de_CH.formats import * # noqa: F403

# Override DATE_FORMAT to use shorter format (d.m.Y instead of j. F Y)
DATE_FORMAT = "d.m.Y"
DATETIME_FORMAT = "d.m.Y H:i"

# Add ISO format as additional input option
DATE_INPUT_FORMATS = [
"%d.%m.%Y", # Swiss format: 25.12.2024
"%d.%m.%y", # Swiss short: 25.12.24
"%Y-%m-%d", # ISO format: 2024-12-25 (added)
]

DATETIME_INPUT_FORMATS = [
"%d.%m.%Y %H:%M:%S",
"%d.%m.%Y %H:%M:%S.%f",
"%d.%m.%Y %H:%M",
"%Y-%m-%d %H:%M:%S", # ISO format (added)
"%Y-%m-%d %H:%M", # ISO format (added)
]
# Import everything from the new de_ch module to preserve behavior.
from cohiva.formats.de_ch.formats import * # noqa: F401,F403
34 changes: 34 additions & 0 deletions django/cohiva/formats/de_ch/formats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""
Custom date/time format overrides for Swiss German (de-ch).

This file provides Swiss German formats for Django when
`LANGUAGE_CODE` is set to 'de-ch'. It mirrors the previous
`cohiva/formats/de/formats.py` behavior but uses the
module name expected for regional code: `de_ch`.
"""

# Import Django's de_CH formats as base
from django.conf.locale.de_CH.formats import * # noqa: F403

# Override DATE_FORMAT to use shorter format (d.m.Y instead of j. F Y)
DATE_FORMAT = "d.m.Y"
DATETIME_FORMAT = "d.m.Y H:i"

# Add ISO format as additional input option
DATE_INPUT_FORMATS = [
"%d.%m.%Y", # Swiss format: 25.12.2024
"%d.%m.%y", # Swiss short: 25.12.24
"%Y-%m-%d", # ISO format: 2024-12-25 (added)
]

DATETIME_INPUT_FORMATS = [
"%d.%m.%Y %H:%M:%S",
"%d.%m.%Y %H:%M:%S.%f",
"%d.%m.%Y %H:%M",
"%Y-%m-%d %H:%M:%S", # ISO format (added)
"%Y-%m-%d %H:%M", # ISO format (added)
]
THOUSAND_SEPARATOR = "'"
DECIMAL_SEPARATOR = "."
NUMBER_GROUPING = 3

3 changes: 3 additions & 0 deletions django/cohiva/settings_defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -1433,3 +1433,6 @@
# Theme Customization
COHIVA_TITLE_FONT = "Lato"
COHIVA_TEXT_FONT = "Liberation Serif"

USE_L10N = True
USE_THOUSAND_SEPARATOR = True
54 changes: 52 additions & 2 deletions django/geno/admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime
from decimal import Decimal, InvalidOperation

from dateutil.relativedelta import relativedelta
from django import forms
Expand Down Expand Up @@ -59,9 +60,39 @@
if settings.DEBUG:
raise
messages.error(request, "Objekte dieses Typs können nicht kopiert werden.")
return
messages.success(request, f"{count} Objekt(e) kopiert.")

Check warning on line 64 in django/geno/admin.py

View workflow job for this annotation

GitHub Actions / test

Missing coverage

Missing coverage on lines 63-64

class ApostropheDecimalField(forms.DecimalField):
def to_python(self, value):
if value in self.empty_values:
return None

if isinstance(value, str):
value = value.replace("'", "").strip()

return super().to_python(value)

Check warning on line 74 in django/geno/admin.py

View workflow job for this annotation

GitHub Actions / test

Missing coverage

Missing coverage on lines 68-74

def prepare_value(self, value):
"""Return a plain numeric string (dot as decimal sep, no thousands) for the widget's value.
This ensures the original input's value is a machine-friendly number (e.g. 1234.56)
and lets the admin JS format it with apostrophes for display.
"""
if value in self.empty_values or value is None:
return ""
try:
# Handle Decimal/float/int and strings with possible thousands/commas
if isinstance(value, Decimal):
d = value.quantize(Decimal("0.01"))
return format(d, 'f')
# string or other numeric
s = str(value).replace("'", "").replace(" ", "").replace(",", ".")
d = Decimal(s)
d = d.quantize(Decimal("0.01"))
return format(d, 'f')
except Exception:
# fallback to default representation
return str(value)

Check warning on line 95 in django/geno/admin.py

View workflow job for this annotation

GitHub Actions / test

Missing coverage

Missing coverage on lines 81-95

class BooleanFieldDefaultTrueListFilter(admin.BooleanFieldListFilter):
"""
Expand Down Expand Up @@ -143,6 +174,25 @@
if setting_name in settings.COHIVA_ADMIN_FIELDS[module_name]:
setattr(self, attr, settings.COHIVA_ADMIN_FIELDS[module_name][setting_name])

def get_form(self, request, obj=None, **kwargs):
"""Ensure DecimalFields rendered in admin forms get the widget attributes
required for apostrophe formatting JS: data-apostrophe, lang, inputmode, step.
This applies globally to admin forms inheriting from GenoBaseAdmin.
"""
form = super().get_form(request, obj, **kwargs)
try:
for _name, field in form.base_fields.items():
if isinstance(field, forms.DecimalField):
w = field.widget
w.attrs.setdefault("data-apostrophe", "1")
w.attrs.setdefault("inputmode", "decimal")
w.attrs.setdefault("step", "0.01")
w.attrs.setdefault("lang", "en")
except Exception:
# keep behaviour stable if something unexpected occurs
pass

Check warning on line 193 in django/geno/admin.py

View workflow job for this annotation

GitHub Actions / test

Missing coverage

Missing coverage on lines 191-193
return form


@admin.display(description="Anrede auf 'Herr' setzen")
def set_title_mr(modeladmin, request, queryset):
Expand Down Expand Up @@ -984,12 +1034,13 @@

def get_form(self, request, obj=None, **kwargs):
# just save obj reference for future processing in Inline
request._obj_ = obj
return super().get_form(request, obj, **kwargs)

Check warning on line 1038 in django/geno/admin.py

View workflow job for this annotation

GitHub Actions / test

Missing coverage

Missing coverage on lines 1037-1038


@admin.decorators.register(RentalUnit)
class RentalUnitAdmin(GenoBaseAdmin):
class Media:
js = ("geno/js/apostrophe_decimal.js",)
fields = [
"name",
("label", "label_short"),
Expand Down Expand Up @@ -1680,7 +1731,6 @@
search_fields = ["name", "comment", "value"]
list_filter = ["name", "ts_created", "ts_modified", "content_type"]


## Unregister default admin classes and re-register with unfold classes to provide the correct
## styling.
admin.site.unregister(User)
Expand Down
4 changes: 2 additions & 2 deletions django/geno/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,11 +570,11 @@ def get_context(self):
today = datetime.date.today()
c["datum"] = today.strftime("%d.%m.%Y")
c["monat"] = today.strftime("%B")
c["jahr"] = today.year
c["jahr"] = str(today.year)
today_plus30 = today + datetime.timedelta(days=30)
c["datum_plus30"] = today_plus30.strftime("%d.%m.%Y")
c["monat_plus30"] = today_plus30.strftime("%B")
c["jahr_plus30"] = today_plus30.year
c["jahr_plus30"] = str(today_plus30.year)

c["org_info"] = settings.GENO_ORG_INFO

Expand Down
2 changes: 1 addition & 1 deletion django/geno/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@
4: "Flag 4", # Projekt unterstützen
5: "Flag 5", # Dranbleiben
},
)
)
Loading
Loading