Fix bugs around editing and time

* Pre-fill the timezone with the offset in hours (correct), instead of minutes, like the DB stores
* Only change the timezone in JS if you're not editing
* Redirect back to a Thought after editing it
This commit is contained in:
Matthias Portzel 2022-01-12 11:08:56 -05:00
parent 1ad106d05d
commit 7da4921d32
3 changed files with 9 additions and 1 deletions

View File

@ -3,8 +3,8 @@ import uuid
import magic import magic
from django.db import models
from django import forms from django import forms
from django.db import models
from django.utils import timezone from django.utils import timezone
from django.utils.text import normalize_newlines from django.utils.text import normalize_newlines
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
@ -37,6 +37,9 @@ class Thought(models.Model):
offset_hours = "+" + str(offset_hours) offset_hours = "+" + str(offset_hours)
return str(offset_hours) return str(offset_hours)
def get_absolute_url(self):
return f"/?show={str(self.uuid)}"
# Honestly I'm so sick of this problem that writing out a comment here to explain why it is necessary is beyond me. # 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 # I'm calling this CharField and not MySpecialLineNormalizingCharField

View File

@ -129,6 +129,8 @@
timezoneOffsetEl.setAttribute("type", "hidden"); timezoneOffsetEl.setAttribute("type", "hidden");
// The text box needs to be in hours, UTC offset (e.g. -8) // The text box needs to be in hours, UTC offset (e.g. -8)
// .getTimezoneOffset() returns minutes behind UTC // .getTimezoneOffset() returns minutes behind UTC
{% if not editing %}
timezoneOffsetEl.value = -(new Date()).getTimezoneOffset() / 60; timezoneOffsetEl.value = -(new Date()).getTimezoneOffset() / 60;
{% endif %}
</script> </script>
{% endblock %} {% endblock %}

View File

@ -86,6 +86,7 @@ def post(request):
editing = request.GET.get("editing", None) editing = request.GET.get("editing", None)
try: try:
editing_thought = Thought.objects.get(uuid=editing) editing_thought = Thought.objects.get(uuid=editing)
editing_thought.timezone_offset = - editing_thought.timezone_offset / 60
except Thought.DoesNotExist: except Thought.DoesNotExist:
editing_thought = None editing_thought = None
@ -151,6 +152,8 @@ def post(request):
thought.save() thought.save()
# Redirect to the same page, so that we make a GET request to /post # Redirect to the same page, so that we make a GET request to /post
if editing_thought:
return redirect(editing_thought)
return redirect("post") return redirect("post")
return render(request, "whispermaphone/post.html", { return render(request, "whispermaphone/post.html", {