refactor: added slugs to course and topics to make nice urls
- the slug is autogenerated from the title - the slug of a topic is unique among all the topics of that course
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
"engine": "InnoDB",
|
||||
"field_order": [
|
||||
"title",
|
||||
"slug",
|
||||
"description",
|
||||
"section_break_3",
|
||||
"is_published",
|
||||
@@ -47,12 +48,20 @@
|
||||
{
|
||||
"fieldname": "column_break_5",
|
||||
"fieldtype": "Column Break"
|
||||
},
|
||||
{
|
||||
"description": "The slug of the course. Autogenerated from the title if not specified.",
|
||||
"fieldname": "slug",
|
||||
"fieldtype": "Data",
|
||||
"in_list_view": 1,
|
||||
"label": "Slug",
|
||||
"unique": 1
|
||||
}
|
||||
],
|
||||
"index_web_pages_for_search": 1,
|
||||
"is_published_field": "is_published",
|
||||
"links": [],
|
||||
"modified": "2021-03-19 15:44:47.411705",
|
||||
"modified": "2021-04-06 15:33:08.870313",
|
||||
"modified_by": "Administrator",
|
||||
"module": "LMS",
|
||||
"name": "LMS Course",
|
||||
@@ -71,7 +80,7 @@
|
||||
"write": 1
|
||||
}
|
||||
],
|
||||
"search_fields": "title",
|
||||
"search_fields": "slug",
|
||||
"sort_field": "creation",
|
||||
"sort_order": "DESC",
|
||||
"title_field": "title",
|
||||
|
||||
@@ -3,8 +3,29 @@
|
||||
# For license information, please see license.txt
|
||||
|
||||
from __future__ import unicode_literals
|
||||
# import frappe
|
||||
import frappe
|
||||
from frappe.model.document import Document
|
||||
from ...utils import slugify
|
||||
|
||||
class LMSCourse(Document):
|
||||
pass
|
||||
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',
|
||||
fields=['slug'])
|
||||
slugs = set([row['slug'] for row in result])
|
||||
return slugify(title, used_slugs=slugs)
|
||||
|
||||
def get_topic(self, slug):
|
||||
"""Returns the topic with given slug in this course as a Document.
|
||||
"""
|
||||
result = frappe.get_all(
|
||||
"LMS Topic",
|
||||
filters={"course": self.name, "slug": slug})
|
||||
|
||||
if result:
|
||||
row = result[0]
|
||||
return frappe.get_doc('LMS Topic', row['name'])
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
{
|
||||
"actions": [],
|
||||
"allow_guest_to_view": 1,
|
||||
"autoname": "format:{title}",
|
||||
"creation": "2021-03-02 07:20:41.686573",
|
||||
"doctype": "DocType",
|
||||
"editable_grid": 1,
|
||||
"engine": "InnoDB",
|
||||
"field_order": [
|
||||
"course",
|
||||
"preview",
|
||||
"title",
|
||||
"slug",
|
||||
"preview",
|
||||
"description",
|
||||
"order",
|
||||
"sections"
|
||||
@@ -50,11 +50,17 @@
|
||||
"fieldtype": "Table",
|
||||
"label": "Sections",
|
||||
"options": "LMS Section"
|
||||
},
|
||||
{
|
||||
"description": "The slug of the topic. Autogenerated from the title if not specified.",
|
||||
"fieldname": "slug",
|
||||
"fieldtype": "Data",
|
||||
"label": "Slug"
|
||||
}
|
||||
],
|
||||
"index_web_pages_for_search": 1,
|
||||
"links": [],
|
||||
"modified": "2021-03-05 17:08:55.580189",
|
||||
"modified": "2021-04-06 14:12:48.514062",
|
||||
"modified_by": "Administrator",
|
||||
"module": "LMS",
|
||||
"name": "LMS Topic",
|
||||
|
||||
@@ -6,12 +6,28 @@ from __future__ import unicode_literals
|
||||
import frappe
|
||||
from frappe.model.document import Document
|
||||
from .section_parser import SectionParser
|
||||
from ...utils import slugify
|
||||
|
||||
class LMSTopic(Document):
|
||||
def before_save(self):
|
||||
sections = SectionParser().parse(self.description)
|
||||
course = self.get_course()
|
||||
if not self.slug:
|
||||
self.slug = self.generate_slug(title=self.title)
|
||||
|
||||
sections = SectionParser().parse(self.description or "")
|
||||
self.sections = [self.make_lms_section(i, s) for i, s in enumerate(sections)]
|
||||
|
||||
def get_course(self):
|
||||
return frappe.get_doc("LMS Course", self.course)
|
||||
|
||||
def generate_slug(self, title):
|
||||
result = frappe.get_all(
|
||||
'LMS Topic',
|
||||
filters={'course': self.course},
|
||||
fields=['slug'])
|
||||
slugs = set([row['slug'] for row in result])
|
||||
return slugify(title, used_slugs=slugs)
|
||||
|
||||
def get_sections(self):
|
||||
return sorted(self.sections, key=lambda s: s.index)
|
||||
|
||||
@@ -22,3 +38,6 @@ class LMSTopic(Document):
|
||||
s.contents = section.contents
|
||||
s.index = index
|
||||
return s
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user