mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-11-23 01:51:08 +00:00
Merge pull request #2491 from chdorner/register-tz-support
Detect preferred timezone via JavaScript on register
This commit is contained in:
commit
89478ac87e
4 changed files with 80 additions and 0 deletions
|
@ -48,6 +48,12 @@ let BookWyrm = new (class {
|
|||
document
|
||||
.querySelector("#barcode-scanner-modal")
|
||||
.addEventListener("open", this.openBarcodeScanner.bind(this));
|
||||
|
||||
document
|
||||
.querySelectorAll('form[name="register"]')
|
||||
.forEach((form) =>
|
||||
form.addEventListener("submit", (e) => this.setPreferredTimezone(e, form))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -785,4 +791,16 @@ let BookWyrm = new (class {
|
|||
|
||||
initBarcodes();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set preferred timezone in register form.
|
||||
*
|
||||
* @param {Event} event - `submit` event fired by the register form.
|
||||
* @return {undefined}
|
||||
*/
|
||||
setPreferredTimezone(event, form) {
|
||||
const tz = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
||||
|
||||
form.querySelector('input[name="preferred_timezone"]').value = tz;
|
||||
}
|
||||
})();
|
||||
|
|
|
@ -58,6 +58,8 @@
|
|||
</div>
|
||||
</div>
|
||||
|
||||
<input type="hidden" name="preferred_timezone" />
|
||||
|
||||
<div class="field">
|
||||
<div class="control">
|
||||
<button class="button is-primary" type="submit">
|
||||
|
|
|
@ -58,6 +58,7 @@ class RegisterViews(TestCase):
|
|||
"localname": "nutria-user.user_nutria",
|
||||
"password": "mouseword",
|
||||
"email": "aa@bb.cccc",
|
||||
"preferred_timezone": "Europe/Berlin",
|
||||
},
|
||||
)
|
||||
with patch("bookwyrm.views.landing.register.login"):
|
||||
|
@ -68,6 +69,7 @@ class RegisterViews(TestCase):
|
|||
self.assertEqual(nutria.username, f"nutria-user.user_nutria@{DOMAIN}")
|
||||
self.assertEqual(nutria.localname, "nutria-user.user_nutria")
|
||||
self.assertEqual(nutria.local, True)
|
||||
self.assertEqual(nutria.preferred_timezone, "Europe/Berlin")
|
||||
|
||||
@patch("bookwyrm.emailing.send_email.delay")
|
||||
def test_register_email_confirm(self, *_):
|
||||
|
@ -198,6 +200,58 @@ class RegisterViews(TestCase):
|
|||
self.assertEqual(models.User.objects.count(), 1)
|
||||
validate_html(response.render())
|
||||
|
||||
def test_register_default_preferred_timezone(self, *_):
|
||||
"""invalid preferred timezone strings should just default to UTC"""
|
||||
view = views.Register.as_view()
|
||||
self.assertEqual(models.User.objects.count(), 1)
|
||||
|
||||
request = self.factory.post(
|
||||
"register/",
|
||||
{
|
||||
"localname": "nutria1",
|
||||
"password": "mouseword",
|
||||
"email": "aa1@bb.cccc",
|
||||
"preferred_timezone": "invalid-tz",
|
||||
},
|
||||
)
|
||||
with patch("bookwyrm.views.landing.register.login"):
|
||||
response = view(request)
|
||||
self.assertEqual(response.status_code, 302)
|
||||
self.assertEqual(models.User.objects.count(), 2)
|
||||
nutria = models.User.objects.last()
|
||||
self.assertEqual(nutria.preferred_timezone, "UTC")
|
||||
|
||||
request = self.factory.post(
|
||||
"register/",
|
||||
{
|
||||
"localname": "nutria2",
|
||||
"password": "mouseword",
|
||||
"email": "aa2@bb.cccc",
|
||||
"preferred_timezone": "",
|
||||
},
|
||||
)
|
||||
with patch("bookwyrm.views.landing.register.login"):
|
||||
response = view(request)
|
||||
self.assertEqual(response.status_code, 302)
|
||||
self.assertEqual(models.User.objects.count(), 3)
|
||||
nutria = models.User.objects.last()
|
||||
self.assertEqual(nutria.preferred_timezone, "UTC")
|
||||
|
||||
request = self.factory.post(
|
||||
"register/",
|
||||
{
|
||||
"localname": "nutria3",
|
||||
"password": "mouseword",
|
||||
"email": "aa3@bb.cccc",
|
||||
},
|
||||
)
|
||||
with patch("bookwyrm.views.landing.register.login"):
|
||||
response = view(request)
|
||||
self.assertEqual(response.status_code, 302)
|
||||
self.assertEqual(models.User.objects.count(), 4)
|
||||
nutria = models.User.objects.last()
|
||||
self.assertEqual(nutria.preferred_timezone, "UTC")
|
||||
|
||||
def test_register_closed_instance(self, *_):
|
||||
"""you can't just register"""
|
||||
view = views.Register.as_view()
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
""" class views for login/register views """
|
||||
import pytz
|
||||
from django.contrib.auth import login
|
||||
from django.core.exceptions import PermissionDenied
|
||||
from django.shortcuts import get_object_or_404, redirect
|
||||
|
@ -55,6 +56,10 @@ class Register(View):
|
|||
localname = form.data["localname"].strip()
|
||||
email = form.data["email"]
|
||||
password = form.data["password"]
|
||||
try:
|
||||
preferred_timezone = pytz.timezone(form.data.get("preferred_timezone"))
|
||||
except pytz.exceptions.UnknownTimeZoneError:
|
||||
preferred_timezone = pytz.utc
|
||||
|
||||
# make sure the email isn't blocked as spam
|
||||
email_domain = email.split("@")[-1]
|
||||
|
@ -71,6 +76,7 @@ class Register(View):
|
|||
local=True,
|
||||
deactivation_reason="pending" if settings.require_confirm_email else None,
|
||||
is_active=not settings.require_confirm_email,
|
||||
preferred_timezone=preferred_timezone,
|
||||
)
|
||||
if invite:
|
||||
invite.times_used += 1
|
||||
|
|
Loading…
Reference in a new issue