silentsokolov / django-admin-rangefilter

A Django app that lets you filter data by date range and numeric range in the admin UI
MIT License
721 stars 106 forks source link

Use current timezone instead of default #114

Closed rwlogel closed 9 months ago

rwlogel commented 9 months ago

If the timezone is set using this approach from the Django docs then it should be using the current timezone not the default one.

codecov-commenter commented 9 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Comparison is base (b4f6c29) 87.45% compared to head (94a5570) 87.45%.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #114 +/- ## ======================================= Coverage 87.45% 87.45% ======================================= Files 9 9 Lines 606 606 ======================================= Hits 530 530 Misses 76 76 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

silentsokolov commented 9 months ago

Are you sure that it works correct for the django-admin <-> model time fields?

rwlogel commented 9 months ago

I verified it in our project which has implemented the admin timezone switching feature as outlined in the Django documentation which I linked in the description.

A simple way to see it in action would be to use a middleware that locks the timezone:

class AdminTimezoneMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        if request.path.startswith('/admin/'):
            with timezone.override(ZoneInfo('America/New_York')):
                return self.get_response(request)

        return self.get_response(request)

With this middleware all the datetimes shown use the wrapped timezone, except the ones generated by the filter which are always UTC because that is the default timezone, not the active one.

silentsokolov commented 9 months ago

Checked. Thanks you