fix: change request modifications

This commit is contained in:
pateljannat 2020-12-21 20:52:20 +05:30
parent b8e656512e
commit 9466e42e70
6 changed files with 82 additions and 75 deletions

View File

@ -6,7 +6,13 @@ import frappe
def execute(): def execute():
frappe.reload_doc("projects", "doctype", "project_template") frappe.reload_doc("projects", "doctype", "project_template")
for template_name in frappe.db.sql(""" select name from `tabProject Template` """, as_dict=1): for template_name in frappe.db.sql("""
select
name
from
`tabProject Template` """,
as_dict=1):
template = frappe.get_doc("Project Template", template_name.name) template = frappe.get_doc("Project Template", template_name.name)
replace_tasks = False replace_tasks = False
new_tasks = [] new_tasks = []

View File

@ -59,8 +59,8 @@ class Project(Document):
for task in template.tasks: for task in template.tasks:
template_task_details = frappe.get_doc("Task", task.task) template_task_details = frappe.get_doc("Task", task.task)
tmp_task_details.append(template_task_details) tmp_task_details.append(template_task_details)
project_tasks.append(self.create_task_from_template(template_task_details)) task = self.create_task_from_template(template_task_details)
project_tasks.append(task)
self.dependency_mapping(tmp_task_details, project_tasks) self.dependency_mapping(tmp_task_details, project_tasks)
def create_task_from_template(self, task_details): def create_task_from_template(self, task_details):
@ -75,36 +75,33 @@ class Project(Document):
task_weight = task_details.task_weight, task_weight = task_details.task_weight,
type = task_details.type, type = task_details.type,
issue = task_details.issue, issue = task_details.issue,
is_group = task_details.is_group, is_group = task_details.is_group
start = task_details.start,
duration = task_details.duration
)).insert() )).insert()
def dependency_mapping(self, template_tasks, project_tasks): def dependency_mapping(self, template_tasks, project_tasks):
for tmp_task in template_tasks: for template_task in template_tasks:
prj_task = list(filter(lambda x: x.subject == tmp_task.subject, project_tasks))[0] project_task = list(filter(lambda x: x.subject == template_task.subject, project_tasks))[0]
prj_task = frappe.get_doc("Task", prj_task.name) if template_task.get("depends_on") and not project_task.get("depends_on"):
self.check_depends_on_value(tmp_task, prj_task, project_tasks) self.check_depends_on_value(template_task, project_task, project_tasks)
self.check_for_parent_tasks(tmp_task, prj_task, project_tasks) if template_task.get("parent_task") and not project_task.get("parent_task"):
self.check_for_parent_tasks(template_task, project_task, project_tasks)
def check_depends_on_value(self, tmp_task, prj_task, project_tasks): def check_depends_on_value(self, template_task, project_task, project_tasks):
if tmp_task.get("depends_on") and not prj_task.get("depends_on"): for child_task in template_task.get("depends_on"):
for child_task in tmp_task.get("depends_on"): child_task_subject = frappe.db.get_value("Task", child_task.task, "subject")
child_task_subject = frappe.db.get_value("Task", child_task.task, "subject") corresponding_project_task = list(filter(lambda x: x.subject == child_task_subject, project_tasks))
corresponding_prj_task = list(filter(lambda x: x.subject == child_task_subject, project_tasks)) if len(corresponding_project_task):
if len(corresponding_prj_task): project_task.append("depends_on",{
prj_task.append("depends_on",{ "task": corresponding_project_task[0].name
"task": corresponding_prj_task[0].name })
}) project_task.save()
prj_task.save()
def check_for_parent_tasks(self, tmp_task, prj_task, project_tasks): def check_for_parent_tasks(self, template_task, project_task, project_tasks):
if tmp_task.get("parent_task") and not prj_task.get("parent_task"): parent_task_subject = frappe.db.get_value("Task", template_task.get("parent_task"), "subject")
parent_task_subject = frappe.db.get_value("Task", tmp_task.get("parent_task"), "subject") corresponding_project_task = list(filter(lambda x: x.subject == parent_task_subject, project_tasks))
corresponding_prj_task = list(filter(lambda x: x.subject == parent_task_subject, project_tasks)) if len(corresponding_project_task):
if len(corresponding_prj_task): project_task.parent_task = corresponding_project_task[0].name
prj_task.parent_task = corresponding_prj_task[0].name project_task.save()
prj_task.save()
def is_row_updated(self, row, existing_task_data, fields): def is_row_updated(self, row, existing_task_data, fields):
if self.get("__islocal") or not existing_task_data: return True if self.get("__islocal") or not existing_task_data: return True

View File

@ -17,78 +17,79 @@ class TestProject(unittest.TestCase):
""" """
Test Action: Basic Test of a Project created from template. The template has a single task. Test Action: Basic Test of a Project created from template. The template has a single task.
""" """
frappe.db.sql('delete from tabTask where project = "Test Project with Templ - no parent and dependend tasks"') project_name = "Test Project with Template - No Parent and Dependend Tasks"
frappe.delete_doc('Project', 'Test Project with Templ - no parent and dependend tasks') frappe.db.sql(""" delete from tabTask where project = %s """, project_name)
frappe.delete_doc('Project', project_name)
task1 = task_exists("Test Temp Task with no parent and dependency") task1 = task_exists("Test Template Task with No Parent and Dependency")
if not task1: if not task1:
task1 = create_task(subject="Test Temp Task with no parent and dependency", is_template=1, begin=5, duration=3) task1 = create_task(subject="Test Template Task with No Parent and Dependency", is_template=1, begin=5, duration=3)
template = make_project_template("Test Project Template - no parent and dependend tasks", [task1]) template = make_project_template("Test Project Template - No Parent and Dependend Tasks", [task1])
project = get_project("Test Project with Templ - no parent and dependend tasks", template) project = get_project(project_name, template)
tasks = frappe.get_all('Task', '*', dict(project=project.name), order_by='creation asc') tasks = frappe.get_all('Task', ['subject','exp_end_date','depends_on_tasks'], dict(project=project.name), order_by='creation asc')
self.assertEqual(tasks[0].subject, 'Test Temp Task with no parent and dependency') self.assertEqual(tasks[0].subject, 'Test Template Task with No Parent and Dependency')
self.assertEqual(getdate(tasks[0].exp_end_date), calculate_end_date(project, tasks[0])) self.assertEqual(getdate(tasks[0].exp_end_date), calculate_end_date(project, 5, 3))
self.assertEqual(len(tasks), 1) self.assertEqual(len(tasks), 1)
def test_project_template_having_parent_child_tasks(self): def test_project_template_having_parent_child_tasks(self):
project_name = "Test Project with Template - Tasks with Parent-Child Relation"
frappe.db.sql(""" delete from tabTask where project = %s """, project_name)
frappe.delete_doc('Project', project_name)
frappe.db.sql('delete from tabTask where project = "Test Project with Templ - tasks with parent-child"') task1 = task_exists("Test Template Task Parent")
frappe.delete_doc('Project', 'Test Project with Templ - tasks with parent-child')
task1 = task_exists("Test Temp Task parent")
if not task1: if not task1:
task1 = create_task(subject="Test Temp Task parent", is_group=1, is_template=1, begin=1, duration=1) task1 = create_task(subject="Test Template Task Parent", is_group=1, is_template=1, begin=1, duration=1)
task2 = task_exists("Test Temp Task child 1") task2 = task_exists("Test Template Task Child 1")
if not task2: if not task2:
task2 = create_task(subject="Test Temp Task child 1", parent_task=task1.name, is_template=1, begin=1, duration=3) task2 = create_task(subject="Test Template Task Child 1", parent_task=task1.name, is_template=1, begin=1, duration=3)
task3 = task_exists("Test Temp Task child 2") task3 = task_exists("Test Template Task Child 2")
if not task3: if not task3:
task3 = create_task(subject="Test Temp Task child 2", parent_task=task1.name, is_template=1, begin=2, duration=3) task3 = create_task(subject="Test Template Task Child 2", parent_task=task1.name, is_template=1, begin=2, duration=3)
template = make_project_template("Test Project Template - tasks with parent-child", [task1, task2, task3]) template = make_project_template("Test Project Template - Tasks with Parent-Child Relation", [task1, task2, task3])
project = get_project("Test Project with Templ - tasks with parent-child", template) project = get_project(project_name, template)
tasks = frappe.get_all('Task', '*', dict(project=project.name), order_by='creation asc') tasks = frappe.get_all('Task', ['subject','exp_end_date','depends_on_tasks', 'name'], dict(project=project.name), order_by='creation asc')
self.assertEqual(tasks[0].subject, 'Test Temp Task parent') self.assertEqual(tasks[0].subject, 'Test Template Task Parent')
self.assertEqual(getdate(tasks[0].exp_end_date), calculate_end_date(project, tasks[0])) self.assertEqual(getdate(tasks[0].exp_end_date), calculate_end_date(project, 1, 1))
self.assertEqual(tasks[1].subject, 'Test Temp Task child 1') self.assertEqual(tasks[1].subject, 'Test Template Task Child 1')
self.assertEqual(getdate(tasks[1].exp_end_date), calculate_end_date(project, tasks[1])) self.assertEqual(getdate(tasks[1].exp_end_date), calculate_end_date(project, 1, 3))
self.assertEqual(tasks[1].parent_task, tasks[0].name) self.assertEqual(tasks[1].parent_task, tasks[0].name)
self.assertEqual(tasks[2].subject, 'Test Temp Task child 2') self.assertEqual(tasks[2].subject, 'Test Template Task Child 2')
self.assertEqual(getdate(tasks[2].exp_end_date), calculate_end_date(project, tasks[2])) self.assertEqual(getdate(tasks[2].exp_end_date), calculate_end_date(project, 2, 3))
self.assertEqual(tasks[2].parent_task, tasks[0].name) self.assertEqual(tasks[2].parent_task, tasks[0].name)
self.assertEqual(len(tasks), 3) self.assertEqual(len(tasks), 3)
def test_project_template_having_dependent_tasks(self): def test_project_template_having_dependent_tasks(self):
project_name = "Test Project with Template - Dependent Tasks"
frappe.db.sql(""" delete from tabTask where project = %s """, project_name)
frappe.delete_doc('Project', project_name)
frappe.db.sql('delete from tabTask where project = "Test Project with Templ - dependent tasks"') task1 = task_exists("Test Template Task for Dependency")
frappe.delete_doc('Project', 'Test Project with Templ - dependent tasks')
task1 = task_exists("Test Temp Task for dependency")
if not task1: if not task1:
task1 = create_task(subject="Test Temp Task for dependency", is_template=1, begin=3, duration=1) task1 = create_task(subject="Test Template Task for Dependency", is_template=1, begin=3, duration=1)
task2 = task_exists("Test Temp Task with dependency") task2 = task_exists("Test Template Task with Dependency")
if not task2: if not task2:
task2 = create_task(subject="Test Temp Task with dependency", depends_on=task1.name, is_template=1, begin=2, duration=2) task2 = create_task(subject="Test Template Task with Dependency", depends_on=task1.name, is_template=1, begin=2, duration=2)
template = make_project_template("Test Project with Templ - dependent tasks", [task1, task2]) template = make_project_template("Test Project with Template - Dependent Tasks", [task1, task2])
project = get_project("Test Project with Templ - dependent tasks", template) project = get_project(project_name, template)
tasks = frappe.get_all('Task', '*', dict(project=project.name), order_by='creation asc') tasks = frappe.get_all('Task', ['subject','exp_end_date','depends_on_tasks', 'name'], dict(project=project.name), order_by='creation asc')
self.assertEqual(tasks[1].subject, 'Test Temp Task with dependency') self.assertEqual(tasks[1].subject, 'Test Template Task with Dependency')
self.assertEqual(getdate(tasks[1].exp_end_date), calculate_end_date(project, tasks[1])) self.assertEqual(getdate(tasks[1].exp_end_date), calculate_end_date(project, 2, 2))
self.assertTrue(tasks[1].depends_on_tasks.find(tasks[0].name) >= 0 ) self.assertTrue(tasks[1].depends_on_tasks.find(tasks[0].name) >= 0 )
self.assertEqual(tasks[0].subject, 'Test Temp Task for dependency') self.assertEqual(tasks[0].subject, 'Test Template Task for Dependency')
self.assertEqual(getdate(tasks[0].exp_end_date), calculate_end_date(project, tasks[0]) ) self.assertEqual(getdate(tasks[0].exp_end_date), calculate_end_date(project, 3, 1) )
self.assertEqual(len(tasks), 2) self.assertEqual(len(tasks), 2)
@ -129,5 +130,5 @@ def task_exists(subject):
return False return False
return frappe.get_doc("Task", result[0].name) return frappe.get_doc("Task", result[0].name)
def calculate_end_date(project, task): def calculate_end_date(project, start, duration):
return getdate(add_days(project.expected_start_date, task.start + task.duration)) return getdate(add_days(project.expected_start_date, start + duration))

