Set the guardian role automatically. (#12136)

* Add the guardian role for parent portal

* invite guardian as user
diff --git a/erpnext/domains/education.py b/erpnext/domains/education.py
index e1e363d..0631f29 100644
--- a/erpnext/domains/education.py
+++ b/erpnext/domains/education.py
@@ -12,7 +12,7 @@
 		'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 139193f..b7c2f0a 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 500b747..d8722e2 100644
--- a/erpnext/education/doctype/guardian/guardian.json
+++ b/erpnext/education/doctype/guardian/guardian.json
@@ -169,6 +169,37 @@
    "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, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "column_break_3", 
    "fieldtype": "Column Break", 
    "hidden": 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 38597f0..3741a06 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 @@
 			})
 
 	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 7966d81..f42799f 100644
--- a/erpnext/patches.txt
+++ b/erpnext/patches.txt
@@ -483,5 +483,4 @@
 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 0000000..0b891f2
--- /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 8115c1f..93fe5da 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 @@
 				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')