Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 1 | # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors |
| 2 | # License: GNU General Public License v3. See license.txt |
| 3 | |
| 4 | from __future__ import unicode_literals |
| 5 | import frappe |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 6 | import json |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 7 | |
| 8 | def 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 Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 19 | |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 20 | project.timelogs = get_timelogs(project.name, start=0, search=frappe.form_dict.get("q")) |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 21 | |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 22 | project.issues = get_issues(project.name, start=0, search=frappe.form_dict.get("q")) |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 23 | |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 24 | project.timelines = get_timeline(project.project_name, start=0) |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 25 | |
| 26 | |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 27 | |
| 28 | context.doc = project |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 29 | |
| 30 | |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 31 | def get_timeline(project, start=10): |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 32 | '''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 Chauhan | 2ad801c | 2016-03-22 16:00:41 +0530 | [diff] [blame] | 41 | timelines = frappe.db.sql(""" |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 42 | 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 Chauhan | 2ad801c | 2016-03-22 16:00:41 +0530 | [diff] [blame] | 49 | (reference_doctype='Project' and reference_name=%s) |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 50 | or (timeline_doctype='Project' and timeline_name=%s) |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 51 | {issues_condition} |
| 52 | order by |
Kanchan Chauhan | 2ad801c | 2016-03-22 16:00:41 +0530 | [diff] [blame] | 53 | modified DESC limit {start}, {limit}""".format( |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 54 | issues_condition=issues_condition, start=start, limit=10), |
Kanchan Chauhan | 2ad801c | 2016-03-22 16:00:41 +0530 | [diff] [blame] | 55 | (project, project), as_dict=True); |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 56 | for timeline in timelines: |
| 57 | timeline.user_image = frappe.db.get_value('User', timeline.modified_by, 'user_image') |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 58 | return timelines |
| 59 | |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 60 | @frappe.whitelist() |
Kanchan Chauhan | 2ad801c | 2016-03-22 16:00:41 +0530 | [diff] [blame] | 61 | def get_timelines_html(project, start=0): |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 62 | return frappe.render_template("erpnext/templates/includes/projects/timeline.html", |
| 63 | {"doc": {"timelines": get_timeline(project, start)}}, is_path=True) |
| 64 | |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 65 | def get_issue_list(project): |
| 66 | return [issue.name for issue in get_issues(project)] |
| 67 | |
Kanchan Chauhan | 2ad801c | 2016-03-22 16:00:41 +0530 | [diff] [blame] | 68 | def get_tasks(project, start=0, search=None, item_status=None): |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 69 | filters = {"project": project} |
| 70 | if search: |
| 71 | filters["subject"] = ("like", "%{0}%".format(search)) |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 72 | if item_status: |
Kanchan Chauhan | 53ce94f | 2016-03-25 12:04:54 +0530 | [diff] [blame] | 73 | filters["status"] = item_status |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 74 | 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 Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 78 | for task in tasks: |
Kanchan Chauhan | e14389e | 2016-03-23 14:14:38 +0530 | [diff] [blame] | 79 | print task._comments |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 80 | 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 Chauhan | e14389e | 2016-03-23 14:14:38 +0530 | [diff] [blame] | 85 | if task._comments: |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 86 | task.comment_count = len(json.loads(task._comments or "[]")) |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 87 | return tasks |
| 88 | |
| 89 | @frappe.whitelist() |
Kanchan Chauhan | 2ad801c | 2016-03-22 16:00:41 +0530 | [diff] [blame] | 90 | def get_tasks_html(project, start=0, item_status=None): |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 91 | return frappe.render_template("erpnext/templates/includes/projects/project_tasks.html", |
Kanchan Chauhan | 2ad801c | 2016-03-22 16:00:41 +0530 | [diff] [blame] | 92 | {"doc": {"tasks": get_tasks(project, start, item_status=item_status)}}, is_path=True) |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 93 | |
| 94 | |
Kanchan Chauhan | 2ad801c | 2016-03-22 16:00:41 +0530 | [diff] [blame] | 95 | def get_issues(project, start=0, search=None, item_status=None): |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 96 | filters = {"project": project} |
| 97 | if search: |
| 98 | filters["subject"] = ("like", "%{0}%".format(search)) |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 99 | if item_status: |
Kanchan Chauhan | 53ce94f | 2016-03-25 12:04:54 +0530 | [diff] [blame] | 100 | filters["status"] = item_status |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 101 | issues = frappe.get_all("Issue", filters=filters, |
| 102 | fields=["name", "subject", "status", "opening_date", "resolution_date", "resolution_details"], |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 103 | order_by='modified desc', |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 104 | limit_start=start, limit_page_length=10) |
| 105 | |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 106 | 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 Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 112 | |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 113 | return issues |
| 114 | |
| 115 | @frappe.whitelist() |
Kanchan Chauhan | 2ad801c | 2016-03-22 16:00:41 +0530 | [diff] [blame] | 116 | def get_issues_html(project, start=0, item_status=None): |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 117 | 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 Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 120 | def get_timelogs(project, start=0, search=None): |
| 121 | filters = {"project": project} |
| 122 | if search: |
| 123 | filters["title"] = ("like", "%{0}%".format(search)) |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 124 | |
| 125 | timelogs = frappe.get_all('Time Log', filters=filters, |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 126 | fields=['name','title','task','activity_type','from_time','to_time','hours','status','modified','modified_by'], |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 127 | limit_start=start, limit_page_length=10) |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 128 | for timelog in timelogs: |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 129 | timelog.user_image = frappe.db.get_value('User', timelog.modified_by, 'user_image') |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 130 | return timelogs |
| 131 | |
| 132 | @frappe.whitelist() |
| 133 | def get_timelogs_html(project, start=0): |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 134 | 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 Chauhan | 2ad801c | 2016-03-22 16:00:41 +0530 | [diff] [blame] | 138 | def set_task_status(project, item_name): |
| 139 | task = frappe.get_doc("Task", item_name) |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 140 | task.status = 'Closed' |
| 141 | task.save(ignore_permissions=True) |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 142 | |
| 143 | @frappe.whitelist() |
Kanchan Chauhan | 2ad801c | 2016-03-22 16:00:41 +0530 | [diff] [blame] | 144 | def set_issue_status(project, item_name): |
| 145 | issue = frappe.get_doc("Issue", item_name) |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 146 | issue.status = 'Closed' |
| 147 | issue.save(ignore_permissions=True) |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 148 | |