View File

@ -6,6 +6,7 @@ from __future__ import unicode_literals
import frappe import frappe
from frappe.model.document import Document from frappe.model.document import Document
from frappe import _ from frappe import _
from frappe.utils import get_link_to_form
class ProjectTemplate(Document): class ProjectTemplate(Document):
@ -18,8 +19,8 @@ class ProjectTemplate(Document):
if task_details.depends_on: if task_details.depends_on:
for dependency_task in task_details.depends_on: for dependency_task in task_details.depends_on:
if not self.check_dependent_task_presence(dependency_task.task): if not self.check_dependent_task_presence(dependency_task.task):
task_details_format = """<a href="#Form/Task/{0}">{0}</a>""".format(task_details.name) task_details_format = get_link_to_form("Task",task_details.name)
dependency_task_format = """<a href="#Form/Task/{0}">{0}</a>""".format(dependency_task.task) dependency_task_format = get_link_to_form("Task", dependency_task.task)
frappe.throw(_("Task {0} depends on Task {1}. Please add Task {1} to the Tasks list.").format(frappe.bold(task_details_format), frappe.bold(dependency_task_format))) frappe.throw(_("Task {0} depends on Task {1}. Please add Task {1} to the Tasks list.").format(frappe.bold(task_details_format), frappe.bold(dependency_task_format)))
def check_dependent_task_presence(self, task): def check_dependent_task_presence(self, task):

