Commit 81c97712 by Ibrahim Awwal

Doing slugging in a less silly way. Requires comment service to return slug as a…

Doing slugging in a less silly way. Requires comment service to return slug as a field and not as the id.
parent e42850ca
......@@ -353,7 +353,7 @@ def safe_content(content):
'updated_at', 'depth', 'type', 'commentable_id', 'comments_count',
'at_position_list', 'children', 'highlighted_title', 'highlighted_body',
'courseware_title', 'courseware_url', 'tags', 'unread_comments_count',
'read',
'read', 'slug',
]
if (content.get('anonymous') is False) and (content.get('anonymous_to_peers') is False):
......
......@@ -10,7 +10,7 @@ class Thread(models.Model):
'closed', 'tags', 'votes', 'commentable_id', 'username', 'user_id',
'created_at', 'updated_at', 'comments_count', 'unread_comments_count',
'at_position_list', 'children', 'type', 'highlighted_title',
'highlighted_body', 'endorsed', 'read'
'highlighted_body', 'endorsed', 'read', 'slug',
]
updatable_fields = [
......@@ -66,7 +66,7 @@ class Thread(models.Model):
def _retrieve(self, *args, **kwargs):
url = self.url(action='get', params=self.attributes)
request_params = {
request_params = {
'recursive': kwargs.get('recursive'),
'user_id': kwargs.get('user_id'),
'mark_as_read': kwargs.get('mark_as_read', True),
......
......@@ -5,12 +5,31 @@ if Backbone?
initialize: (models, options={})->
@pages = options['pages'] || 1
@current_page = 1
@slugToId = {}
@bind "add", (item) =>
item.discussion = @
@updateSlugMap(item)
@comparator = @sortByDateRecentFirst
@on "thread:remove", (thread) =>
@remove(thread)
_.each models, @updateSlugMap
@bind "reset", @onReset
onReset: (new_collection)=>
new_collection.each @updateSlugMap
updateSlugMap: (thread)=>
# Don't try to update if thread doesn't have slug
# Thread could either be a raw JSON object or a Thread model...
if not(thread.slug) and not (thread.get and thread.get('slug'))
return
slug = thread.slug or thread.get('slug')
@slugToId[slug] = thread.id
getBySlug: (slug) =>
@get(@slugToId[slug])
find: (id) ->
_.first @where(id: id)
......@@ -18,8 +37,7 @@ if Backbone?
@current_page < @pages
addThread: (thread, options) ->
# TODO: Check for existing thread with same ID in a faster way
if not @find(thread.id)
if not @get(thread.id)
options ||= {}
model = new Thread thread
@add model
......
......@@ -26,7 +26,7 @@ if Backbone?
@nav.setActiveThread(@thread.get("id"))
showThread: (forum_name, thread_id) ->
@thread = @discussion.get(thread_id)
@thread = @discussion.get(thread_id) or @discussion.getBySlug(thread_id)
@thread.set("unread_comments_count", 0)
@thread.set("read", true)
@setActiveThread()
......@@ -44,7 +44,7 @@ if Backbone?
navigateToThread: (thread_id) =>
thread = @discussion.get(thread_id)
@navigate("#{thread.get("commentable_id")}/threads/#{thread_id}", trigger: true)
@navigate("#{thread.get("commentable_id")}/threads/#{(thread.get('slug') or thread_id)}", trigger: true)
navigateToAllThreads: =>
@navigate("", trigger: true)
......
......@@ -128,7 +128,7 @@
</script>
<script type="text/template" id="thread-list-item-template">
<a href="${'<%- id %>'}" data-id="${'<%- id %>'}">
<a href="${'<%- slug %>'}" data-id="${'<%- id %>'}">
<span class="title">${"<%- title %>"}</span>
${"<% if (unread_comments_count > 0) { %>"}
<span class="comments-count unread" data-tooltip="${"<%- unread_comments_count %>"} new comment${"<%- unread_comments_count > 1 ? 's' : '' %>"}">${"<%- comments_count %>"}</span>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment