adding middleware and use in readthrough template

This commit is contained in:
Pablo Barton 2021-03-29 08:20:35 -04:00
parent 224c7e4f0e
commit 765e6b542f
4 changed files with 21 additions and 3 deletions

View file

@ -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",
]

View file

@ -1,5 +1,6 @@
{% load i18n %}
{% load humanize %}
{% load tz %}
<div class="content block">
<div id="hide-edit-readthrough-{{ readthrough.id }}">
<div class="columns">
@ -8,7 +9,7 @@
</dl>
<ul>
{% if readthrough.finish_date or readthrough.progress %}
<li>{% 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 %}
<li>{% 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 %}
</li>
{% endif %}
<li>{{ readthrough.start_date | naturalday }}: {% trans "started" %}</li>
<li>{{ readthrough.start_date | localtime | naturalday }}: {% trans "started" %}</li>
</ul>
</div>
<div class="column is-narrow">

View file

@ -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

View file

@ -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