View File

@ -371,11 +371,13 @@
"label": "Is Template" "label": "Is Template"
}, },
{ {
"depends_on": "is_template",
"fieldname": "start", "fieldname": "start",
"fieldtype": "Int", "fieldtype": "Int",
"label": "Begin On (Days)" "label": "Begin On (Days)"
}, },
{ {
"depends_on": "is_template",
"fieldname": "duration", "fieldname": "duration",
"fieldtype": "Int", "fieldtype": "Int",
"label": "Duration (Days)" "label": "Duration (Days)"
@ -386,7 +388,7 @@
"is_tree": 1, "is_tree": 1,
"links": [], "links": [],
"max_attachments": 5, "max_attachments": 5,
"modified": "2020-12-07 13:26:53.614689", "modified": "2020-12-21 11:59:24.196834",
"modified_by": "Administrator", "modified_by": "Administrator",
"module": "Projects", "module": "Projects",
"name": "Task", "name": "Task",

View File

@ -104,7 +104,7 @@ def create_task(subject, start=None, end=None, depends_on=None, project=None, pa
task.subject = subject task.subject = subject
task.exp_start_date = start or nowdate() task.exp_start_date = start or nowdate()
task.exp_end_date = end or nowdate() task.exp_end_date = end or nowdate()
task.project = project or "_Test Project" task.project = project or None if is_template else "_Test Project"
task.is_template = is_template task.is_template = is_template
task.start = begin task.start = begin
task.duration = duration task.duration = duration