Skip to content

Commit

Permalink
Merge pull request #33862 from dimagi/mk/domain-banners-schedule
Browse files Browse the repository at this point in the history
Add scheduling for domain banners
  • Loading branch information
mkangia committed Dec 13, 2023
2 parents e8ea229 + 7dd236f commit 1c666d8
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 6 deletions.
22 changes: 20 additions & 2 deletions corehq/apps/domain/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
BooleanField,
CharField,
ChoiceField,
DateTimeField,
Field,
ImageField,
IntegerField,
Expand Down Expand Up @@ -110,7 +111,7 @@
all_restricted_ucr_expressions,
)
from corehq.apps.hqwebapp import crispy as hqcrispy
from corehq.apps.hqwebapp.crispy import HQFormHelper
from corehq.apps.hqwebapp.crispy import DatetimeLocalWidget, HQFormHelper
from corehq.apps.hqwebapp.fields import MultiCharField
from corehq.apps.hqwebapp.tasks import send_html_email_async
from corehq.apps.hqwebapp.widgets import (
Expand Down Expand Up @@ -2669,9 +2670,26 @@ class DomainAlertForm(forms.Form):
widget=forms.Textarea,
required=True,
)
start_time = DateTimeField(
label="Start Time",
widget=DatetimeLocalWidget,
required=False
)
end_time = DateTimeField(
label="End Time",
widget=DatetimeLocalWidget,
required=False
)

def __init__(self, *args, **kwargs):
def __init__(self, request, *args, **kwargs):
super().__init__(*args, **kwargs)

datetime_local_widget_helptext = _("Using project's timezone: {}").format(
request.project.default_timezone
)
self.fields['start_time'].help_text = datetime_local_widget_helptext
self.fields['end_time'].help_text = datetime_local_widget_helptext

self.helper = hqcrispy.HQFormHelper(self)
self.helper.layout = Layout(
crispy.Fieldset(
Expand Down
10 changes: 9 additions & 1 deletion corehq/apps/domain/templates/domain/admin/manage_alerts.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
{% block page_content %}
{% initial_page_data 'alerts' alerts %}

<form method="post">
<form class="form form-horizontal" method="post">
{% crispy form %}
</form>
<div id="ko-alert-container">
Expand All @@ -21,6 +21,12 @@ <h3>
<table class="table">
<thead>
<tr>
<th>
{% trans "Start Time" %}
</th>
<th>
{% trans "End Time" %}
</th>
<th>
{% trans "Message" %}
</th>
Expand All @@ -38,6 +44,8 @@ <h3>
{% if alerts %}
<tbody data-bind="foreach: alerts">
<tr>
<td data-bind="text: start_time"></td>
<td data-bind="text: end_time"></td>
<td>
<div class="alert alert-warning"
data-bind="html: html"></div>
Expand Down
5 changes: 4 additions & 1 deletion corehq/apps/domain/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,10 @@ def test_only_domain_alerts_listed(self):
self.assertListEqual(
response.context['alerts'],
[
{'active': False, 'html': 'Test Alert 1!', 'id': alert.id, 'created_by_user': self.username}
{
'start_time': None, 'end_time': None,
'active': False, 'html': 'Test Alert 1!', 'id': alert.id, 'created_by_user': self.username
}
]
)
self.assertEqual(response.status_code, 200)
Expand Down
27 changes: 25 additions & 2 deletions corehq/apps/domain/views/settings.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pytz
import json
from collections import defaultdict
from functools import cached_property
Expand Down Expand Up @@ -59,6 +60,7 @@
from corehq.apps.users.models import CouchUser
from corehq.toggles import NAMESPACE_DOMAIN
from corehq.toggles.models import Toggle
from corehq.util.timezones.conversions import UserTime, ServerTime

MAX_ACTIVE_ALERTS = 3

Expand Down Expand Up @@ -538,6 +540,10 @@ def page_context(self):
'form': self.form,
'alerts': [
{
'start_time': ServerTime(alert.start_time).user_time(pytz.timezone(alert.timezone))
.ui_string() if alert.start_time else None,
'end_time': ServerTime(alert.end_time).user_time(pytz.timezone(alert.timezone))
.ui_string() if alert.end_time else None,
'active': alert.active,
'html': alert.html,
'id': alert.id,
Expand All @@ -550,8 +556,8 @@ def page_context(self):
@cached_property
def form(self):
if self.request.method == 'POST':
return DomainAlertForm(self.request.POST)
return DomainAlertForm()
return DomainAlertForm(self.request, self.request.POST)
return DomainAlertForm(self.request)

def post(self, request, *args, **kwargs):
if self.form.is_valid():
Expand All @@ -563,13 +569,30 @@ def post(self, request, *args, **kwargs):
return HttpResponseRedirect(self.page_url)

def _create_alert(self):
start_time = self.form.cleaned_data['start_time']
end_time = self.form.cleaned_data['end_time']
timezone = self.request.project.default_timezone

start_time = self._convert_timestamp_to_utc(start_time, timezone) if start_time else None
end_time = self._convert_timestamp_to_utc(end_time, timezone) if end_time else None

Alert.objects.create(
created_by_domain=self.domain,
domains=[self.domain],
text=self.form.cleaned_data['text'],
start_time=start_time,
end_time=end_time,
timezone=timezone,
created_by_user=self.request.couch_user.username,
)

@staticmethod
def _convert_timestamp_to_utc(timestamp, timezone):
return UserTime(
timestamp,
tzinfo=pytz.timezone(timezone)
).server_time().done()


@toggles.CUSTOM_DOMAIN_BANNER_ALERTS.required_decorator()
@domain_admin_required
Expand Down
5 changes: 5 additions & 0 deletions corehq/apps/hqwebapp/crispy.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import re
from contextlib import contextmanager
from django.forms.widgets import DateTimeInput
from django.template.loader import render_to_string
from django.utils.safestring import mark_safe
from django.utils.translation import gettext
Expand Down Expand Up @@ -352,6 +353,10 @@ class RadioSelect(Field):
template = "hqwebapp/crispy/radioselect.html"


class DatetimeLocalWidget(DateTimeInput):
input_type = "datetime-local"


def make_form_readonly(form):
if form is None:
return
Expand Down

0 comments on commit 1c666d8

Please sign in to comment.