feat: Added support for approve/reject join requests to a cohort subgroup
Issue #271
This commit is contained in:
@@ -70,3 +70,41 @@ def join_cohort(course, cohort, subgroup, invite_code):
|
|||||||
doc = frappe.get_doc(data)
|
doc = frappe.get_doc(data)
|
||||||
doc.insert(ignore_permissions=True)
|
doc.insert(ignore_permissions=True)
|
||||||
return {"ok": True}
|
return {"ok": True}
|
||||||
|
|
||||||
|
@frappe.whitelist()
|
||||||
|
def approve_cohort_join_request(join_request):
|
||||||
|
r = frappe.get_doc("Cohort Join Request", join_request)
|
||||||
|
sg = r and frappe.get_doc("Cohort Subgroup", r.subgroup)
|
||||||
|
if not sg or r.status not in ["Pending", "Accepted"]:
|
||||||
|
return {
|
||||||
|
"ok": False,
|
||||||
|
"error": "Invalid Join Request"
|
||||||
|
}
|
||||||
|
if not sg.is_manager(frappe.session.user):
|
||||||
|
return {
|
||||||
|
"ok": False,
|
||||||
|
"error": "Permission Deined"
|
||||||
|
}
|
||||||
|
|
||||||
|
r.status = "Accepted"
|
||||||
|
r.save(ignore_permissions=True)
|
||||||
|
return {"ok": True}
|
||||||
|
|
||||||
|
@frappe.whitelist()
|
||||||
|
def reject_cohort_join_request(join_request):
|
||||||
|
r = frappe.get_doc("Cohort Join Request", join_request)
|
||||||
|
sg = r and frappe.get_doc("Cohort Subgroup", r.subgroup)
|
||||||
|
if not sg or r.status not in ["Pending", "Rejected"]:
|
||||||
|
return {
|
||||||
|
"ok": False,
|
||||||
|
"error": "Invalid Join Request"
|
||||||
|
}
|
||||||
|
if not sg.is_manager(frappe.session.user):
|
||||||
|
return {
|
||||||
|
"ok": False,
|
||||||
|
"error": "Permission Deined"
|
||||||
|
}
|
||||||
|
|
||||||
|
r.status = "Rejected"
|
||||||
|
r.save(ignore_permissions=True)
|
||||||
|
return {"ok": True}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
# Copyright (c) 2021, FOSS United and contributors
|
# Copyright (c) 2021, FOSS United and contributors
|
||||||
# For license information, please see license.txt
|
# For license information, please see license.txt
|
||||||
|
|
||||||
# import frappe
|
import frappe
|
||||||
from frappe.model.document import Document
|
from frappe.model.document import Document
|
||||||
|
|
||||||
class CohortJoinRequest(Document):
|
class CohortJoinRequest(Document):
|
||||||
@@ -10,4 +10,25 @@ class CohortJoinRequest(Document):
|
|||||||
self.ensure_student()
|
self.ensure_student()
|
||||||
|
|
||||||
def ensure_student(self):
|
def ensure_student(self):
|
||||||
pass
|
q = {
|
||||||
|
"doctype": "LMS Batch Membership",
|
||||||
|
"cohort": self.cohort,
|
||||||
|
"subgroup": self.subgroup,
|
||||||
|
"email": self.email
|
||||||
|
}
|
||||||
|
if frappe.db.exists(q):
|
||||||
|
return
|
||||||
|
|
||||||
|
cohort = frappe.get_doc("Cohort", self.cohort)
|
||||||
|
|
||||||
|
data = {
|
||||||
|
"doctype": "LMS Batch Membership",
|
||||||
|
"course": cohort.course,
|
||||||
|
"cohort": self.cohort,
|
||||||
|
"subgroup": self.subgroup,
|
||||||
|
"member": self.email,
|
||||||
|
"member_type": "Student",
|
||||||
|
"role": "Member"
|
||||||
|
}
|
||||||
|
doc = frappe.get_doc(data)
|
||||||
|
doc.insert(ignore_permissions=True)
|
||||||
|
|||||||
@@ -57,5 +57,15 @@ class CohortSubgroup(Document):
|
|||||||
}
|
}
|
||||||
return frappe.db.exists(q)
|
return frappe.db.exists(q)
|
||||||
|
|
||||||
|
def is_manager(self, email):
|
||||||
|
"""Returns True if the given user is a manager of this subgroup.
|
||||||
|
|
||||||
|
Mentors of the subgroup, admins of the Cohort are considered as managers.
|
||||||
|
"""
|
||||||
|
return self.is_mentor(email) or self.get_cohort().is_admin(email)
|
||||||
|
|
||||||
|
def get_cohort(self):
|
||||||
|
return frappe.get_doc("Cohort", self.cohort)
|
||||||
|
|
||||||
#def after_doctype_insert():
|
#def after_doctype_insert():
|
||||||
# frappe.db.add_unique("Cohort Subgroup", ("cohort", "slug"))
|
# frappe.db.add_unique("Cohort Subgroup", ("cohort", "slug"))
|
||||||
|
|||||||
@@ -59,6 +59,8 @@
|
|||||||
|
|
||||||
|
|
||||||
{% macro render_join_requests() %}
|
{% macro render_join_requests() %}
|
||||||
|
{% set join_requests = subgroup.get_join_requests() %}
|
||||||
|
{% if join_requests %}
|
||||||
<table class="table">
|
<table class="table">
|
||||||
<tr>
|
<tr>
|
||||||
<th>#</th>
|
<th>#</th>
|
||||||
@@ -78,6 +80,9 @@
|
|||||||
</tr>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</table>
|
</table>
|
||||||
|
{% else %}
|
||||||
|
<em>There are no pending join requests.</em>
|
||||||
|
{% endif %}
|
||||||
{% endmacro %}
|
{% endmacro %}
|
||||||
|
|
||||||
{% macro render_navitem(title, link, count, active) %}
|
{% macro render_navitem(title, link, count, active) %}
|
||||||
@@ -118,7 +123,7 @@ $(function() {
|
|||||||
frappe.confirm(
|
frappe.confirm(
|
||||||
`Are you sure to accept ${email} to this subgroup?`,
|
`Are you sure to accept ${email} to this subgroup?`,
|
||||||
function() {
|
function() {
|
||||||
console.log("approve", name);
|
run_action("school.lms.api.approve_cohort_join_request", name);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
@@ -127,10 +132,23 @@ $(function() {
|
|||||||
var name = $(this).parent().data("name");
|
var name = $(this).parent().data("name");
|
||||||
var email = $(this).parent().data("email");
|
var email = $(this).parent().data("email");
|
||||||
frappe.confirm(`Are you sure to reject <strong>${email}</strong> from joining this subgroup?`, function() {
|
frappe.confirm(`Are you sure to reject <strong>${email}</strong> from joining this subgroup?`, function() {
|
||||||
console.log("reject", name);
|
run_action("school.lms.api.reject_cohort_join_request", name);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function run_action(method, join_request) {
|
||||||
|
frappe.call(method, {
|
||||||
|
join_request: join_request,
|
||||||
|
})
|
||||||
|
.then(r => {
|
||||||
|
if (r.message.ok) {
|
||||||
|
window.location.reload();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
frappe.msgprint(r.message.error);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|||||||
Reference in New Issue
Block a user