fix: miscellaneous

This commit is contained in:
pateljannat
2021-05-04 12:47:45 +05:30
parent 8933ca9ac9
commit e931ead270
12 changed files with 101 additions and 72 deletions

View File

@@ -42,7 +42,7 @@ class CommunityMember(Document):
return frappe.db.count(
'LMS Batch Membership', {
'member': self.name,
'member_role': 'Mentor'
'member_type': 'Mentor'
})
def __repr__(self):

View File

@@ -11,7 +11,7 @@ from frappe.utils.password import get_decrypted_password
class InviteRequest(Document):
def on_update(self):
if self.has_value_changed('status') and self.status == "Approved":
if self.has_value_changed("status") and self.status == "Approved":
self.send_email()
def create_user(self, password):
@@ -53,7 +53,7 @@ def create_invite_request(invite_email):
}).save(ignore_permissions=True)
return "OK"
except frappe.UniqueValidationError:
frappe.throw(_("Email {0} has already been used to request an invite").format(invite_email))
frappe.throw(_("Email {0} has already been used to request an invitation").format(invite_email))
@frappe.whitelist(allow_guest=True)
def update_invite(data):

View File

@@ -29,7 +29,7 @@ class LMSBatchMembership(Document):
frappe.throw(_("{0} is already a {1} of {2} course through {3} batch").format(member_name, membership.member_type, course, membership.batch))
@frappe.whitelist()
def create_membership(batch, course, member=None, member_type="Student", role="Member"):
def create_membership(batch, course=None, member=None, member_type="Student", role="Member"):
if not member:
member = frappe.db.get_value("Community Member", {"email": frappe.session.user}, "name")
frappe.get_doc({
@@ -39,5 +39,7 @@ def create_membership(batch, course, member=None, member_type="Student", role="M
"member_type": member_type,
"member": member
}).save(ignore_permissions=True)
course_slug = frappe.db.get_value("LMS Course", {"title": course}, ["slug"])
return course_slug
if course:
course_slug = frappe.db.get_value("LMS Course", {"title": course}, ["slug"])
return course_slug
return "OK"

View File

@@ -97,7 +97,7 @@ class LMSCourse(Document):
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, "member_type": "Mentor"}))
member.batch_count = len(frappe.get_all("LMS Batch Membership", {"member": member.name, "member_type": "Mentor"}))
course_mentors.append(member)
return course_mentors
@@ -108,9 +108,9 @@ class LMSCourse(Document):
return False
member = self.get_community_member(email)
return frappe.db.exists({
'doctype': 'LMS Course Mentor Mapping',
"doctype": "LMS Course Mentor Mapping",
"course": self.name,
"member": member
"mentor": member
})
def get_instructor(self):
@@ -131,15 +131,30 @@ class LMSCourse(Document):
memberships = frappe.db.get_all(
"LMS Batch Membership",
{"member": member},
["name"], as_dict=1)
["batch"])
batch_names = {m.batch for m in memberships}
return [b for b in batches if b.name in batch_names]
mentor_batches = [b for b in batches if b.name in batch_names]
return self.get_batch_mentors(mentor_batches)
def get_upcoming_batches(self):
now = frappe.utils.nowdate()
return find_all("LMS Batch",
batches = find_all("LMS Batch",
course=self.name,
start_date=[">", now])
batches = self.get_batch_mentors(batches)
return batches
def get_batch_mentors(self, batches):
for batch in batches:
batch.mentors = []
mentors = frappe.get_all(
"LMS Batch Membership",
{"batch": batch.name, "member_type": "Mentor"},
["member"])
for mentor in mentors:
member = frappe.db.get_value("Community Member", mentor.member, ["full_name", "photo", "abbr"], as_dict=1)
batch.mentors.append(member)
return batches
def find_all(doctype, order_by=None, **filters):
"""Queries the database for documents of a doctype matching given filters.

View File

@@ -287,6 +287,7 @@ img.profile-photo {
}
.message-section {
margin-left: 5%;
margin-left: 3%;
display: inline-block;
width: 95%;
}

View File

@@ -16,7 +16,7 @@
{% if batch.description %}
{{ BatchDetails(batch.description) }}
{% endif %}
{{ MentorsSection(mentors, True) }}
{{ MentorsSection(mentors, True, course.name) }}
</div>
{% endblock %}
@@ -43,4 +43,4 @@
{{ frappe.utils.md_to_html(description) }}
</div>
</div>
{% endmacro %}
{% endmacro %}

View File

@@ -11,13 +11,11 @@ def get_context(context):
context.instructor = get_instructor(context.course.owner)
context.batch = get_batch(context.batch_code)
context.mentors = get_mentors(context.batch.name)
print(context.mentors)
def get_mentors(batch):
mentors = []
memberships = frappe.get_all("LMS Batch Membership", {"batch": batch, "member_type": "Mentor"}, ["member"])
for membership in memberships:
member = frappe.db.get_value("Community Member", membership.member, ["name","full_name"], as_dict=True)
member.batch_count = len(frappe.get_all("LMS Batch Membership", {"member": member.name, "member_type": "Mentor"}))
member = frappe.get_doc("Community Member", membership.member)
mentors.append(member)
return mentors
return mentors

View File

@@ -10,7 +10,7 @@
<div class="container">
<div class="course-header">
<div class="course-type">course</div>
<h1>{{course.title}}</h1>
<h1 id="course-title" data-course="{{course.name}}">{{course.title}}</h1>
</div>
<div class="row">
@@ -28,7 +28,7 @@
</div>
<div class="sidebar">
{{ MentorsSection(course.get_mentors(), course.is_mentor(frappe.session.user)) }}
{{ MentorsSection(course.get_mentors(), course.is_mentor(frappe.session.user), course.name) }}
</div>
</div>
</div>
@@ -86,9 +86,9 @@
<div class="cta">
<div class="">
{% if can_manage %}
<button type="button">Manage</button>
<button >Manage</button>
{% else %}
<button type="button">Join this Batch</button>
<button class="join-batch" data-batch="{{ batch.name | urlencode }}">Join this Batch</button>
{% endif %}
</div>
</div>
@@ -123,11 +123,13 @@
{% macro BatchSectionForStudents(course, upcoming_batches) %}
<h2>Upcoming Batches</h2>
{% for batch in upcoming_batches %}
<div class="col-lg-4 col-md-6">
{{ RenderBatch(batch, can_manage=False) }}
<div class="row">
{% for batch in upcoming_batches %}
<div class="col-lg-4 col-md-6">
{{ RenderBatch(batch, can_manage=False) }}
</div>
{% endfor %}
</div>
{% endfor %}
{% endmacro %}
{% macro CourseOutline(course) %}

View File

@@ -3,18 +3,19 @@ frappe.ready(() => {
frappe.call({
'method': 'community.lms.doctype.lms_mentor_request.lms_mentor_request.has_requested',
'args': {
course: decodeURIComponent($(".course-title").attr("data-course")),
course: decodeURIComponent($("#course-title").attr("data-course")),
},
'callback': (data) => {
if (data.message) {
$(".mentor-request").addClass("hide");
$(".already-applied").removeClass("hide")
$("#mentor-request").addClass("hide");
$("#already-applied").removeClass("hide")
}
}
})
}
$(".apply-now").click((e) => {
$("#apply-now").click((e) => {
e.preventDefault();
if (frappe.session.user == "Guest") {
window.location.href = "/login";
return;
@@ -26,14 +27,15 @@ frappe.ready(() => {
},
"callback": (data) => {
if (data.message == "OK") {
$(".mentor-request").addClass("hide");
$(".already-applied").removeClass("hide")
$("#mentor-request").addClass("hide");
$("#already-applied").removeClass("hide")
}
}
})
})
$(".cancel-request").click((e) => {
$("#cancel-request").click((e) => {
e.preventDefault()
frappe.call({
"method": "community.lms.doctype.lms_mentor_request.lms_mentor_request.cancel_request",
"args": {
@@ -41,14 +43,15 @@ frappe.ready(() => {
},
"callback": (data) => {
if (data.message == "OK") {
$(".mentor-request").removeClass("hide");
$(".already-applied").addClass("hide")
$("#mentor-request").removeClass("hide");
$("#already-applied").addClass("hide")
}
}
})
})
$(".join-batch").click((e) => {
e.preventDefault()
if (frappe.session.user == "Guest") {
window.location.href = "/login";
return;
@@ -62,7 +65,6 @@ frappe.ready(() => {
"callback": (data) => {
if (data.message == "OK") {
frappe.msgprint(__("You are now a student of this course."))
$(".upcoming-batches").addClass("hide")
}
}
})

View File

@@ -1,38 +1,42 @@
import frappe
from ...lms.doctype.lms_sketch.lms_sketch import get_recent_sketches
from community.lms.models import Sketch
def get_context(context):
context.no_cache = 1
context.member = frappe.get_all("Community Member", {"email": frappe.session.user}, ["name", "email", "photo", "full_name", "abbr"])[0]
context.memberships = get_memberships(context.member.name)
context.courses = get_courses(context.memberships)
context.activity = get_activity(context.memberships)
context.sketches = list(filter(lambda x: x.owner == frappe.session.user, get_recent_sketches()))
context.no_cache = 1
if frappe.session.user == "Guest":
frappe.local.flags.redirect_location = "/login"
raise frappe.Redirect
context.member = frappe.get_all("Community Member", {"email": frappe.session.user}, ["name", "email", "photo", "full_name", "abbr"])[0]
context.memberships = get_memberships(context.member.name)
context.courses = get_courses(context.memberships)
context.activity = get_activity(context.memberships)
context.sketches = list(filter(lambda x: x.owner == frappe.session.user, Sketch.get_recent_sketches(owner=context.member.email)))
def get_memberships(member):
return frappe.get_all("LMS Batch Membership", {"member": member}, ["batch", "member_type", "creation"])
return frappe.get_all("LMS Batch Membership", {"member": member}, ["batch", "member_type", "creation"])
def get_courses(memberships):
courses = []
for membership in memberships:
course = frappe.db.get_value("LMS Batch", membership.batch, "course")
course_details = frappe.get_doc("LMS Course", course)
course_in_list = list(filter(lambda x: x.name == course_details.name, courses))
if not len(course_in_list):
course_details.description = course_details.description[0:100] + "..."
course_details.joining = membership.creation
if membership.member_type != "Student":
course_details.member_type = membership.member_type
courses.append(course_details)
return courses
courses = []
for membership in memberships:
course = frappe.db.get_value("LMS Batch", membership.batch, "course")
course_details = frappe.get_doc("LMS Course", course)
course_in_list = list(filter(lambda x: x.name == course_details.name, courses))
if not len(course_in_list):
course_details.description = course_details.description[0:100] + "..."
course_details.joining = membership.creation
if membership.member_type != "Student":
course_details.member_type = membership.member_type
courses.append(course_details)
return courses
def get_activity(memberships):
messages, courses = [], {}
batches = [x.batch for x in memberships]
for batch in batches:
courses[batch] = frappe.db.get_value("LMS Batch", batch, "course")
messages = frappe.get_all("LMS Message", {"batch": ["in", ",".join(batches)]}, ["message", "author", "creation", "batch"], order_by='creation desc')
for message in messages:
message.course = courses[message.batch]
message.profile, message.full_name, message.abbr = frappe.db.get_value("Community Member", message.author, ["photo", "full_name", "abbr"])
return messages
messages, courses = [], {}
batches = [x.batch for x in memberships]
for batch in batches:
courses[batch] = frappe.db.get_value("LMS Batch", batch, "course")
messages = frappe.get_all("LMS Message", {"batch": ["in", ",".join(batches)]}, ["message", "author", "creation", "batch"], order_by='creation desc')
for message in messages:
message.course = courses[message.batch]
message.profile, message.full_name, message.abbr = frappe.db.get_value("Community Member", message.author, ["photo", "full_name", "abbr"])
return messages

View File

@@ -6,7 +6,7 @@
</div>
{% endmacro %}
{% macro MentorsSection(mentors, is_mentor) %}
{% macro MentorsSection(mentors, is_mentor, course_name) %}
<h3>Mentors</h3>
{% for m in mentors %}
<div class="instructor">
@@ -15,10 +15,15 @@
</div>
{% endfor %}
{% if not is_mentor %}
<div class="notice">
<div id="mentor-request" class="notice">
Interested to become a mentor?
<div><a href="#">Apply Now!</a></div>
<div><a id="apply-now" data-course="{{course_name | urlencode}}" href="">Apply Now!</a></div>
</div>
<div id="already-applied" class="notice hide">
You've applied to become a mentor for this course. Your request is currently under review.
If you are not any more interested to mentor this course, you can <a id="cancel-request" data-course="{{course_name | urlencode}}" href="">cancel your application</a>.
</div>
{% endif %}
{% endmacro %}
@@ -26,7 +31,7 @@
{% macro BatchHearder(course_name, member_count) %}
<div class="border p-3">
<h3>{{course_name}}</h3>
<div class="text-muted">{{member_count}} members</div>
<h3>{{course_name}}</h3>
<div class="text-muted">{{member_count}} members</div>
</div>
{% endmacro %}

View File

@@ -8,7 +8,7 @@ def get_context(context):
if not context.member:
context.template = "www/404.html"
else:
context.sketches = Sketch.get_recent_sketches(context.member.email)
context.sketches = Sketch.get_recent_sketches(owner=context.member.email)
def get_member(username):
try: