Skip to content

Commit

Permalink
Allow timezones
Browse files Browse the repository at this point in the history
  • Loading branch information
avdata99 committed Apr 22, 2024
1 parent 70728f9 commit 7266fbc
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 14 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ To install ckanext-announcements:
`ckanext.announcements.limit_announcements` (default: 50)
> Limit for the announcement list
`ckan.display_timezone`
> Start and end dates are displayed in the timezone defined in this CKAN core setting
## Developer installation

To install ckanext-announcements for development, activate your CKAN virtualenv and
Expand Down
10 changes: 5 additions & 5 deletions ckanext/announcements/actions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from datetime import datetime
from ckan import model
from ckan.plugins import toolkit
from ckanext.announcements.models import Announcement
Expand All @@ -9,8 +8,9 @@ def announcement_create(context, data_dict):
toolkit.check_access("announcement_create", context, data_dict)
m = context.get("model", model)
validate_announcement(data_dict)
from_date = datetime.fromisoformat(data_dict.get("from_date"))
to_date = datetime.fromisoformat(data_dict.get("to_date"))

from_date = data_dict.get("from_date")
to_date = data_dict.get("to_date")

announcement = Announcement(
timestamp=data_dict["timestamp"],
Expand All @@ -37,8 +37,8 @@ def announcement_update(context, data_dict):
if not announcement:
raise toolkit.ObjectNotFound("Announcement not found")

announcement.from_date = datetime.fromisoformat(data_dict["from_date"])
announcement.to_date = datetime.fromisoformat(data_dict["to_date"])
announcement.from_date = data_dict["from_date"]
announcement.to_date = data_dict["to_date"]
announcement.message = data_dict["message"]

m.Session.commit()
Expand Down
34 changes: 29 additions & 5 deletions ckanext/announcements/blueprints.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from datetime import datetime
import logging
import pytz
from flask import Blueprint
from ckan.lib import base
from ckan.plugins import toolkit


log = logging.getLogger(__name__)
announcements_blueprint = Blueprint(
"announcements", __name__, url_prefix="/ckan-admin/announcements"
)
Expand All @@ -13,16 +16,37 @@ def index():
"""Get the Announcements home page"""
if not toolkit.c.userobj.sysadmin:
base.abort(403, ("Need to be system administrator to administer"))
return toolkit.render("admin/announcements.html")

display_timezone = toolkit.config.get("ckan.display_timezone")
pytz_timezones = pytz.all_timezones.copy()
# remove CET and UTC to display them first
pytz_timezones.remove("CET")
pytz_timezones.remove("UTC")
ctx = {
"display_timezone": display_timezone,
"timezones": ["UTC", "CET"] + sorted(pytz_timezones),
}
return toolkit.render("admin/announcements.html", extra_vars=ctx)


def get_dates(form):
from_date = form.get("from_date")
to_date = form.get("to_date")
timezone = form.get("timezone")
# apply the selected tuimezone
from_date = datetime.strptime(from_date, "%Y-%m-%dT%H:%M")
from_date = pytz.timezone(timezone).localize(from_date)
to_date = datetime.strptime(to_date, "%Y-%m-%dT%H:%M")
to_date = pytz.timezone(timezone).localize(to_date)
return from_date, to_date


def create():
"""Create (POST) a new announcement"""

user_obj = toolkit.c.userobj
user_creator_id = user_obj.id
from_date = toolkit.request.form.get("from_date")
to_date = toolkit.request.form.get("to_date")
from_date, to_date = get_dates(toolkit.request.form)
message = toolkit.request.form.get("message")

new_announcements_data = {
Expand Down Expand Up @@ -50,8 +74,8 @@ def update():

user_obj = toolkit.c.userobj
announ_id = toolkit.request.form.get("id")
from_date = toolkit.request.form.get("from_date")
to_date = toolkit.request.form.get("to_date")
from_date, to_date = get_dates(toolkit.request.form)

message = toolkit.request.form.get("message")

announcements_data = {
Expand Down
6 changes: 3 additions & 3 deletions ckanext/announcements/templates/admin/announcements.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ <h2>{{ _('Announcements to all users') }}</h2>
{% for message in messages %}
<tr>
<td>{{ h.render_markdown(message.message) }}</td>
<td>{{ h.render_datetime(message.from_date, with_hours=True) }}</td>
<td>{{ h.render_datetime(message.to_date, with_hours=True) }}</td>
<td>{{ h.render_datetime(message.from_date, with_hours=True) }} ({{ display_timezone }})</td>
<td>{{ h.render_datetime(message.to_date, with_hours=True) }} ({{ display_timezone }})</td>
<td>
<a href="#system-message-edit-{{ message.id }}" role="button" class="btn btn-info announcement-edit-btn" title="edit" data-bs-toggle="modal" data-toggle="modal" data-target="#system-message-edit-{{ message.id }}">
<i class="fa fa-edit"></i>
Expand All @@ -54,7 +54,7 @@ <h2>{{ _('Announcements to all users') }}</h2>

{% for message in messages %}

{% snippet "admin/snippets/edit-announcement.html", message=message %}
{% snippet "admin/snippets/edit-announcement.html", message=message, display_timezone=display_timezone, timezones=timezones %}
{% snippet "admin/snippets/delete-announcement.html", message=message %}

{% endfor %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ <h3>Message details</h3>

<h4>Time Period</h4>

<div class="form-group">
<label for="from_date">Timezone</label>
<div class="controls ">
<select name="timezone" class="announcement-timezone-input">
{% for tz in timezones %}
<option
{% if tz == display_timezone %}selected="selected"{% endif %}
value="{{ tz }}">{{ tz }}
</option>
{% endfor %}
</select>
</div>
</div>
<div class="form-group">
<label for="from_date">From</label>
<div class="controls ">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,19 @@ <h3>New Announcement</h3>
{{ form.markdown('message', id='field-message', label=_('Announcement message'), classes=["announcement-message-input"]) }}

<h4>Time Period</h4>

<div class="form-group">
<label for="from_date">Timezone</label>
<div class="controls ">
<select name="timezone" class="announcement-timezone-input">
{% for tz in timezones %}
<option
{% if tz == display_timezone %}selected="selected"{% endif %}
value="{{ tz }}">{{ tz }}
</option>
{% endfor %}
</select>
</div>
</div>
<div class="form-group">
<label for="from_date">From</label>
<div class="controls ">
Expand Down

0 comments on commit 7266fbc

Please sign in to comment.