blob: 39648e390b4448c2a7e464368bdb296eaccefc19 [file] [log] [blame]
Kanchan Chauhanb3fe6a42016-03-16 18:01:22 +05301# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
2# License: GNU General Public License v3. See license.txt
3
4from __future__ import unicode_literals
5import frappe
Rushabh Mehtab2269dd2016-03-23 18:28:50 +05306import json
Kanchan Chauhanb3fe6a42016-03-16 18:01:22 +05307
8def get_context(context):
9 context.no_cache = 1
10
11 project = frappe.get_doc('Project', frappe.form_dict.project)
12
13 project.has_permission('read')
14
15 context.issues = frappe.get_all('Issue', filters={'project': project.project_name},
16 fields=['subject', 'opening_date', 'resolution_date', 'status', 'name', 'resolution_details','modified','modified_by'])
17
18 project.tasks = get_tasks(project.name, start=0, search=frappe.form_dict.get("q"))
Rushabh Mehtab2269dd2016-03-23 18:28:50 +053019
Kanchan Chauhanb3fe6a42016-03-16 18:01:22 +053020 project.timelogs = get_timelogs(project.name, start=0, search=frappe.form_dict.get("q"))
Rushabh Mehtab2269dd2016-03-23 18:28:50 +053021
Kanchan Chauhanb3fe6a42016-03-16 18:01:22 +053022 project.issues = get_issues(project.name, start=0, search=frappe.form_dict.get("q"))
Rushabh Mehtab2269dd2016-03-23 18:28:50 +053023
Kanchan Chauhanb3fe6a42016-03-16 18:01:22 +053024 project.timelines = get_timeline(project.project_name, start=0)
Rushabh Mehtab2269dd2016-03-23 18:28:50 +053025
26
Kanchan Chauhanb3fe6a42016-03-16 18:01:22 +053027
28 context.doc = project
Rushabh Mehtab2269dd2016-03-23 18:28:50 +053029
30
Kanchan Chauhanb3fe6a42016-03-16 18:01:22 +053031def get_timeline(project, start=10):
Rushabh Mehtab2269dd2016-03-23 18:28:50 +053032 '''Get timeline from project, tasks, issues'''
33 issues_condition = ''
34 project_issues = get_issues(project)
35
36 if project_issues:
37 issue_names = '({0})'.format(", ".join(["'{0}'".format(i.name) for i in project_issues]))
38 issues_condition = """or (reference_doctype='Issue' and reference_name IN {issue_names})""".format(issue_names=issue_names)
39
40
Kanchan Chauhan2ad801c2016-03-22 16:00:41 +053041 timelines = frappe.db.sql("""
Rushabh Mehtab2269dd2016-03-23 18:28:50 +053042 select
43 sender_full_name,
44 subject, communication_date, comment_type, name, creation, modified_by, reference_doctype, reference_name,
45 _liked_by, comment_type, _comments
46 from
47 tabCommunication
48 where
Kanchan Chauhan2ad801c2016-03-22 16:00:41 +053049 (reference_doctype='Project' and reference_name=%s)
Kanchan Chauhanb3fe6a42016-03-16 18:01:22 +053050 or (timeline_doctype='Project' and timeline_name=%s)
Rushabh Mehtab2269dd2016-03-23 18:28:50 +053051 {issues_condition}
52 order by
Kanchan Chauhan2ad801c2016-03-22 16:00:41 +053053 modified DESC limit {start}, {limit}""".format(
Rushabh Mehtab2269dd2016-03-23 18:28:50 +053054 issues_condition=issues_condition, start=start, limit=10),
Kanchan Chauhan2ad801c2016-03-22 16:00:41 +053055 (project, project), as_dict=True);
Kanchan Chauhanb3fe6a42016-03-16 18:01:22 +053056 for timeline in timelines:
57 timeline.user_image = frappe.db.get_value('User', timeline.modified_by, 'user_image')
Rushabh Mehtab2269dd2016-03-23 18:28:50 +053058 return timelines
59
Kanchan Chauhanb3fe6a42016-03-16 18:01:22 +053060@frappe.whitelist()
Kanchan Chauhan2ad801c2016-03-22 16:00:41 +053061def get_timelines_html(project, start=0):
Rushabh Mehtab2269dd2016-03-23 18:28:50 +053062 return frappe.render_template("erpnext/templates/includes/projects/timeline.html",
63 {"doc": {"timelines": get_timeline(project, start)}}, is_path=True)
64
Kanchan Chauhanb3fe6a42016-03-16 18:01:22 +053065def get_issue_list(project):
66 return [issue.name for issue in get_issues(project)]
67
Kanchan Chauhan2ad801c2016-03-22 16:00:41 +053068def get_tasks(project, start=0, search=None, item_status=None):
Kanchan Chauhanb3fe6a42016-03-16 18:01:22 +053069 filters = {"project": project}
70 if search:
71 filters["subject"] = ("like", "%{0}%".format(search))
Rushabh Mehtab2269dd2016-03-23 18:28:50 +053072 if item_status:
Kanchan Chauhan53ce94f2016-03-25 12:04:54 +053073 filters["status"] = item_status
Rushabh Mehtab2269dd2016-03-23 18:28:50 +053074 tasks = frappe.get_all("Task", filters=filters,
75 fields=["name", "subject", "status", "exp_start_date", "exp_end_date", "priority"],
76 limit_start=start, limit_page_length=10)
77
Kanchan Chauhanb3fe6a42016-03-16 18:01:22 +053078 for task in tasks:
Kanchan Chauhane14389e2016-03-23 14:14:38 +053079 print task._comments
Kanchan Chauhanb3fe6a42016-03-16 18:01:22 +053080 task.todo = frappe.get_all('ToDo',filters={'reference_name':task.name, 'reference_type':'Task'},
81 fields=["assigned_by", "owner", "modified", "modified_by"])
82 if task.todo:
83 task.todo=task.todo[0]
84 task.todo.user_image = frappe.db.get_value('User', task.todo.owner, 'user_image')
Kanchan Chauhane14389e2016-03-23 14:14:38 +053085 if task._comments:
Rushabh Mehtab2269dd2016-03-23 18:28:50 +053086 task.comment_count = len(json.loads(task._comments or "[]"))
Kanchan Chauhanb3fe6a42016-03-16 18:01:22 +053087 return tasks
88
89@frappe.whitelist()
Kanchan Chauhan2ad801c2016-03-22 16:00:41 +053090def get_tasks_html(project, start=0, item_status=None):
Rushabh Mehtab2269dd2016-03-23 18:28:50 +053091 return frappe.render_template("erpnext/templates/includes/projects/project_tasks.html",
Kanchan Chauhan2ad801c2016-03-22 16:00:41 +053092 {"doc": {"tasks": get_tasks(project, start, item_status=item_status)}}, is_path=True)
Rushabh Mehtab2269dd2016-03-23 18:28:50 +053093
94
Kanchan Chauhan2ad801c2016-03-22 16:00:41 +053095def get_issues(project, start=0, search=None, item_status=None):
Kanchan Chauhanb3fe6a42016-03-16 18:01:22 +053096 filters = {"project": project}
97 if search:
98 filters["subject"] = ("like", "%{0}%".format(search))
Rushabh Mehtab2269dd2016-03-23 18:28:50 +053099 if item_status:
Kanchan Chauhan53ce94f2016-03-25 12:04:54 +0530100 filters["status"] = item_status
Rushabh Mehtab2269dd2016-03-23 18:28:50 +0530101 issues = frappe.get_all("Issue", filters=filters,
102 fields=["name", "subject", "status", "opening_date", "resolution_date", "resolution_details"],
Kanchan Chauhanb3fe6a42016-03-16 18:01:22 +0530103 order_by='modified desc',
Rushabh Mehtab2269dd2016-03-23 18:28:50 +0530104 limit_start=start, limit_page_length=10)
105
Kanchan Chauhanb3fe6a42016-03-16 18:01:22 +0530106 for issue in issues:
107 issue.todo = frappe.get_all('ToDo',filters={'reference_name':issue.name, 'reference_type':'Issue'},
108 fields=["assigned_by", "owner", "modified", "modified_by"])
109 if issue.todo:
110 issue.todo=issue.todo[0]
111 issue.todo.user_image = frappe.db.get_value('User', issue.todo.owner, 'user_image')
Rushabh Mehtab2269dd2016-03-23 18:28:50 +0530112
Kanchan Chauhanb3fe6a42016-03-16 18:01:22 +0530113 return issues
114
115@frappe.whitelist()
Kanchan Chauhan2ad801c2016-03-22 16:00:41 +0530116def get_issues_html(project, start=0, item_status=None):
Rushabh Mehtab2269dd2016-03-23 18:28:50 +0530117 return frappe.render_template("erpnext/templates/includes/projects/project_issues.html",
118 {"doc": {"issues": get_issues(project, start, item_status=item_status)}}, is_path=True)
119
Kanchan Chauhanb3fe6a42016-03-16 18:01:22 +0530120def get_timelogs(project, start=0, search=None):
121 filters = {"project": project}
122 if search:
123 filters["title"] = ("like", "%{0}%".format(search))
Rushabh Mehtab2269dd2016-03-23 18:28:50 +0530124
125 timelogs = frappe.get_all('Time Log', filters=filters,
Kanchan Chauhanb3fe6a42016-03-16 18:01:22 +0530126 fields=['name','title','task','activity_type','from_time','to_time','hours','status','modified','modified_by'],
Rushabh Mehtab2269dd2016-03-23 18:28:50 +0530127 limit_start=start, limit_page_length=10)
Kanchan Chauhanb3fe6a42016-03-16 18:01:22 +0530128 for timelog in timelogs:
Rushabh Mehtab2269dd2016-03-23 18:28:50 +0530129 timelog.user_image = frappe.db.get_value('User', timelog.modified_by, 'user_image')
Kanchan Chauhanb3fe6a42016-03-16 18:01:22 +0530130 return timelogs
131
132@frappe.whitelist()
133def get_timelogs_html(project, start=0):
Rushabh Mehtab2269dd2016-03-23 18:28:50 +0530134 return frappe.render_template("erpnext/templates/includes/projects/project_timelogs.html",
135 {"doc": {"timelogs": get_timelogs(project, start)}}, is_path=True)
136
137@frappe.whitelist()
Kanchan Chauhan2ad801c2016-03-22 16:00:41 +0530138def set_task_status(project, item_name):
139 task = frappe.get_doc("Task", item_name)
Kanchan Chauhanb3fe6a42016-03-16 18:01:22 +0530140 task.status = 'Closed'
141 task.save(ignore_permissions=True)
Rushabh Mehtab2269dd2016-03-23 18:28:50 +0530142
143@frappe.whitelist()
Kanchan Chauhan2ad801c2016-03-22 16:00:41 +0530144def set_issue_status(project, item_name):
145 issue = frappe.get_doc("Issue", item_name)
Kanchan Chauhanb3fe6a42016-03-16 18:01:22 +0530146 issue.status = 'Closed'
147 issue.save(ignore_permissions=True)
Rushabh Mehtab2269dd2016-03-23 18:28:50 +0530148