diff --git a/community/lms/doctype/lms_course/lms_course.json b/community/lms/doctype/lms_course/lms_course.json index 5478d5c1..4a558f78 100644 --- a/community/lms/doctype/lms_course/lms_course.json +++ b/community/lms/doctype/lms_course/lms_course.json @@ -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", diff --git a/community/lms/doctype/lms_course/lms_course.py b/community/lms/doctype/lms_course/lms_course.py index 9882d82f..eaa33c90 100644 --- a/community/lms/doctype/lms_course/lms_course.py +++ b/community/lms/doctype/lms_course/lms_course.py @@ -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']) diff --git a/community/lms/doctype/lms_topic/lms_topic.json b/community/lms/doctype/lms_topic/lms_topic.json index 52d00479..81cc6477 100644 --- a/community/lms/doctype/lms_topic/lms_topic.json +++ b/community/lms/doctype/lms_topic/lms_topic.json @@ -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", diff --git a/community/lms/doctype/lms_topic/lms_topic.py b/community/lms/doctype/lms_topic/lms_topic.py index acf5fe4e..fec5e261 100644 --- a/community/lms/doctype/lms_topic/lms_topic.py +++ b/community/lms/doctype/lms_topic/lms_topic.py @@ -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 + + + diff --git a/community/www/courses/course.html b/community/www/courses/course.html index f592bff9..00f68acf 100644 --- a/community/www/courses/course.html +++ b/community/www/courses/course.html @@ -1,6 +1,5 @@ {% extends "templates/base.html" %} {% block title %}{{ 'Courses' }}{% endblock %} -{% from "www/courses/macros/card.html" import course_card, topic_card %} {% block head_include %} @@ -34,16 +33,16 @@ aria-selected="false">Discussions {% endif %} - +
{{topic.description}}
-