Commit 1144ef6a by Asad Azam Committed by AsadAzam

Validation checks on calendar start and end dates

parent 41ec68db
......@@ -6,6 +6,7 @@ from django import forms
from django.core.exceptions import ValidationError
from django.db.models.functions import Lower
from django.template.loader import render_to_string
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
from opaque_keys import InvalidKeyError
from opaque_keys.edx.keys import CourseKey
......@@ -321,8 +322,14 @@ class CustomCourseRunForm(CourseRunForm):
cleaned_data = self.cleaned_data
min_effort = cleaned_data.get("min_effort")
max_effort = cleaned_data.get("max_effort")
start = cleaned_data.get("start")
end = cleaned_data.get("end")
if start and start < timezone.now():
raise ValidationError({'start': _('Start date cannot be in the Past')})
if start and end and start > end:
raise ValidationError({'start': _('Start date cannot be after the End date')})
if min_effort and max_effort and min_effort > max_effort:
raise ValidationError({'min_effort': "Minimum effort cannot be greater than Maximum effort"})
raise ValidationError({'min_effort': _('Minimum effort cannot be greater than Maximum effort')})
return cleaned_data
......
from datetime import datetime, timedelta
from django.core.exceptions import ValidationError
from django.test import TestCase
from pytz import timezone
from course_discovery.apps.core.models import User
from course_discovery.apps.core.tests.factories import UserFactory
......@@ -96,3 +98,24 @@ class PublisherCourseRunEditFormTests(TestCase):
run_form.cleaned_data['min_effort'] = 1
self.assertEqual(run_form.clean(), run_form.cleaned_data)
def test_course_run_dates(self):
"""
Verify that 'clean' raises 'ValidationError' if the Start date is in the past
Or if the Start date is after the End date
"""
run_form = CustomCourseRunForm()
current_datetime = datetime.now(timezone('US/Central'))
run_form.cleaned_data = {'start': current_datetime + timedelta(days=3),
'end': current_datetime + timedelta(days=1)}
with self.assertRaises(ValidationError):
run_form.clean()
run_form.cleaned_data = {'start': current_datetime - timedelta(days=3),
'end': current_datetime + timedelta(days=3)}
with self.assertRaises(ValidationError):
run_form.clean()
run_form.cleaned_data['start'] = current_datetime + timedelta(days=1)
run_form.cleaned_data['end'] = current_datetime + timedelta(days=3)
self.assertEqual(run_form.clean(), run_form.cleaned_data)
......@@ -664,6 +664,18 @@ msgid "Video Language"
msgstr ""
#: apps/publisher/forms.py
msgid "Start date cannot be in the Past"
msgstr ""
#: apps/publisher/forms.py
msgid "Start date cannot be after the End date"
msgstr ""
#: apps/publisher/forms.py
msgid "Minimum effort cannot be greater than Maximum effort"
msgstr ""
#: apps/publisher/forms.py
msgid "Only audit seat can be without price."
msgstr ""
......
......@@ -790,6 +790,23 @@ msgid "Video Language"
msgstr "Vïdéö Längüägé Ⱡ'σяєм ιρѕυм ∂σłσя ѕιт#"
#: apps/publisher/forms.py
msgid "Start date cannot be in the Past"
msgstr ""
"Stärt däté çännöt ßé ïn thé Päst Ⱡ'σяєм ιρѕυм ∂σłσя ѕιт αмєт, ¢σηѕє¢тє#"
#: apps/publisher/forms.py
msgid "Start date cannot be after the End date"
msgstr ""
"Stärt däté çännöt ßé äftér thé Énd däté Ⱡ'σяєм ιρѕυм ∂σłσя ѕιт αмєт, "
"¢σηѕє¢тєтυя#"
#: apps/publisher/forms.py
msgid "Minimum effort cannot be greater than Maximum effort"
msgstr ""
"Mïnïmüm éffört çännöt ßé gréätér thän Mäxïmüm éffört Ⱡ'σяєм ιρѕυм ∂σłσя ѕιт "
"αмєт, ¢σηѕє¢тєтυя α#"
#: apps/publisher/forms.py
msgid "Only audit seat can be without price."
msgstr ""
"Önlý äüdït séät çän ßé wïthöüt prïçé. Ⱡ'σяєм ιρѕυм ∂σłσя ѕιт αмєт, "
......
......@@ -54,6 +54,9 @@
<div class="col col-6">
<label class="field-label ">{{ run_form.start.label_tag }} <span class="required">*</span></label>
{{ run_form.start }}
{% if run_form.start.errors %}
{{ run_form.start.errors|escape }}
{% endif %}
</div>
</div>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment