Refactor timezone logic into methods of Thought

This commit is contained in:
MatthiasSaihttam 2021-10-12 09:11:24 -04:00
parent c5dc1cbc05
commit de79cba49d
5 changed files with 17 additions and 23 deletions

View File

@ -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,
})

View File

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

View File

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

View File

@ -41,11 +41,12 @@
{% endwith %}
<div class="thought-end">
<span class="timestamp">
{% 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 %}
</span>
</div>

View File

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