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 |
| 6 | |
| 7 | from frappe import _ |
| 8 | |
| 9 | def get_context(context): |
| 10 | context.no_cache = 1 |
| 11 | |
| 12 | project = frappe.get_doc('Project', frappe.form_dict.project) |
| 13 | |
| 14 | project.has_permission('read') |
| 15 | |
| 16 | context.issues = frappe.get_all('Issue', filters={'project': project.project_name}, |
| 17 | fields=['subject', 'opening_date', 'resolution_date', 'status', 'name', 'resolution_details','modified','modified_by']) |
| 18 | |
| 19 | project.tasks = get_tasks(project.name, start=0, search=frappe.form_dict.get("q")) |
| 20 | |
| 21 | project.timelogs = get_timelogs(project.name, start=0, search=frappe.form_dict.get("q")) |
| 22 | |
| 23 | project.issues = get_issues(project.name, start=0, search=frappe.form_dict.get("q")) |
| 24 | |
| 25 | project.timelines = get_timeline(project.project_name, start=0) |
| 26 | |
| 27 | |
| 28 | |
| 29 | context.doc = project |
| 30 | |
| 31 | |
| 32 | def get_timeline(project, start=10): |
| 33 | issue_names = '({0})'.format(", ".join(["'{0}'".format(i.name) for i in get_issues(project)])) |
| 34 | |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 35 | |
Kanchan Chauhan | 2ad801c | 2016-03-22 16:00:41 +0530 | [diff] [blame] | 36 | timelines = frappe.db.sql(""" |
| 37 | select |
| 38 | sender_full_name, |
| 39 | subject, communication_date, comment_type, name, creation, modified_by, reference_doctype, reference_name, |
| 40 | _liked_by, comment_type, _comments |
| 41 | from |
| 42 | tabCommunication |
| 43 | where |
| 44 | (reference_doctype='Project' and reference_name=%s) |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 45 | or (timeline_doctype='Project' and timeline_name=%s) |
| 46 | or (reference_doctype='Issue' and reference_name IN {issue_names}) |
Kanchan Chauhan | 2ad801c | 2016-03-22 16:00:41 +0530 | [diff] [blame] | 47 | order by |
| 48 | modified DESC limit {start}, {limit}""".format( |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 49 | issue_names=issue_names, start=start, limit=10), |
Kanchan Chauhan | 2ad801c | 2016-03-22 16:00:41 +0530 | [diff] [blame] | 50 | (project, project), as_dict=True); |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 51 | for timeline in timelines: |
| 52 | timeline.user_image = frappe.db.get_value('User', timeline.modified_by, 'user_image') |
| 53 | return timelines |
| 54 | |
| 55 | @frappe.whitelist() |
Kanchan Chauhan | 2ad801c | 2016-03-22 16:00:41 +0530 | [diff] [blame] | 56 | def get_timelines_html(project, start=0): |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 57 | return frappe.render_template("erpnext/templates/includes/projects/timeline.html", |
| 58 | {"doc": {"timelines": get_timeline(project, start)}}, is_path=True) |
| 59 | |
| 60 | def get_issue_list(project): |
| 61 | return [issue.name for issue in get_issues(project)] |
| 62 | |
Kanchan Chauhan | 2ad801c | 2016-03-22 16:00:41 +0530 | [diff] [blame] | 63 | def get_tasks(project, start=0, search=None, item_status=None): |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 64 | filters = {"project": project} |
| 65 | if search: |
| 66 | filters["subject"] = ("like", "%{0}%".format(search)) |
Kanchan Chauhan | 2ad801c | 2016-03-22 16:00:41 +0530 | [diff] [blame] | 67 | if item_status: |
| 68 | filters = {"status": item_status} |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 69 | tasks = frappe.get_all("Task", filters=filters, |
| 70 | fields=["name", "subject", "status", "exp_start_date", "exp_end_date", "priority"], |
Kanchan Chauhan | 2ad801c | 2016-03-22 16:00:41 +0530 | [diff] [blame] | 71 | limit_start=start, limit_page_length=10) |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 72 | |
| 73 | for task in tasks: |
| 74 | task.todo = frappe.get_all('ToDo',filters={'reference_name':task.name, 'reference_type':'Task'}, |
| 75 | fields=["assigned_by", "owner", "modified", "modified_by"]) |
| 76 | if task.todo: |
| 77 | task.todo=task.todo[0] |
| 78 | task.todo.user_image = frappe.db.get_value('User', task.todo.owner, 'user_image') |
| 79 | |
| 80 | return tasks |
| 81 | |
| 82 | @frappe.whitelist() |
Kanchan Chauhan | 2ad801c | 2016-03-22 16:00:41 +0530 | [diff] [blame] | 83 | def get_tasks_html(project, start=0, item_status=None): |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 84 | return frappe.render_template("erpnext/templates/includes/projects/project_tasks.html", |
Kanchan Chauhan | 2ad801c | 2016-03-22 16:00:41 +0530 | [diff] [blame] | 85 | {"doc": {"tasks": get_tasks(project, start, item_status=item_status)}}, is_path=True) |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 86 | |
| 87 | |
Kanchan Chauhan | 2ad801c | 2016-03-22 16:00:41 +0530 | [diff] [blame] | 88 | def get_issues(project, start=0, search=None, item_status=None): |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 89 | filters = {"project": project} |
| 90 | if search: |
| 91 | filters["subject"] = ("like", "%{0}%".format(search)) |
Kanchan Chauhan | 2ad801c | 2016-03-22 16:00:41 +0530 | [diff] [blame] | 92 | if item_status: |
| 93 | filters = {"status": item_status} |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 94 | issues = frappe.get_all("Issue", filters=filters, |
| 95 | fields=["name", "subject", "status", "opening_date", "resolution_date", "resolution_details"], |
| 96 | order_by='modified desc', |
Kanchan Chauhan | 2ad801c | 2016-03-22 16:00:41 +0530 | [diff] [blame] | 97 | limit_start=start, limit_page_length=10) |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 98 | |
| 99 | for issue in issues: |
| 100 | issue.todo = frappe.get_all('ToDo',filters={'reference_name':issue.name, 'reference_type':'Issue'}, |
| 101 | fields=["assigned_by", "owner", "modified", "modified_by"]) |
| 102 | if issue.todo: |
| 103 | issue.todo=issue.todo[0] |
| 104 | issue.todo.user_image = frappe.db.get_value('User', issue.todo.owner, 'user_image') |
| 105 | |
| 106 | return issues |
| 107 | |
| 108 | @frappe.whitelist() |
Kanchan Chauhan | 2ad801c | 2016-03-22 16:00:41 +0530 | [diff] [blame] | 109 | def get_issues_html(project, start=0, item_status=None): |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 110 | return frappe.render_template("erpnext/templates/includes/projects/project_issues.html", |
Kanchan Chauhan | 2ad801c | 2016-03-22 16:00:41 +0530 | [diff] [blame] | 111 | {"doc": {"issues": get_issues(project, start, item_status=item_status)}}, is_path=True) |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 112 | |
| 113 | def get_timelogs(project, start=0, search=None): |
| 114 | filters = {"project": project} |
| 115 | if search: |
| 116 | filters["title"] = ("like", "%{0}%".format(search)) |
| 117 | |
| 118 | timelogs = frappe.get_all('Time Log', filters=filters, |
| 119 | fields=['name','title','task','activity_type','from_time','to_time','hours','status','modified','modified_by'], |
Kanchan Chauhan | 2ad801c | 2016-03-22 16:00:41 +0530 | [diff] [blame] | 120 | limit_start=start, limit_page_length=10) |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 121 | for timelog in timelogs: |
| 122 | timelog.user_image = frappe.db.get_value('User', timelog.modified_by, 'user_image') |
| 123 | return timelogs |
| 124 | |
| 125 | @frappe.whitelist() |
| 126 | def get_timelogs_html(project, start=0): |
| 127 | return frappe.render_template("erpnext/templates/includes/projects/project_timelogs.html", |
| 128 | {"doc": {"timelogs": get_timelogs(project, start)}}, is_path=True) |
| 129 | |
| 130 | @frappe.whitelist() |
Kanchan Chauhan | 2ad801c | 2016-03-22 16:00:41 +0530 | [diff] [blame] | 131 | def set_task_status(project, item_name): |
| 132 | task = frappe.get_doc("Task", item_name) |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 133 | task.status = 'Closed' |
| 134 | task.save(ignore_permissions=True) |
| 135 | |
| 136 | @frappe.whitelist() |
Kanchan Chauhan | 2ad801c | 2016-03-22 16:00:41 +0530 | [diff] [blame] | 137 | def set_issue_status(project, item_name): |
| 138 | issue = frappe.get_doc("Issue", item_name) |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 139 | issue.status = 'Closed' |
| 140 | issue.save(ignore_permissions=True) |
| 141 | |
| 142 | |