refactor: removed the slug using the course name as part of url

This commit is contained in:
Anand Chitipothu
2021-05-06 13:25:24 +05:30
parent 88457a82ac
commit 5a70687067
9 changed files with 21 additions and 22 deletions

View File

@@ -40,6 +40,6 @@ def create_membership(batch, course=None, member=None, member_type="Student", ro
"member": member
}).save(ignore_permissions=True)
if course:
course_slug = frappe.db.get_value("LMS Course", {"title": course}, ["slug"])
course_slug = frappe.db.get_value("LMS Course", {"title": course}, ["name"])
return course_slug
return "OK"

View File

@@ -2,7 +2,6 @@
"actions": [],
"allow_guest_to_view": 1,
"allow_rename": 1,
"autoname": "field:slug",
"creation": "2021-03-01 16:49:33.622422",
"doctype": "DocType",
"editable_grid": 1,
@@ -89,7 +88,7 @@
"link_fieldname": "course"
}
],
"modified": "2021-05-06 06:06:08.760597",
"modified": "2021-05-06 11:15:45.728976",
"modified_by": "Administrator",
"module": "LMS",
"name": "LMS Course",

View File

@@ -10,10 +10,14 @@ from community.query import find, find_all
class LMSCourse(Document):
@staticmethod
def find(slug):
"""Returns the course with specified slug.
def find(name):
"""Returns the course with specified name.
"""
return find("LMS Course", is_published=True, slug=slug)
return find("LMS Course", is_published=True, name=name)
def autoname(self):
if not self.name:
self.name = self.generate_slug(title=self.title)
@staticmethod
def find_all():
@@ -21,10 +25,6 @@ class LMSCourse(Document):
"""
return find_all("LMS Course", is_published=True)
def before_save(self):
if not self.slug:
self.slug = self.generate_slug(title=self.title)
def generate_slug(self, title):
result = frappe.get_all(
'LMS Course',
@@ -33,7 +33,7 @@ class LMSCourse(Document):
return slugify(title, used_slugs=slugs)
def __repr__(self):
return f"<Course#{self.name} {self.slug}>"
return f"<Course#{self.name}>"
def get_topic(self, slug):
"""Returns the topic with given slug in this course as a Document.

View File

@@ -24,7 +24,7 @@ class TestLMSCourse(unittest.TestCase):
def test_new_course(self):
course = self.new_course("Test Course")
assert course.title == "Test Course"
assert course.slug == "test-course"
assert course.name == "test-course"
assert course.get_mentors() == []
def test_find_all(self):
@@ -41,7 +41,7 @@ class TestLMSCourse(unittest.TestCase):
# now we should find one course
courses = LMSCourse.find_all()
assert [c.slug for c in courses] == [course.slug]
assert [c.name for c in courses] == [course.name]
# disabled this test as it is failing
def _test_add_mentors(self):

View File

@@ -1,6 +1,6 @@
<div class="course-teaser">
<div class="course-body">
<h3 class="course-title"><a href="/courses/{{ course.slug }}">{{ course.title }}</a></h3>
<h3 class="course-title"><a href="/courses/{{ course.name }}">{{ course.title }}</a></h3>
<div class="course-intro">
{{ course.short_introduction or "" }}
</div>

View File

@@ -7,12 +7,12 @@ def get_context(context):
context.no_cache = 1
try:
course_slug = frappe.form_dict["course"]
course_name = frappe.form_dict["course"]
except KeyError:
frappe.local.flags.redirect_location = "/courses"
raise frappe.Redirect
course = Course.find(course_slug)
course = Course.find(course_name)
if course is None:
frappe.local.flags.redirect_location = "/courses"
raise frappe.Redirect

View File

@@ -26,11 +26,11 @@
{% macro course_card(course) %}
<div class="card mb-5 w-100">
<div class="card-body">
<h5 class="card-title"><a href="/courses/{{course.slug}}">{{course.title}}</a></h5>
<h5 class="card-title"><a href="/courses/{{course.name}}">{{course.title}}</a></h5>
{% if course.description %}
<p class="card-text">{{ frappe.utils.md_to_html(course.description[:250]) }}</p>
{% endif %}
<a href="/courses/{{course.slug}}" class="card-link">See more &rarr;</a>
<a href="/courses/{{course.name}}" class="card-link">See more &rarr;</a>
</div>
</div>
{% endmacro %}

View File

@@ -31,9 +31,9 @@ def redirect_if_not_a_member(course,batch_code):
frappe.local.flags.redirect_location = "/courses/" + course
raise frappe.Redirect
def get_course(slug):
def get_course(name):
try:
return frappe.get_doc("LMS Course", {"slug": slug})
return frappe.get_doc("LMS Course", {"name": name})
except frappe.DoesNotExistError:
return

View File

@@ -182,8 +182,8 @@
{% for course in courses %}
<div class="dashboard__course">
<div class="dashboard__courseHeader">
<a class="text-decoration-none" target="_blank" href="/courses/{{course.slug}}">
<h5 class="w-75">{{ course.name }}</h5>
<a class="text-decoration-none" target="_blank" href="/courses/{{course.name}}">
<h5 class="w-75">{{ course.title }}</h5>
</a>
{% if course.member_type %}
<div class="dashboard__badge">