From 765e6b542ff9782509b2caa7c17e41823c804050 Mon Sep 17 00:00:00 2001 From: Pablo Barton Date: Mon, 29 Mar 2021 08:20:35 -0400 Subject: [PATCH] adding middleware and use in readthrough template --- bookwyrm/settings.py | 1 + bookwyrm/templates/snippets/readthrough.html | 5 +++-- bookwyrm/timezone_middleware.py | 17 +++++++++++++++++ bookwyrm/views/reading.py | 1 - 4 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 bookwyrm/timezone_middleware.py diff --git a/bookwyrm/settings.py b/bookwyrm/settings.py index b615fa293..e671f07c5 100644 --- a/bookwyrm/settings.py +++ b/bookwyrm/settings.py @@ -66,6 +66,7 @@ MIDDLEWARE = [ "django.middleware.common.CommonMiddleware", "django.middleware.csrf.CsrfViewMiddleware", "django.contrib.auth.middleware.AuthenticationMiddleware", + "bookwyrm.timezone_middleware.TimezoneMiddleware", "django.contrib.messages.middleware.MessageMiddleware", "django.middleware.clickjacking.XFrameOptionsMiddleware", ] diff --git a/bookwyrm/templates/snippets/readthrough.html b/bookwyrm/templates/snippets/readthrough.html index 82f6a088c..3e694f616 100644 --- a/bookwyrm/templates/snippets/readthrough.html +++ b/bookwyrm/templates/snippets/readthrough.html @@ -1,5 +1,6 @@ {% load i18n %} {% load humanize %} +{% load tz %}
@@ -8,7 +9,7 @@
    {% if readthrough.finish_date or readthrough.progress %} -
  • {% if readthrough.finish_date %} {{ readthrough.finish_date | naturalday }}: {% trans "finished" %} {% else %}{% if readthrough.progress_mode == 'PG' %}on page {{ readthrough.progress }}{% if book.pages %} of {{ book.pages }}{% endif %} +
  • {% if readthrough.finish_date %} {{ readthrough.finish_date | localtime | naturalday }}: {% trans "finished" %} {% else %}{% if readthrough.progress_mode == 'PG' %}on page {{ readthrough.progress }}{% if book.pages %} of {{ book.pages }}{% endif %} {% else %}{{ readthrough.progress }}%{% endif %}{% endif %} {% if readthrough.progress %} {% trans "Show all updates" as button_text %} @@ -37,7 +38,7 @@ {% endif %}
  • {% endif %} -
  • {{ readthrough.start_date | naturalday }}: {% trans "started" %}
  • +
  • {{ readthrough.start_date | localtime | naturalday }}: {% trans "started" %}
diff --git a/bookwyrm/timezone_middleware.py b/bookwyrm/timezone_middleware.py new file mode 100644 index 000000000..633d6835d --- /dev/null +++ b/bookwyrm/timezone_middleware.py @@ -0,0 +1,17 @@ +import pytz + +from django.utils import timezone + + +class TimezoneMiddleware: + def __init__(self, get_response): + self.get_response = get_response + + def __call__(self, request): + if request.user.is_authenticated: + timezone.activate(pytz.timezone(request.user.preferred_timezone)) + else: + timezone.activate(pytz.utc) + response = self.get_response(request) + timezone.deactivate() + return response diff --git a/bookwyrm/views/reading.py b/bookwyrm/views/reading.py index 27e95855b..59acdf6cb 100644 --- a/bookwyrm/views/reading.py +++ b/bookwyrm/views/reading.py @@ -7,7 +7,6 @@ from dateutil.parser import ParserError from django.contrib.auth.decorators import login_required from django.http import HttpResponseBadRequest, HttpResponseNotFound from django.shortcuts import get_object_or_404, redirect -from django.utils import timezone from django.views.decorators.http import require_POST from bookwyrm import models