refactor: added methods to access mentors from course with test cases
This commit is contained in:
@@ -25,6 +25,9 @@ class CommunityMember(WebsiteGenerator):
|
|||||||
frappe.throw(_("Username can only contain alphabets, numbers and underscore."))
|
frappe.throw(_("Username can only contain alphabets, numbers and underscore."))
|
||||||
self.username = self.username.lower()
|
self.username = self.username.lower()
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f"<CommunityMember: {self.email}>"
|
||||||
|
|
||||||
def create_member_from_user(doc, method):
|
def create_member_from_user(doc, method):
|
||||||
member = frappe.get_doc({
|
member = frappe.get_doc({
|
||||||
"doctype": "Community Member",
|
"doctype": "Community Member",
|
||||||
|
|||||||
@@ -19,6 +19,9 @@ class LMSCourse(Document):
|
|||||||
slugs = set([row['slug'] for row in result])
|
slugs = set([row['slug'] for row in result])
|
||||||
return slugify(title, used_slugs=slugs)
|
return slugify(title, used_slugs=slugs)
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f"<Course#{self.name} {self.slug}>"
|
||||||
|
|
||||||
def get_topic(self, slug):
|
def get_topic(self, slug):
|
||||||
"""Returns the topic with given slug in this course as a Document.
|
"""Returns the topic with given slug in this course as a Document.
|
||||||
"""
|
"""
|
||||||
@@ -29,3 +32,59 @@ class LMSCourse(Document):
|
|||||||
if result:
|
if result:
|
||||||
row = result[0]
|
row = result[0]
|
||||||
return frappe.get_doc('LMS Topic', row['name'])
|
return frappe.get_doc('LMS Topic', row['name'])
|
||||||
|
|
||||||
|
def has_mentor(self, email):
|
||||||
|
"""Checks if this course has a mentor with given email.
|
||||||
|
"""
|
||||||
|
if not email or email == "Guest":
|
||||||
|
return False
|
||||||
|
|
||||||
|
member = self.get_community_member(email)
|
||||||
|
if not member:
|
||||||
|
return False
|
||||||
|
|
||||||
|
mapping = frappe.get_all("LMS Course Mentor Mapping", {"course": self.name, "mentor": member})
|
||||||
|
return mapping != []
|
||||||
|
|
||||||
|
def get_community_member(self, email):
|
||||||
|
"""Returns the name of Community Member document for a give user.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
return frappe.db.get_value("Community Member", {"email": email}, ["name"])
|
||||||
|
except frappe.DoesNotExistError:
|
||||||
|
return None
|
||||||
|
|
||||||
|
def add_mentor(self, email):
|
||||||
|
"""Adds a new mentor to the course.
|
||||||
|
"""
|
||||||
|
if not email:
|
||||||
|
raise ValueError("Invalid email")
|
||||||
|
if email == "Guest":
|
||||||
|
raise ValueError("Guest user can not be added as a mentor")
|
||||||
|
|
||||||
|
# given user is already a mentor
|
||||||
|
if self.has_mentor(email):
|
||||||
|
return
|
||||||
|
|
||||||
|
member = self.get_community_member(email)
|
||||||
|
if not member:
|
||||||
|
return False
|
||||||
|
|
||||||
|
doc = frappe.get_doc({
|
||||||
|
"doctype": "LMS Course Mentor Mapping",
|
||||||
|
"course": self.name,
|
||||||
|
"mentor": member
|
||||||
|
})
|
||||||
|
doc.insert()
|
||||||
|
|
||||||
|
def get_mentors(self):
|
||||||
|
"""Returns the list of all mentors for this course.
|
||||||
|
"""
|
||||||
|
course_mentors = []
|
||||||
|
mentors = frappe.get_all("LMS Course Mentor Mapping", {"course": self.name}, ["mentor"])
|
||||||
|
for mentor in mentors:
|
||||||
|
member = frappe.get_doc("Community Member", mentor.mentor)
|
||||||
|
# TODO: change this to count query
|
||||||
|
member.batch_count = len(frappe.get_all("LMS Batch Membership", {"member": member.name, "member_type": "Mentor"}))
|
||||||
|
course_mentors.append(member)
|
||||||
|
return course_mentors
|
||||||
|
|||||||
@@ -3,8 +3,48 @@
|
|||||||
# See license.txt
|
# See license.txt
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
# import frappe
|
import frappe
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
class TestLMSCourse(unittest.TestCase):
|
class TestLMSCourse(unittest.TestCase):
|
||||||
pass
|
def setUp(self):
|
||||||
|
frappe.db.sql('delete from `tabLMS Course Mentor Mapping`')
|
||||||
|
frappe.db.sql('delete from `tabLMS Course`')
|
||||||
|
frappe.db.sql('delete from `tabCommunity Member`')
|
||||||
|
frappe.db.sql('delete from `tabUser` where email like "%@example.com"')
|
||||||
|
|
||||||
|
def new_course(self, title):
|
||||||
|
doc = frappe.get_doc({
|
||||||
|
"doctype": "LMS Course",
|
||||||
|
"title": title
|
||||||
|
})
|
||||||
|
doc.insert()
|
||||||
|
return doc
|
||||||
|
|
||||||
|
def new_user(self, name, email):
|
||||||
|
doc = frappe.get_doc(dict(
|
||||||
|
doctype='User',
|
||||||
|
email=email,
|
||||||
|
first_name=name))
|
||||||
|
doc.insert()
|
||||||
|
return doc
|
||||||
|
|
||||||
|
def test_new_course(self):
|
||||||
|
course = self.new_course("Test Course")
|
||||||
|
assert course.title == "Test Course"
|
||||||
|
assert course.slug == "test-course"
|
||||||
|
assert course.get_mentors() == []
|
||||||
|
|
||||||
|
def test_add_mentors(self):
|
||||||
|
course = self.new_course("Test Course")
|
||||||
|
assert course.get_mentors() == []
|
||||||
|
|
||||||
|
user = self.new_user("Tester", "tester@example.com")
|
||||||
|
course.add_mentor("tester@example.com")
|
||||||
|
|
||||||
|
mentors = course.get_mentors()
|
||||||
|
mentors_data = [dict(email=mentor.email, batch_count=mentor.batch_count) for mentor in mentors]
|
||||||
|
assert mentors_data == [{"email": "tester@example.com", "batch_count": 0}]
|
||||||
|
|
||||||
|
print(course)
|
||||||
|
doom
|
||||||
|
|||||||
Reference in New Issue
Block a user