Skip to content

Commit

Permalink
Merge branch 'dev/7.0' into release/7.12
Browse files Browse the repository at this point in the history
  • Loading branch information
teehamaral committed May 7, 2024
2 parents ebac7a4 + 7b1fc4f commit 020999c
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 10 deletions.
21 changes: 16 additions & 5 deletions temba/triggers/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ class BaseLargeSendForm(forms.ModelForm):
(45, _("45 minutes")),
(60, _("1 hour")),
)
CHUNK_MAX_LIMIT = 500

batch_interval = forms.ChoiceField(
choices=BATCH_INTERVAL,
Expand All @@ -366,12 +367,14 @@ class BaseLargeSendForm(forms.ModelForm):
required=True,
widget=SelectWidget(),
)

flow = TembaChoiceField(
Flow.objects.none(),
label=_("Flow"),
required=True,
widget=SelectWidget(attrs={"placeholder": _("Select a flow"), "searchable": True}),
)

groups = TembaMultipleChoiceField(
queryset=ContactGroup.user_groups.none(),
label=_("Groups"),
Expand All @@ -381,15 +384,18 @@ class BaseLargeSendForm(forms.ModelForm):
attrs={"icons": True, "placeholder": _("Select contact groups"), "searchable": True}
),
)

start_time = forms.DateTimeField(
required=True,
label=_("Start Time for Send"),
widget=InputWidget(attrs={"datetimepicker": True, "placeholder": _("Select a date and time")}),
)

chunk_size = forms.IntegerField(
label=_("Chunks"),
help_text=_("I want to split the message to this many pieces"),
widget=InputWidget(attrs={"type": "number", "placeholder": _("Enter chunk size")}),
max_value=CHUNK_MAX_LIMIT,
help_text=_("I want to split the message to this many pieces. Max: 500"),
widget=InputWidget(attrs={"type": "number", "max": CHUNK_MAX_LIMIT, "placeholder": _("Enter chunk size")}),
)

limit_time = forms.BooleanField(required=False, label=_("Limit to business hours"), help_text=_("9 AM to 5 PM"))
Expand All @@ -414,9 +420,14 @@ class LargeSendMixin:

@classmethod
def get_new_time(cls, input_time, hour, add_to_day=0):
input_time = datetime.datetime(
year=input_time.year, month=input_time.month, day=input_time.day + add_to_day, hour=hour
)
try:
input_time = datetime.datetime(
year=input_time.year, month=input_time.month, day=input_time.day + add_to_day, hour=hour
)
except ValueError:
input_time = datetime.datetime(
year=input_time.year, month=input_time.month, day=input_time.day, hour=hour
) + timedelta(days=add_to_day)
return timezone.make_aware(input_time)

def derive_start_time(self, start_time, limit_time):
Expand Down
49 changes: 44 additions & 5 deletions templates/triggers/trigger_create_large_send.haml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@
groupFields.forEach(elem => groupList.push(Number(elem.value)));

if (startTime && groupList.length > 0 && intervalValue && chunkSizeValue) {
if (chunkSizeValue > 500) {
showAlertMessage("Chunks should be max of 500", "red");
recreatePrimaryButton(true);
return
}

const formData = new FormData();
formData.append('groups', groupList);
formData.append('start_time', startTime);
Expand All @@ -100,24 +106,54 @@
const formOption = {
method: 'POST',
body: formData,
signal: AbortSignal.timeout(30000),
headers: {
'X-Requested-With': 'XMLHttpRequest',
'X-CSRFToken': getCookie('csrftoken')
}
}
showAlertMessage("Calculating groups...", "black");
recreatePrimaryButton(true);
fetch('{% url "triggers.trigger_large_send_schedule_summary" %}', formOption)
.then((response) => response.json())
.then((response) => {
toggleScheduleList(response);
recreatePrimaryButton(false);
}).catch((error) => {
console.log("[trigger_large_send_schedule_summary]", error)
showAlertMessage("Unable to process the summary, please try again.", "red")
});
}
}, 100);
}, 500);
}

function recreatePrimaryButton(disable) {
const { dialogFooter } = getFormElements();
const currentSubmit = dialogFooter.querySelector('temba-button');
let options = { name: 'Create', onclick: showConfirmation, primary: true };
if (disable) {
options['disabled'] = true;
}
const submitButton = createButton(options);
try {
if (currentSubmit) {
dialogFooter.removeChild(currentSubmit);
dialogFooter.prepend(submitButton);
}
} catch (e) {}
}

function chunkBy(number, n) {
return new Array(Math.floor(number / n)).fill(n).concat(number % n)
}

function showAlertMessage(msg, color) {
const { modaxBody } = getFormElements();
const scheduleListPreview = modaxBody.querySelector('.schedule-list-preview');
scheduleListPreview.innerHTML = `<p style="color: ${color}">${msg}</p>`;
scheduleListPreview.classList.remove('hidden');
}

function toggleScheduleList(response) {
const { modaxBody } = getFormElements();
const scheduleListPreview = modaxBody.querySelector('.schedule-list-preview');
Expand Down Expand Up @@ -152,6 +188,7 @@
event.preventDefault();
const scheduleList = modaxBody.querySelector('#schedule-list');
const confirmationContent = confirmationDialog.querySelector('.p-6');
confirmationContent.innerHTML = "Please confirm you want to send on the following schedule:"
if (scheduleList) confirmationContent.appendChild(scheduleList);
confirmationDialog.open = true;
}
Expand Down Expand Up @@ -186,9 +223,11 @@
hasError = true;
}
});
event.target.disabled = false;
if (hasError) confirmationDialog.open = false;
else window.location.href = '{% url "triggers.trigger_list" %}';
if (hasError) {
confirmationDialog.open = false;
} else {
window.location.href = '{% url "triggers.trigger_list" %}';
}
});
}

Expand Down Expand Up @@ -224,7 +263,7 @@
initCalFields(form);
onValueChange();
const currentSubmit = dialogFooter.querySelector('temba-button[primary]');
const submitButton = createButton({ name: 'Create', onclick: showConfirmation, primary: true });
const submitButton = createButton({ name: 'Create', onclick: showConfirmation, primary: true, disabled: true });
try {
if (currentSubmit) {
dialogFooter.removeChild(currentSubmit);
Expand Down

0 comments on commit 020999c

Please sign in to comment.