refactor: added nice urls for sketches.

- Sketches will be available at `/sketches/<sketch-id>`
This commit is contained in:
Anand Chitipothu
2021-04-06 18:36:07 +05:30
parent 06f7698e8f
commit a3788a0f39
4 changed files with 29 additions and 14 deletions

View File

@@ -132,7 +132,9 @@ install_regex_converter()
# Add all simple route rules here
primary_rules = [
{"from_route": "/sketches", "to_route": "sketches"},
{"from_route": "/sketches/<sketch>", "to_route": "sketches/sketch"},
{"from_route": "/courses", "to_route": "courses"},
{"from_route": "/courses/<course>", "to_route": "courses/course"},
{"from_route": "/courses/<course>/<topic>", "to_route": "courses/topic"},
{"from_route": "/courses/<course>/<topic>", "to_route": "courses/topic"}

View File

@@ -13,6 +13,14 @@ class LMSSketch(Document):
def get_owner_name(self):
return get_userinfo(self.owner)['full_name']
@property
def sketch_id(self):
"""Returns the numeric part of the name.
For example, the skech_id will be "123" for sketch with name "SKETCH-123".
"""
return self.name.replace("SKETCH-", "")
def get_livecode_url(self):
doc = frappe.get_cached_doc("LMS Settings")
return doc.livecode_url

View File

@@ -21,13 +21,13 @@
<div class="col mb-4">
<div class="card sketch-card" style="width: 200px;">
<div class="card-img-top">
<a href="/sketches/{{sketch.name}}">
<a href="/sketches/{{sketch.sketch_id}}">
{{ sketch.to_svg() }}
</a>
</div>
<div class="card-footer">
<div class="sketch-title">
<a href="sketches/{{sketch.name}}">{{sketch.title}}</a>
<a href="sketches/{{sketch.sketch_id}}">{{sketch.title}}</a>
</div>
<div class="sketch-author">
by {{sketch.get_owner_name()}}

View File

@@ -2,8 +2,19 @@ import frappe
def get_context(context):
context.no_cache = 1
course_name = get_queryparam("sketch", '/sketches')
context.sketch = get_sketch(course_name)
try:
sketch_id = frappe.form_dict["sketch"]
except KeyError:
context.template = "www/404.html"
return
sketch = get_sketch(sketch_id)
if not sketch:
context.template = "www/404.html"
return
context.sketch = sketch
context.livecode_url = get_livecode_url()
context.editable = is_editable(context.sketch, frappe.session.user)
@@ -19,15 +30,8 @@ def get_livecode_url():
doc = frappe.get_doc("LMS Settings")
return doc.livecode_url
def get_queryparam(name, redirect_when_not_found):
try:
return frappe.form_dict[name]
except KeyError:
frappe.local.flags.redirect_location = redirect_when_not_found
raise frappe.Redirect
def get_sketch(name):
if name == 'new':
def get_sketch(sketch_id):
if sketch_id == 'new':
sketch = frappe.new_doc('LMS Sketch')
sketch.name = "new"
sketch.title = "New Sketch"
@@ -35,7 +39,8 @@ def get_sketch(name):
return sketch
try:
name = "SKETCH-" + sketch_id
return frappe.get_doc('LMS Sketch', name)
except frappe.exceptions.DoesNotExistError:
raise frappe.NotFound
return