From 14c396d483a85e9f68168bf8344bac2014dac454 Mon Sep 17 00:00:00 2001 From: lizerk Date: Fri, 20 Mar 2026 00:09:57 +0300 Subject: [PATCH] Fix timezone compatibility: Properly implement _UtcCompat as tzinfo subclass - Made _UtcCompat inherit from datetime.tzinfo instead of plain class - Implemented required tzinfo methods: utcoffset, tzname, dst - Fixes TypeError: tzinfo argument must be None or of a tzinfo subclass - Resolves admin panel crashes when viewing LDAP users with timestamps --- backend/employees/ldap/orm_models.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/backend/employees/ldap/orm_models.py b/backend/employees/ldap/orm_models.py index 5cf820e0..2e964d94 100644 --- a/backend/employees/ldap/orm_models.py +++ b/backend/employees/ldap/orm_models.py @@ -105,10 +105,29 @@ from django.utils import timezone as _dj_tz # Compatibility: django-ldapdb 1.5.1 uses timezone.utc removed in Django 5.x if not hasattr(_dj_tz, 'utc'): - class _UtcCompat: - """pytz-compatible UTC stub for django-ldapdb.""" + class _UtcCompat(_dt.tzinfo): + """pytz-compatible UTC wrapper for django-ldapdb. + + Acts as a tzinfo subclass but delegates to datetime.timezone.utc + and provides the localize() method for pytz compatibility. + """ + def localize(self, dt): + """pytz-style localize method.""" return dt.replace(tzinfo=_dt.timezone.utc) + + def utcoffset(self, dt): + return _dt.timedelta(0) + + def tzname(self, dt): + return "UTC" + + def dst(self, dt): + return _dt.timedelta(0) + + def __repr__(self): + return "UTC" + _dj_tz.utc = _UtcCompat() from ldapdb.models import Model as LdapModel