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): |
Kanchan Chauhan | a4ff5d3 | 2016-04-08 23:22:04 +0530 | [diff] [blame] | 9 | project_user = frappe.db.get_value("Project User", {"parent": frappe.form_dict.project, "user": frappe.session.user} , "user") |
| 10 | if not project_user or frappe.session.user == 'Guest': |
| 11 | raise frappe.PermissionError |
| 12 | |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 13 | context.no_cache = 1 |
Kanchan Chauhan | 11638ba | 2016-04-20 16:20:49 +0530 | [diff] [blame] | 14 | context.show_sidebar = True |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 15 | project = frappe.get_doc('Project', frappe.form_dict.project) |
| 16 | |
| 17 | project.has_permission('read') |
Kanchan Chauhan | b566d42 | 2016-03-29 11:21:42 +0530 | [diff] [blame] | 18 | |
Rushabh Mehta | c20c536 | 2016-03-25 17:19:28 +0530 | [diff] [blame] | 19 | project.tasks = get_tasks(project.name, start=0, item_status='open', |
Kanchan Chauhan | 239b351 | 2016-05-02 11:43:44 +0530 | [diff] [blame] | 20 | search=frappe.form_dict.get("search")) |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 21 | |
Kanchan Chauhan | 6d76359c | 2016-07-05 12:12:39 +0530 | [diff] [blame] | 22 | project.timesheets = get_timesheets(project.name, start=0, |
| 23 | search=frappe.form_dict.get("search")) |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 24 | |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 25 | |
| 26 | context.doc = project |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 27 | |
| 28 | |
Kanchan Chauhan | 2ad801c | 2016-03-22 16:00:41 +0530 | [diff] [blame] | 29 | def get_tasks(project, start=0, search=None, item_status=None): |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 30 | filters = {"project": project} |
| 31 | if search: |
| 32 | filters["subject"] = ("like", "%{0}%".format(search)) |
Kanchan Chauhan | 6d76359c | 2016-07-05 12:12:39 +0530 | [diff] [blame] | 33 | # if item_status: |
| 34 | # filters["status"] = item_status |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 35 | tasks = frappe.get_all("Task", filters=filters, |
Kanchan Chauhan | b566d42 | 2016-03-29 11:21:42 +0530 | [diff] [blame] | 36 | fields=["name", "subject", "status", "_seen", "_comments", "modified", "description"], |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 37 | limit_start=start, limit_page_length=10) |
| 38 | |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 39 | for task in tasks: |
| 40 | task.todo = frappe.get_all('ToDo',filters={'reference_name':task.name, 'reference_type':'Task'}, |
| 41 | fields=["assigned_by", "owner", "modified", "modified_by"]) |
Rushabh Mehta | c20c536 | 2016-03-25 17:19:28 +0530 | [diff] [blame] | 42 | |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 43 | if task.todo: |
| 44 | task.todo=task.todo[0] |
| 45 | 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] | 46 | |
Kanchan Chauhan | b566d42 | 2016-03-29 11:21:42 +0530 | [diff] [blame] | 47 | |
| 48 | task.comment_count = len(json.loads(task._comments or "[]")) |
Rushabh Mehta | c20c536 | 2016-03-25 17:19:28 +0530 | [diff] [blame] | 49 | |
| 50 | task.css_seen = '' |
| 51 | if task._seen: |
| 52 | if frappe.session.user in json.loads(task._seen): |
| 53 | task.css_seen = 'seen' |
| 54 | |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 55 | return tasks |
| 56 | |
| 57 | @frappe.whitelist() |
Rushabh Mehta | c20c536 | 2016-03-25 17:19:28 +0530 | [diff] [blame] | 58 | def get_task_html(project, start=0, item_status=None): |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 59 | return frappe.render_template("erpnext/templates/includes/projects/project_tasks.html", |
Rushabh Mehta | c20c536 | 2016-03-25 17:19:28 +0530 | [diff] [blame] | 60 | {"doc": { |
| 61 | "name": project, |
| 62 | "project_name": project, |
| 63 | "tasks": get_tasks(project, start, item_status=item_status)} |
| 64 | }, is_path=True) |
| 65 | |
Kanchan Chauhan | 6d76359c | 2016-07-05 12:12:39 +0530 | [diff] [blame] | 66 | def get_timesheets(project, start=0, search=None): |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 67 | filters = {"project": project} |
| 68 | if search: |
Kanchan Chauhan | 6d76359c | 2016-07-05 12:12:39 +0530 | [diff] [blame] | 69 | filters["activity_type"] = ("like", "%{0}%".format(search)) |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 70 | |
Kanchan Chauhan | 6d76359c | 2016-07-05 12:12:39 +0530 | [diff] [blame] | 71 | timesheets = frappe.get_all('Timesheet Detail', filters=filters, |
| 72 | fields=['project','activity_type','from_time','to_time','parent'], |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 73 | limit_start=start, limit_page_length=10) |
Kanchan Chauhan | 6d76359c | 2016-07-05 12:12:39 +0530 | [diff] [blame] | 74 | for timesheet in timesheets: |
| 75 | timesheet.infos = frappe.get_all('Timesheet', filters={"name": timesheet.parent}, |
| 76 | fields=['name','_comments','_seen','status','modified','modified_by'], |
| 77 | limit_start=start, limit_page_length=10) |
Kanchan Chauhan | b566d42 | 2016-03-29 11:21:42 +0530 | [diff] [blame] | 78 | |
Kanchan Chauhan | 6d76359c | 2016-07-05 12:12:39 +0530 | [diff] [blame] | 79 | for timesheet.info in timesheet.infos: |
| 80 | timesheet.info.user_image = frappe.db.get_value('User', timesheet.info.modified_by, 'user_image') |
| 81 | |
| 82 | timesheet.info.comment_count = len(json.loads(timesheet.info._comments or "[]")) |
| 83 | |
| 84 | timesheet.info.css_seen = '' |
| 85 | if timesheet.info._seen: |
| 86 | if frappe.session.user in json.loads(timesheet.info._seen): |
| 87 | timesheet.info.css_seen = 'seen' |
| 88 | return timesheets |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 89 | |
| 90 | @frappe.whitelist() |
Kanchan Chauhan | 6d76359c | 2016-07-05 12:12:39 +0530 | [diff] [blame] | 91 | def get_timesheet_html(project, start=0): |
| 92 | return frappe.render_template("erpnext/templates/includes/projects/project_timesheets.html", |
| 93 | {"doc": {"timesheets": get_timesheets(project, start)}}, is_path=True) |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 94 | |