fix: Validate task dates in project (#16586)

* fix: Validate task dates in project

* fix: Test case fix and validation only if date exists

* fix: Added dates in error messages
This commit is contained in:
Deepesh Garg 2019-02-12 12:55:50 +05:30 committed by Faris Ansari
parent 1a7e438fb2
commit da7c20e474
3 changed files with 20 additions and 17 deletions

View File

@ -66,11 +66,11 @@ class Project(Document):
def validate(self):
self.validate_project_name()
self.validate_dates()
self.validate_weights()
self.sync_tasks()
self.tasks = []
self.load_tasks()
self.validate_dates()
self.send_welcome_email()
self.update_percent_complete()
@ -79,6 +79,24 @@ class Project(Document):
frappe.throw(_("Project {0} already exists").format(frappe.safe_decode(self.project_name)))
def validate_dates(self):
if self.tasks:
for d in self.tasks:
if self.expected_start_date:
if d.start_date and getdate(d.start_date) < getdate(self.expected_start_date):
frappe.throw(_("Start date of task <b>{0}</b> cannot be less than <b>{1}</b> expected start date <b>{2}</b>")
.format(d.title, self.name, self.expected_start_date))
if d.end_date and getdate(d.end_date) < getdate(self.expected_start_date):
frappe.throw(_("End date of task <b>{0}</b> cannot be less than <b>{1}</b> expected start date <b>{2}</b>")
.format(d.title, self.name, self.expected_start_date))
if self.expected_end_date:
if d.start_date and getdate(d.start_date) > getdate(self.expected_end_date):
frappe.throw(_("Start date of task <b>{0}</b> cannot be greater than <b>{1}</b> expected end date <b>{2}</b>")
.format(d.title, self.name, self.expected_end_date))
if d.end_date and getdate(d.end_date) > getdate(self.expected_end_date):
frappe.throw(_("End date of task <b>{0}</b> cannot be greater than <b>{1}</b> expected end date <b>{2}</b>")
.format(d.title, self.name, self.expected_end_date))
if self.expected_start_date and self.expected_end_date:
if getdate(self.expected_end_date) < getdate(self.expected_start_date):
frappe.throw(_("Expected End Date can not be less than Expected Start Date"))

View File

@ -44,12 +44,6 @@ class Task(NestedSet):
if self.act_start_date and self.act_end_date and getdate(self.act_start_date) > getdate(self.act_end_date):
frappe.throw(_("'Actual Start Date' can not be greater than 'Actual End Date'"))
if(self.project):
if frappe.db.exists("Project", self.project):
expected_end_date = frappe.db.get_value("Project", self.project, "expected_end_date")
if self.exp_end_date and expected_end_date and getdate(self.exp_end_date) > getdate(expected_end_date) :
frappe.throw(_("Expected end date cannot be after Project: <b>'{0}'</b> Expected end date").format(self.project), EndDateCannotBeGreaterThanProjectEndDateError)
def validate_status(self):
if self.status!=self.get_db_value("status") and self.status == "Closed":
for d in self.depends_on:

View File

@ -5,7 +5,7 @@ import frappe
import unittest
from frappe.utils import getdate, nowdate, add_days
from erpnext.projects.doctype.task.task import CircularReferenceError, EndDateCannotBeGreaterThanProjectEndDateError
from erpnext.projects.doctype.task.task import CircularReferenceError
class TestTask(unittest.TestCase):
def test_circular_reference(self):
@ -97,15 +97,6 @@ class TestTask(unittest.TestCase):
self.assertEqual(frappe.db.get_value("Task", task.name, "status"), "Overdue")
def test_end_date_validation(self):
task_end = create_task("Testing_Enddate_validation", add_days(nowdate(), 35), add_days(nowdate(), 45), save=False)
pro = frappe.get_doc("Project", task_end.project)
pro.expected_end_date = add_days(nowdate(), 40)
pro.save()
self.assertRaises(EndDateCannotBeGreaterThanProjectEndDateError, task_end.save)
def create_task(subject, start=None, end=None, depends_on=None, project=None, save=True):
if not frappe.db.exists("Task", subject):
task = frappe.new_doc('Task')