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 | |
Rushabh Mehta | c20c536 | 2016-03-25 17:19:28 +0530 | [diff] [blame] | 18 | project.tasks = get_tasks(project.name, start=0, item_status='open', |
| 19 | search=frappe.form_dict.get("q")) |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 20 | |
Rushabh Mehta | c20c536 | 2016-03-25 17:19:28 +0530 | [diff] [blame] | 21 | project.issues = get_issues(project.name, start=0, item_status='open', |
| 22 | search=frappe.form_dict.get("q")) |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 23 | |
Rushabh Mehta | c20c536 | 2016-03-25 17:19:28 +0530 | [diff] [blame] | 24 | project.timelogs = get_timelogs(project.name, start=0, |
| 25 | search=frappe.form_dict.get("q")) |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 26 | |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 27 | project.timelines = get_timeline(project.project_name, start=0) |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 28 | |
| 29 | |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 30 | |
| 31 | context.doc = project |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 32 | |
| 33 | |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 34 | def get_timeline(project, start=10): |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 35 | '''Get timeline from project, tasks, issues''' |
| 36 | issues_condition = '' |
| 37 | project_issues = get_issues(project) |
| 38 | |
| 39 | if project_issues: |
| 40 | issue_names = '({0})'.format(", ".join(["'{0}'".format(i.name) for i in project_issues])) |
| 41 | issues_condition = """or (reference_doctype='Issue' and reference_name IN {issue_names})""".format(issue_names=issue_names) |
| 42 | |
| 43 | |
Kanchan Chauhan | 2ad801c | 2016-03-22 16:00:41 +0530 | [diff] [blame] | 44 | timelines = frappe.db.sql(""" |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 45 | select |
| 46 | sender_full_name, |
| 47 | subject, communication_date, comment_type, name, creation, modified_by, reference_doctype, reference_name, |
| 48 | _liked_by, comment_type, _comments |
| 49 | from |
| 50 | tabCommunication |
| 51 | where |
Kanchan Chauhan | 2ad801c | 2016-03-22 16:00:41 +0530 | [diff] [blame] | 52 | (reference_doctype='Project' and reference_name=%s) |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 53 | or (timeline_doctype='Project' and timeline_name=%s) |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 54 | {issues_condition} |
| 55 | order by |
Kanchan Chauhan | 2ad801c | 2016-03-22 16:00:41 +0530 | [diff] [blame] | 56 | modified DESC limit {start}, {limit}""".format( |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 57 | issues_condition=issues_condition, start=start, limit=10), |
Kanchan Chauhan | 2ad801c | 2016-03-22 16:00:41 +0530 | [diff] [blame] | 58 | (project, project), as_dict=True); |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 59 | for timeline in timelines: |
| 60 | 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] | 61 | return timelines |
| 62 | |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 63 | @frappe.whitelist() |
Kanchan Chauhan | 2ad801c | 2016-03-22 16:00:41 +0530 | [diff] [blame] | 64 | def get_timelines_html(project, start=0): |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 65 | return frappe.render_template("erpnext/templates/includes/projects/timeline.html", |
Rushabh Mehta | c20c536 | 2016-03-25 17:19:28 +0530 | [diff] [blame] | 66 | {"doc": { |
| 67 | "timelines": get_timeline(project, start)} |
| 68 | }, is_path=True) |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 69 | |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 70 | def get_issue_list(project): |
| 71 | return [issue.name for issue in get_issues(project)] |
| 72 | |
Rushabh Mehta | c20c536 | 2016-03-25 17:19:28 +0530 | [diff] [blame] | 73 | |
| 74 | |
Kanchan Chauhan | 2ad801c | 2016-03-22 16:00:41 +0530 | [diff] [blame] | 75 | def get_tasks(project, start=0, search=None, item_status=None): |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 76 | filters = {"project": project} |
| 77 | if search: |
| 78 | filters["subject"] = ("like", "%{0}%".format(search)) |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 79 | if item_status: |
Kanchan Chauhan | 53ce94f | 2016-03-25 12:04:54 +0530 | [diff] [blame] | 80 | filters["status"] = item_status |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 81 | tasks = frappe.get_all("Task", filters=filters, |
Rushabh Mehta | c20c536 | 2016-03-25 17:19:28 +0530 | [diff] [blame] | 82 | fields=["name", "subject", "status", "exp_start_date", "exp_end_date", "priority", "_seen"], |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 83 | limit_start=start, limit_page_length=10) |
| 84 | |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 85 | for task in tasks: |
| 86 | task.todo = frappe.get_all('ToDo',filters={'reference_name':task.name, 'reference_type':'Task'}, |
| 87 | fields=["assigned_by", "owner", "modified", "modified_by"]) |
Rushabh Mehta | c20c536 | 2016-03-25 17:19:28 +0530 | [diff] [blame] | 88 | |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 89 | if task.todo: |
| 90 | task.todo=task.todo[0] |
| 91 | task.todo.user_image = frappe.db.get_value('User', task.todo.owner, 'user_image') |
Rushabh Mehta | c20c536 | 2016-03-25 17:19:28 +0530 | [diff] [blame] | 92 | |
Kanchan Chauhan | e14389e | 2016-03-23 14:14:38 +0530 | [diff] [blame] | 93 | if task._comments: |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 94 | task.comment_count = len(json.loads(task._comments or "[]")) |
Rushabh Mehta | c20c536 | 2016-03-25 17:19:28 +0530 | [diff] [blame] | 95 | |
| 96 | task.css_seen = '' |
| 97 | if task._seen: |
| 98 | if frappe.session.user in json.loads(task._seen): |
| 99 | task.css_seen = 'seen' |
| 100 | |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 101 | return tasks |
| 102 | |
| 103 | @frappe.whitelist() |
Rushabh Mehta | c20c536 | 2016-03-25 17:19:28 +0530 | [diff] [blame] | 104 | def get_task_html(project, start=0, item_status=None): |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 105 | return frappe.render_template("erpnext/templates/includes/projects/project_tasks.html", |
Rushabh Mehta | c20c536 | 2016-03-25 17:19:28 +0530 | [diff] [blame] | 106 | {"doc": { |
| 107 | "name": project, |
| 108 | "project_name": project, |
| 109 | "tasks": get_tasks(project, start, item_status=item_status)} |
| 110 | }, is_path=True) |
| 111 | |
| 112 | |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 113 | |
| 114 | |
Kanchan Chauhan | 2ad801c | 2016-03-22 16:00:41 +0530 | [diff] [blame] | 115 | def get_issues(project, start=0, search=None, item_status=None): |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 116 | filters = {"project": project} |
| 117 | if search: |
| 118 | filters["subject"] = ("like", "%{0}%".format(search)) |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 119 | if item_status: |
Kanchan Chauhan | 53ce94f | 2016-03-25 12:04:54 +0530 | [diff] [blame] | 120 | filters["status"] = item_status |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 121 | issues = frappe.get_all("Issue", filters=filters, |
| 122 | fields=["name", "subject", "status", "opening_date", "resolution_date", "resolution_details"], |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 123 | order_by='modified desc', |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 124 | limit_start=start, limit_page_length=10) |
| 125 | |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 126 | for issue in issues: |
| 127 | issue.todo = frappe.get_all('ToDo',filters={'reference_name':issue.name, 'reference_type':'Issue'}, |
| 128 | fields=["assigned_by", "owner", "modified", "modified_by"]) |
| 129 | if issue.todo: |
| 130 | issue.todo=issue.todo[0] |
| 131 | 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] | 132 | |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 133 | return issues |
| 134 | |
| 135 | @frappe.whitelist() |
Rushabh Mehta | c20c536 | 2016-03-25 17:19:28 +0530 | [diff] [blame] | 136 | def get_issue_html(project, start=0, item_status=None): |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 137 | return frappe.render_template("erpnext/templates/includes/projects/project_issues.html", |
Rushabh Mehta | c20c536 | 2016-03-25 17:19:28 +0530 | [diff] [blame] | 138 | {"doc": { |
| 139 | "name": project, |
| 140 | "project_name": project, |
| 141 | "issues": get_issues(project, start, item_status=item_status)} |
| 142 | }, is_path=True) |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 147 | |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 148 | def get_timelogs(project, start=0, search=None): |
| 149 | filters = {"project": project} |
| 150 | if search: |
| 151 | filters["title"] = ("like", "%{0}%".format(search)) |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 152 | |
| 153 | timelogs = frappe.get_all('Time Log', filters=filters, |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 154 | 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] | 155 | limit_start=start, limit_page_length=10) |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 156 | for timelog in timelogs: |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 157 | 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] | 158 | return timelogs |
| 159 | |
| 160 | @frappe.whitelist() |
Rushabh Mehta | c20c536 | 2016-03-25 17:19:28 +0530 | [diff] [blame] | 161 | def get_timelog_html(project, start=0): |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 162 | return frappe.render_template("erpnext/templates/includes/projects/project_timelogs.html", |
| 163 | {"doc": {"timelogs": get_timelogs(project, start)}}, is_path=True) |
| 164 | |