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