From 5807cd8e6fa6a12d06cb1a216cb6801df3f558d7 Mon Sep 17 00:00:00 2001 From: Manas Solanki Date: Fri, 22 Dec 2017 10:50:10 +0530 Subject: [PATCH] Set the guardian role automatically. (#12136) * Add the guardian role for parent portal * invite guardian as user --- erpnext/domains/education.py | 2 +- .../education/doctype/guardian/guardian.js | 14 +++++++- .../education/doctype/guardian/guardian.json | 33 ++++++++++++++++++- .../education/doctype/guardian/guardian.py | 27 ++++++++++++++- erpnext/patches.txt | 3 +- .../add_guardian_role_for_parent_portal.py | 23 +++++++++++++ erpnext/portal/utils.py | 4 ++- 7 files changed, 99 insertions(+), 7 deletions(-) create mode 100644 erpnext/patches/v10_0/add_guardian_role_for_parent_portal.py diff --git a/erpnext/domains/education.py b/erpnext/domains/education.py index e1e363d40a..0631f299ad 100644 --- a/erpnext/domains/education.py +++ b/erpnext/domains/education.py @@ -12,7 +12,7 @@ data = { 'Student Attendance Tool', 'Student Applicant' ], - 'default_portal_role': 'Student', + 'default_portal_role': 'Guardian', 'restricted_roles': [ 'Student', 'Instructor', diff --git a/erpnext/education/doctype/guardian/guardian.js b/erpnext/education/doctype/guardian/guardian.js index 139193f08e..b7c2f0af31 100644 --- a/erpnext/education/doctype/guardian/guardian.js +++ b/erpnext/education/doctype/guardian/guardian.js @@ -3,6 +3,18 @@ frappe.ui.form.on('Guardian', { refresh: function(frm) { - + if(!frm.doc.user && !frm.is_new()) { + frm.add_custom_button(__("Invite as User"), function() { + return frappe.call({ + method: "erpnext.education.doctype.guardian.guardian.invite_guardian", + args: { + guardian: frm.doc.name + }, + callback: function(r) { + frm.set_value("user", r.message); + } + }); + }); + } } }); diff --git a/erpnext/education/doctype/guardian/guardian.json b/erpnext/education/doctype/guardian/guardian.json index 500b747eb9..d8722e2c90 100644 --- a/erpnext/education/doctype/guardian/guardian.json +++ b/erpnext/education/doctype/guardian/guardian.json @@ -163,6 +163,37 @@ "set_only_once": 0, "unique": 0 }, + { + "allow_bulk_edit": 0, + "allow_on_submit": 0, + "bold": 0, + "collapsible": 0, + "columns": 0, + "fieldname": "user", + "fieldtype": "Link", + "hidden": 0, + "ignore_user_permissions": 0, + "ignore_xss_filter": 0, + "in_filter": 0, + "in_global_search": 0, + "in_list_view": 0, + "in_standard_filter": 0, + "label": "User Id", + "length": 0, + "no_copy": 0, + "options": "User", + "permlevel": 0, + "precision": "", + "print_hide": 0, + "print_hide_if_no_value": 0, + "read_only": 0, + "remember_last_selected_value": 0, + "report_hide": 0, + "reqd": 0, + "search_index": 0, + "set_only_once": 0, + "unique": 0 + }, { "allow_bulk_edit": 0, "allow_on_submit": 0, @@ -476,7 +507,7 @@ "issingle": 0, "istable": 0, "max_attachments": 0, - "modified": "2017-11-10 19:06:57.122193", + "modified": "2017-12-06 18:17:38.090252", "modified_by": "Administrator", "module": "Education", "name": "Guardian", diff --git a/erpnext/education/doctype/guardian/guardian.py b/erpnext/education/doctype/guardian/guardian.py index 38597f0fed..3741a06a90 100644 --- a/erpnext/education/doctype/guardian/guardian.py +++ b/erpnext/education/doctype/guardian/guardian.py @@ -4,7 +4,9 @@ from __future__ import unicode_literals import frappe +from frappe import _ from frappe.model.document import Document +from frappe.utils.csvutils import getlink class Guardian(Document): def __setup__(self): @@ -25,4 +27,27 @@ class Guardian(Document): }) def validate(self): - self.students = [] \ No newline at end of file + self.students = [] + + +@frappe.whitelist() +def invite_guardian(guardian): + guardian_doc = frappe.get_doc("Guardian", guardian) + if not guardian_doc.email_address: + frappe.throw(_("Please set Email Address")) + else: + guardian_as_user = frappe.get_value('User', dict(email=guardian_doc.email_address)) + print guardian_as_user + if guardian_as_user: + frappe.msgprint(_("User {0} already exists").format(getlink("User", guardian_as_user))) + return guardian_as_user + else: + user = frappe.get_doc({ + "doctype": "User", + "first_name": guardian_doc.guardian_name, + "email": guardian_doc.email_address, + "user_type": "Website User", + "send_welcome_email": 1 + }).insert(ignore_permissions = True) + frappe.msgprint(_("User {0} created").format(getlink("User", user.name))) + return user.name diff --git a/erpnext/patches.txt b/erpnext/patches.txt index 7966d81a5d..f42799fb84 100644 --- a/erpnext/patches.txt +++ b/erpnext/patches.txt @@ -483,5 +483,4 @@ erpnext.patches.v10_0.set_primary_contact_for_customer erpnext.patches.v10_0.copy_projects_renamed_fields erpnext.patches.v10_0.enabled_regional_print_format_based_on_country erpnext.patches.v10_0.update_asset_calculate_depreciation -erpnext.patches.v10_0.enabled_regional_print_format_based_on_country -erpnext.patches.v10_0.update_asset_calculate_depreciation +erpnext.patches.v10_0.add_guardian_role_for_parent_portal diff --git a/erpnext/patches/v10_0/add_guardian_role_for_parent_portal.py b/erpnext/patches/v10_0/add_guardian_role_for_parent_portal.py new file mode 100644 index 0000000000..0b891f21f4 --- /dev/null +++ b/erpnext/patches/v10_0/add_guardian_role_for_parent_portal.py @@ -0,0 +1,23 @@ +# Copyright (c) 2017, Frappe and Contributors +# License: GNU General Public License v3. See license.txt + +from __future__ import unicode_literals +import frappe + +def execute(): + # create guardian role + if not frappe.get_value('Role', dict(role_name='Guardian')): + frappe.get_doc({ + 'doctype': 'Role', + 'role_name': 'Guardian', + 'desk_access': 0, + 'restrict_to_domain': 'Education' + }).insert(ignore_permissions=True) + + # set guardian roles in already created users + if frappe.db.exists("Doctype", "Guardian"): + for user in frappe.db.sql_list("""select u.name from `tabUser` u , `tabGuardian` g where g.email_address = u.name"""): + user = frappe.get_doc('User', user) + user.flags.ignore_validate = True + user.flags.ignore_mandatory = True + user.save() diff --git a/erpnext/portal/utils.py b/erpnext/portal/utils.py index 8115c1fb9b..93fe5da693 100644 --- a/erpnext/portal/utils.py +++ b/erpnext/portal/utils.py @@ -1,7 +1,7 @@ import frappe def set_default_role(doc, method): - '''Set customer, supplier, student based on email''' + '''Set customer, supplier, student, guardian based on email''' if frappe.flags.setting_role or frappe.flags.in_migrate: return @@ -18,3 +18,5 @@ def set_default_role(doc, method): doc.add_roles('Supplier') elif frappe.get_value('Student', dict(student_email_id=doc.email)) and 'Student' not in roles: doc.add_roles('Student') + elif frappe.get_value('Guardian', dict(email_address=doc.email)) and 'Guardian' not in roles: + doc.add_roles('Guardian')