fix: don't validate to & from dates if any one is missing in validate_from_to_dates (#19079)

This commit is contained in:
Raffael Meyer 2022-12-03 14:02:59 +01:00 committed by GitHub
parent 9c104a8e5e
commit c7ef28fa0c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 8 deletions

View file

@ -1506,16 +1506,18 @@ class Document(BaseDocument):
if self in frappe.local.locked_documents:
frappe.local.locked_documents.remove(self)
# validation helpers
def validate_from_to_dates(self, from_date_field, to_date_field):
"""
Generic validation to verify date sequence
"""
if date_diff(self.get(to_date_field), self.get(from_date_field)) < 0:
def validate_from_to_dates(self, from_date_field: str, to_date_field: str) -> None:
"""Validate that the value of `from_date_field` is not later than the value of `to_date_field`."""
from_date = self.get(from_date_field)
to_date = self.get(to_date_field)
if not (from_date and to_date):
return
if date_diff(to_date, from_date) < 0:
frappe.throw(
_("{0} must be after {1}").format(
frappe.bold(self.meta.get_label(to_date_field)),
frappe.bold(self.meta.get_label(from_date_field)),
frappe.bold(_(self.meta.get_label(to_date_field))),
frappe.bold(_(self.meta.get_label(from_date_field))),
),
frappe.exceptions.InvalidDates,
)

View file

@ -424,6 +424,30 @@ class TestDocument(FrappeTestCase):
self.assertRaises(frappe.DoesNotExistError, doc.save)
def test_validate_from_to_dates(self):
doc = frappe.new_doc("Web Page")
doc.start_date = None
doc.end_date = None
doc.validate_from_to_dates("start_date", "end_date")
doc.start_date = "2020-01-01"
doc.end_date = None
doc.validate_from_to_dates("start_date", "end_date")
doc.start_date = None
doc.end_date = "2020-12-31"
doc.validate_from_to_dates("start_date", "end_date")
doc.start_date = "2020-01-01"
doc.end_date = "2020-12-31"
doc.validate_from_to_dates("start_date", "end_date")
doc.end_date = "2020-01-01"
doc.start_date = "2020-12-31"
self.assertRaises(
frappe.exceptions.InvalidDates, doc.validate_from_to_dates, "start_date", "end_date"
)
class TestDocumentWebView(FrappeTestCase):
def get(self, path, user="Guest"):