Skip to content

Commit

Permalink
Properly display dates with TZ
Browse files Browse the repository at this point in the history
  • Loading branch information
avdata99 committed Apr 23, 2024
1 parent d721e87 commit 717c93d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
15 changes: 13 additions & 2 deletions ckanext/announcements/helpers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime
import logging
from pytz import timezone
from ckan import model
from ckan.plugins import toolkit
from ckanext.announcements.models import Announcement
Expand All @@ -11,15 +12,15 @@
def get_public_announcements():
"""Get a list of Announcements"""
# display messages up to 3 days after they disappeared.
now = datetime.datetime.utcnow()
now = datetime.datetime.now()
messages = (
model.Session.query(Announcement)
.filter_by(status="active")
.filter(Announcement.to_date > now, Announcement.from_date < now)
.limit(10)
.all()
)

messages = _apply_tz(messages)
return messages


Expand All @@ -33,5 +34,15 @@ def get_all_announcements():
.limit(limit)
.all()
)
messages = _apply_tz(messages)
return messages


def _apply_tz(messages):
"""Apply the timezone to the messages"""
display_timezone = toolkit.config.get("ckan.display_timezone")
tz = timezone(display_timezone)
for message in messages:
message.from_date = message.from_date.astimezone(tz)
message.to_date = message.to_date.astimezone(tz)
return messages
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ <h3>Are you sure you want to delete this announcement?</h3>
<div style="max-height: 400px; overflow-y: scroll;">
<h4>Time Period</h4>
<ul>
<li>From: <b>{{ message.from_date.isoformat() }}</b></li>
<li>To: <b>{{ message.to_date.isoformat() }}</b></li>
<li>From: <b>{{ message.from_date.strftime('%Y-%m-%dT%H:%M:%S') }}</b></li>
<li>To: <b>{{ message.to_date.strftime('%Y-%m-%dT%H:%M:%S') }}</b></li>
</ul>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ <h4>Time Period</h4>
<div class="form-group">
<label for="from_date">From</label>
<div class="controls ">
<input name="from_date" type="datetime-local" value="{{ message.from_date.isoformat() }}" class="announcement-date-input"/>
<input name="from_date" type="datetime-local" value="{{ message.from_date.strftime('%Y-%m-%dT%H:%M:%S') }}" class="announcement-date-input"/>
</div>
</div>

<div class="form-group">
<label for="to_date">To</label>
<div class="controls ">
<input name="to_date" type="datetime-local" value="{{ message.to_date.isoformat() }}" class="announcement-date-input"/>
<input name="to_date" type="datetime-local" value="{{ message.to_date.strftime('%Y-%m-%dT%H:%M:%S') }}" class="announcement-date-input"/>
</div>
</div>

Expand Down
4 changes: 2 additions & 2 deletions ckanext/announcements/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class Meta:

_user = helpers.call_action("get_site_user")
user_creator_id = _user["name"]
from_date = datetime.datetime.utcnow() + datetime.timedelta(days=3)
to_date = datetime.datetime.utcnow() + datetime.timedelta(days=7)
from_date = datetime.datetime.now() + datetime.timedelta(days=3)
to_date = datetime.datetime.now() + datetime.timedelta(days=7)
message = "This is an announcement message"
status = "active"

Expand Down

0 comments on commit 717c93d

Please sign in to comment.