mirror of
https://gitlab.com/brutaldon/brutaldon
synced 2025-01-09 22:44:02 +01:00
Add timezone handling to settings and middleware
This commit is contained in:
parent
1d240339de
commit
b851462b12
@ -1,11 +1,14 @@
|
||||
from django import forms
|
||||
from django.conf import settings
|
||||
from pytz import common_timezones
|
||||
|
||||
PRIVACY_CHOICES = (('public', 'Public'),
|
||||
('unlisted', 'Unlisted'),
|
||||
('private', 'Private'),
|
||||
('direct', 'Direct'))
|
||||
|
||||
timezones = [ (tz, tz) for tz in common_timezones]
|
||||
|
||||
MAX_LENGTH = settings.TOOT_MAX_LENGTH
|
||||
|
||||
class LoginForm(forms.Form):
|
||||
@ -35,6 +38,11 @@ class SettingsForm(forms.Form):
|
||||
help_text=
|
||||
"""Should replies be filtered out of your home timeline, giving you only pure,
|
||||
Original Content?""")
|
||||
timezone = forms.ChoiceField(label="Your local timezone",
|
||||
choices=timezones,
|
||||
required=False,
|
||||
help_text=
|
||||
"""What time zone do you prefer to have times displayed in? The default choice is UTC.""")
|
||||
|
||||
|
||||
class PostForm(forms.Form):
|
||||
|
12
brutaldon/middleware/timezone.py
Normal file
12
brutaldon/middleware/timezone.py
Normal file
@ -0,0 +1,12 @@
|
||||
import pytz
|
||||
|
||||
from django.utils import timezone
|
||||
from django.utils.deprecation import MiddlewareMixin
|
||||
|
||||
class TimezoneMiddleware(MiddlewareMixin):
|
||||
def process_request(self, request):
|
||||
tzname = request.session.get('timezone', "UTC")
|
||||
if tzname:
|
||||
timezone.activate(pytz.timezone(tzname))
|
||||
else:
|
||||
timezone.deactivate()
|
@ -51,6 +51,7 @@ MIDDLEWARE = [
|
||||
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
||||
'django.contrib.messages.middleware.MessageMiddleware',
|
||||
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
||||
'brutaldon.middleware.timezone.TimezoneMiddleware',
|
||||
]
|
||||
|
||||
ROOT_URLCONF = 'brutaldon.urls'
|
||||
|
@ -18,11 +18,21 @@
|
||||
</label>
|
||||
</div>
|
||||
<div class="field">
|
||||
<label class="label checkbox">
|
||||
<label class="label checkbox"">
|
||||
{% render_field form.filter_boosts %}
|
||||
{{ form.filter_boosts.label }}
|
||||
</label>
|
||||
</div>
|
||||
<div class="field">
|
||||
<label class="label" for="timezone">{{ form.timezone.label }}</label>
|
||||
<div class="control has-icons-left">
|
||||
<div class="select">
|
||||
{% render_field form.timezone class+="select" %}
|
||||
<span class="icon is-small is-left">
|
||||
<span class="fa fa-clock-o"></span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="field">
|
||||
<input type="submit" name="submit"
|
||||
value="Save" class="button is-primary" >
|
||||
|
@ -1,5 +1,6 @@
|
||||
from datetime import datetime, timedelta
|
||||
from django.utils.timezone import get_default_timezone, get_current_timezone, make_naive
|
||||
from django.utils.timezone import get_default_timezone, get_current_timezone, localtime
|
||||
from django.utils.timezone import now as django_now
|
||||
from django import template
|
||||
|
||||
register = template.Library()
|
||||
@ -24,16 +25,20 @@ def humane_time(arg):
|
||||
It is not safe to use on future times.
|
||||
|
||||
'''
|
||||
now = datetime.now()
|
||||
arg = make_naive(arg, timezone=get_current_timezone())
|
||||
now = django_now()
|
||||
arg = localtime(arg)
|
||||
diff = now - arg
|
||||
|
||||
if arg.tzinfo == now.tzinfo:
|
||||
utc = " (UTC)"
|
||||
else:
|
||||
utc = ""
|
||||
if diff < timedelta(hours=6):
|
||||
return arg.strftime("%a, %b %d, %Y at %I:%M %p")
|
||||
return arg.strftime("%a, %b %d, %Y at %I:%M %p") + utc
|
||||
elif diff < timedelta(hours=12):
|
||||
return arg.strftime("%a, %b %d, %Y around %I %p")
|
||||
elif diff < timedelta(days=2):
|
||||
return arg.strftime("%a, %b %d, %Y in the ") + time_of_day(arg.hour)
|
||||
return arg.strftime("%a, %b %d, %Y around %I %p") + utc
|
||||
elif diff < timedelta(hours=36):
|
||||
return arg.strftime("%a, %b %d, %Y in the ") + time_of_day(arg.hour) + utc
|
||||
elif diff < timedelta(days=6*28):
|
||||
return arg.strftime("%b %d, %Y")
|
||||
elif diff < timedelta(days=10*365):
|
||||
|
@ -321,6 +321,7 @@ def settings(request):
|
||||
request.session['fullbrutalism'] = form.cleaned_data['fullbrutalism']
|
||||
request.session['filter_replies'] = form.cleaned_data['filter_replies']
|
||||
request.session['filter_boosts'] = form.cleaned_data['filter_boosts']
|
||||
request.session['timezone'] = form.cleaned_data['timezone']
|
||||
return redirect(home)
|
||||
else:
|
||||
return render(request, 'setup/settings.html',
|
||||
@ -328,7 +329,12 @@ def settings(request):
|
||||
'own_acct': request.session['user'],
|
||||
'fullbrutalism': fullbrutalism_p(request)})
|
||||
else:
|
||||
form = SettingsForm(request.session)
|
||||
form = SettingsForm(initial={
|
||||
"fullbrutalism": fullbrutalism_p(request),
|
||||
"filter_replies": request.session.get('filter_replies', False),
|
||||
"filter_boosts": request.session.get('filter_boosts', False),
|
||||
"timezone": request.session.get('timezone', 'UTC')
|
||||
})
|
||||
return render(request, 'setup/settings.html',
|
||||
{ 'form': form,
|
||||
'own_acct': request.session['user'],
|
||||
|
Loading…
Reference in New Issue
Block a user