From de79cba49df605cf73ca6281321f9ae93d68cd51 Mon Sep 17 00:00:00 2001 From: MatthiasSaihttam Date: Tue, 12 Oct 2021 09:11:24 -0400 Subject: [PATCH] Refactor timezone logic into methods of Thought --- jetforce_app.py | 10 ---------- main/models.py | 13 +++++++++++++ main/templates/whispermaphone/index.gmi | 2 +- main/templates/whispermaphone/index.html | 5 +++-- main/views.py | 10 ---------- 5 files changed, 17 insertions(+), 23 deletions(-) diff --git a/jetforce_app.py b/jetforce_app.py index a2226a5..718d1be 100644 --- a/jetforce_app.py +++ b/jetforce_app.py @@ -20,16 +20,6 @@ app = JetforceApplication() def index(request): thoughts = Thought.objects.order_by("-posted") - for thought in thoughts: - thought.timezone = timezone.get_fixed_timezone(-thought.timezone_offset) - offset_hours = -thought.timezone_offset / 60 - if offset_hours == int(offset_hours): - offset_hours = int(offset_hours) - if offset_hours > 0: - offset_hours = "+" + str(offset_hours) - thought.offset_hours = offset_hours - - rendered_text = render_to_string("whispermaphone/index.gmi", { "thoughts": thoughts, }) diff --git a/main/models.py b/main/models.py index f7f86a7..470c60f 100644 --- a/main/models.py +++ b/main/models.py @@ -5,6 +5,7 @@ import magic from django.db import models from django import forms +from django.utils import timezone from django.utils.text import normalize_newlines from django.core.exceptions import ValidationError @@ -24,6 +25,18 @@ class Thought(models.Model): else: return os.path.splitext(self.media.path)[1][1:] + def get_timezone(self): + return timezone.get_fixed_timezone(-self.timezone_offset) + + def get_offset_hours(self): + offset_hours = -self.timezone_offset / 60 + # Convert 4.0 to 4 + if offset_hours == int(offset_hours): + offset_hours = int(offset_hours) + if offset_hours > 0: + offset_hours = "+" + str(offset_hours) + return str(offset_hours) + # Honestly I'm so sick of this problem that writing out a comment here to explain why it is necessary is beyond me. # I'm calling this CharField and not MySpecialLineNormalizingCharField diff --git a/main/templates/whispermaphone/index.gmi b/main/templates/whispermaphone/index.gmi index 8d51a2a..a2d702c 100644 --- a/main/templates/whispermaphone/index.gmi +++ b/main/templates/whispermaphone/index.gmi @@ -6,7 +6,7 @@ {% endif %}{% if thought.media %} => gemini://thoughts.learnerpages.com{{ thought.media.url }}{% endif %}{% load tz %} ``` -{% timezone thought.timezone %}{{ thought.posted|time:"g:i a" }} {{ thought.posted|date:"M d, Y" }}, UTC{{ thought.offset_hours }}{% endtimezone %} +{% timezone thought.get_timezone %}{{ thought.posted|time:"g:i a" }} {{ thought.posted|date:"M d, Y" }}, UTC{{ thought.get_offset_hours }}{% endtimezone %} ``` ▔▔▔ {% endfor %} diff --git a/main/templates/whispermaphone/index.html b/main/templates/whispermaphone/index.html index dcfe070..47405be 100644 --- a/main/templates/whispermaphone/index.html +++ b/main/templates/whispermaphone/index.html @@ -41,11 +41,12 @@ {% endwith %}
- {% timezone thought.timezone %} + {% timezone thought.get_timezone %} {{ thought.posted|time:"g:i a" }} {{ thought.posted|date:"M d, Y" }}, - UTC{{ thought.offset_hours }} + UTC{{ thought.get_offset_hours }} + {{ thought.get_season }} {% endtimezone %}
diff --git a/main/views.py b/main/views.py index 973874a..b632b0f 100644 --- a/main/views.py +++ b/main/views.py @@ -5,7 +5,6 @@ import subprocess import magic from django.shortcuts import render -from django.utils import timezone from django.utils.crypto import constant_time_compare from whispermaphone import settings @@ -32,15 +31,6 @@ def index(request): thoughts = Thought.objects.order_by("-posted") - for thought in thoughts: - thought.timezone = timezone.get_fixed_timezone(-thought.timezone_offset) - offset_hours = -thought.timezone_offset / 60 - if offset_hours == int(offset_hours): - offset_hours = int(offset_hours) - if offset_hours > 0: - offset_hours = "+" + str(offset_hours) - thought.offset_hours = offset_hours - return render(request, "whispermaphone/index.html", { "thoughts": thoughts, "highlighted": highlighted_uuid,