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 "member": member
}).save(ignore_permissions=True) }).save(ignore_permissions=True)
if course: 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 course_slug
return "OK" return "OK"

View File

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

View File

@@ -10,10 +10,14 @@ from community.query import find, find_all
class LMSCourse(Document): class LMSCourse(Document):
@staticmethod @staticmethod
def find(slug): def find(name):
"""Returns the course with specified slug. """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 @staticmethod
def find_all(): def find_all():
@@ -21,10 +25,6 @@ class LMSCourse(Document):
""" """
return find_all("LMS Course", is_published=True) 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): def generate_slug(self, title):
result = frappe.get_all( result = frappe.get_all(
'LMS Course', 'LMS Course',
@@ -33,7 +33,7 @@ class LMSCourse(Document):
return slugify(title, used_slugs=slugs) return slugify(title, used_slugs=slugs)
def __repr__(self): def __repr__(self):
return f"<Course#{self.name} {self.slug}>" return f"<Course#{self.name}>"
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.

View File

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

View File

@@ -1,6 +1,6 @@
<div class="course-teaser"> <div class="course-teaser">
<div class="course-body"> <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"> <div class="course-intro">
{{ course.short_introduction or "" }} {{ course.short_introduction or "" }}
</div> </div>

View File

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

View File

@@ -26,11 +26,11 @@
{% macro course_card(course) %} {% macro course_card(course) %}
<div class="card mb-5 w-100"> <div class="card mb-5 w-100">
<div class="card-body"> <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 %} {% if course.description %}
<p class="card-text">{{ frappe.utils.md_to_html(course.description[:250]) }}</p> <p class="card-text">{{ frappe.utils.md_to_html(course.description[:250]) }}</p>
{% endif %} {% 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>
</div> </div>
{% endmacro %} {% endmacro %}

View File

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

View File

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