From fa2023d3daff5651fe52f248931eecc119fb6826 Mon Sep 17 00:00:00 2001 From: Neil Trini Lasrado Date: Mon, 13 Feb 2017 19:26:16 +0530 Subject: [PATCH] Added ability to send newsletter to Student batch Guardians and Student Group Guardians --- erpnext/schools/api.py | 32 ++++++++- .../doctype/student_batch/student_batch.js | 18 ++++- .../doctype/student_batch/student_batch.py | 6 ++ .../doctype/student_group/student_group.js | 65 ++++++++++++------- .../doctype/student_group/student_group.py | 5 ++ 5 files changed, 99 insertions(+), 27 deletions(-) diff --git a/erpnext/schools/api.py b/erpnext/schools/api.py index 313b5ff23a..f1d375333e 100644 --- a/erpnext/schools/api.py +++ b/erpnext/schools/api.py @@ -8,6 +8,7 @@ import json from frappe import _ from frappe.model.mapper import get_mapped_doc from frappe.utils import flt, cstr +from frappe.email.doctype.email_group.email_group import add_subscribers @frappe.whitelist() def enroll_student(source_name): @@ -82,6 +83,16 @@ def make_attendance_records(student, student_name, status, course_schedule=None, student_attendance.status = status student_attendance.submit() +@frappe.whitelist() +def get_student_guardians(student): + """Returns List of Guardians of a Student. + + :param student: Student. + """ + guardians = frappe.get_list("Student Guardian", fields=["guardian"] , + filters={"parent": student}) + return guardians + @frappe.whitelist() def get_student_batch_students(student_batch): """Returns List of student, student_name, idx in Student Batch. @@ -253,4 +264,23 @@ def mark_assessment_result(student, assessment_plan, scores): }) assessment_result.save() assessment_result.submit() - return assessment_result \ No newline at end of file + return assessment_result + +@frappe.whitelist() +def update_email_group(doctype, name): + if not frappe.db.exists("Email Group", name): + email_group = frappe.new_doc("Email Group") + email_group.title = name + email_group.save() + email_list = [] + students = [] + if doctype == "Student Batch": + students = get_student_batch_students(name) + if doctype == "Student Group": + students = get_student_group_students(name) + for stud in students: + for guard in get_student_guardians(stud.student): + email = frappe.db.get_value("Guardian", guard.guardian, "email_address") + if email: + email_list.append(email) + add_subscribers(name, email_list) \ No newline at end of file diff --git a/erpnext/schools/doctype/student_batch/student_batch.js b/erpnext/schools/doctype/student_batch/student_batch.js index 931e41b6d2..7fad5d7894 100644 --- a/erpnext/schools/doctype/student_batch/student_batch.js +++ b/erpnext/schools/doctype/student_batch/student_batch.js @@ -3,7 +3,23 @@ frappe.ui.form.on('Student Batch', { refresh: function(frm) { - + if (!frm.doc.__islocal) { + frm.add_custom_button(__("Update Email Group"), function() { + frappe.call({ + method: "erpnext.schools.api.update_email_group", + args: { + "doctype": "Student Batch", + "name": frm.doc.name + } + }); + }); + frm.add_custom_button(__("Newsletter"), function() { + frappe.route_options = { + email_group: frm.doc.name + } + frappe.set_route("List", "Newsletter"); + }); + } }, onload: function(frm){ diff --git a/erpnext/schools/doctype/student_batch/student_batch.py b/erpnext/schools/doctype/student_batch/student_batch.py index 1a0d799482..9c0f3d7530 100644 --- a/erpnext/schools/doctype/student_batch/student_batch.py +++ b/erpnext/schools/doctype/student_batch/student_batch.py @@ -6,6 +6,7 @@ from __future__ import unicode_literals from frappe.model.document import Document from erpnext.schools.utils import validate_duplicate_student import frappe +from frappe import _ class StudentBatch(Document): def autoname(self): @@ -16,3 +17,8 @@ class StudentBatch(Document): def validate(self): validate_duplicate_student(self.students) + self.validate_name() + + def validate_name(self): + if frappe.db.exists("Student Group", self.name): + frappe.throw(_("""Student Group exists with same name""")) diff --git a/erpnext/schools/doctype/student_group/student_group.js b/erpnext/schools/doctype/student_group/student_group.js index 392793a1f6..1dcbc3acfb 100644 --- a/erpnext/schools/doctype/student_group/student_group.js +++ b/erpnext/schools/doctype/student_group/student_group.js @@ -1,33 +1,48 @@ cur_frm.add_fetch("student", "title", "student_name"); frappe.ui.form.on("Student Group", { - refresh: function(frm) { - if (!frm.doc.__islocal) { - frm.add_custom_button(__("Course Schedule"), function() { - frappe.route_options = { - student_group: frm.doc.name - } - frappe.set_route("List", "Course Schedule"); - }); + refresh: function(frm) { + if (!frm.doc.__islocal) { + frm.add_custom_button(__("Course Schedule"), function() { + frappe.route_options = { + student_group: frm.doc.name + } + frappe.set_route("List", "Course Schedule"); + }); - frm.add_custom_button(__("Assessment Plan"), function() { - frappe.route_options = { - student_group: frm.doc.name - } - frappe.set_route("List", "Assessment Plan"); - }); - } - }, + frm.add_custom_button(__("Assessment Plan"), function() { + frappe.route_options = { + student_group: frm.doc.name + } + frappe.set_route("List", "Assessment Plan"); + }); + frm.add_custom_button(__("Update Email Group"), function() { + frappe.call({ + method: "erpnext.schools.api.update_email_group", + args: { + "doctype": "Student Group", + "name": frm.doc.name + } + }); + }); + frm.add_custom_button(__("Newsletter"), function() { + frappe.route_options = { + email_group: frm.doc.name + } + frappe.set_route("List", "Newsletter"); + }); + } + }, - onload: function(frm) { - cur_frm.set_query("academic_term", function() { - return { - "filters": { - "academic_year": (frm.doc.academic_year) - } - }; - }); - } + onload: function(frm) { + frm.set_query("academic_term", function() { + return { + "filters": { + "academic_year": (frm.doc.academic_year) + } + }; + }); + } }); //If Student Batch is entered, deduce program, academic_year and academic term from it diff --git a/erpnext/schools/doctype/student_group/student_group.py b/erpnext/schools/doctype/student_group/student_group.py index 2f2795757e..996f518813 100644 --- a/erpnext/schools/doctype/student_group/student_group.py +++ b/erpnext/schools/doctype/student_group/student_group.py @@ -30,6 +30,7 @@ class StudentGroup(Document): def validate(self): self.validate_strength() self.validate_student_name() + self.validate_name() if self.student_batch: self.validate_student_batch() validate_duplicate_student(self.students) @@ -42,6 +43,10 @@ class StudentGroup(Document): for d in self.students: d.student_name = frappe.db.get_value("Student", d.student, "title") + def validate_name(self): + if frappe.db.exists("Student Batch", self.name): + frappe.throw(_("""Student Batch exists with same name""")) + def validate_student_batch(self): student_batch_students = [] for d in get_student_batch_students(self.student_batch):