Skip to content

Commit

Permalink
Merge pull request #241 from jayoshih/serializers
Browse files Browse the repository at this point in the history
Optimizations
  • Loading branch information
jamalex committed May 2, 2017
2 parents 92700b6 + 97e2015 commit 007d25c
Show file tree
Hide file tree
Showing 20 changed files with 248 additions and 260 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.12 on 2017-04-27 21:42
from __future__ import unicode_literals

import contentcuration.models
from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('contentcuration', '0066_auto_20170412_0015'),
]

operations = [
migrations.AlterField(
model_name='contentnode',
name='original_source_node_id',
field=contentcuration.models.UUIDField(db_index=True, editable=False, max_length=32, null=True),
),
]
21 changes: 15 additions & 6 deletions contentcuration/contentcuration/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import json
from django.conf import settings
from django.contrib import admin
from django.core.cache import cache
from django.core.files.storage import FileSystemStorage
from django.db import IntegrityError, connections, models, connection
from django.db.models import Q, Sum, Max, Count, Case, When, IntegerField
Expand Down Expand Up @@ -345,7 +346,7 @@ class ContentNode(MPTTModel, models.Model):
# TODO: disallow nulls once existing models have been set
original_channel_id = UUIDField(primary_key=False, editable=False, null=True, db_index=True) # Original channel copied from
source_channel_id = UUIDField(primary_key=False, editable=False, null=True) # Immediate channel copied from
original_source_node_id = UUIDField(primary_key=False, editable=False, null=True) # Original node_id of node copied from (TODO: original_node_id clashes with original_node field - temporary)
original_source_node_id = UUIDField(primary_key=False, editable=False, null=True, db_index=True) # Original node_id of node copied from (TODO: original_node_id clashes with original_node field - temporary)
source_node_id = UUIDField(primary_key=False, editable=False, null=True) # Immediate node_id of node copied from

# Fields specific to content generated by Ricecooker
Expand Down Expand Up @@ -377,12 +378,20 @@ class ContentNode(MPTTModel, models.Model):
objects = TreeManager()

def get_original_node(self):

original_node = self.original_node or self
if self.original_channel_id and self.original_source_node_id:
original_channel = Channel.objects.get(pk=self.original_channel_id)
return original_channel.main_tree.get_descendants().filter(node_id=self.original_source_node_id).first() or self

return self.original_node or self
original_channel = Channel.objects.get(pk=self.original_channel_id).prefetch_related("main_tree")
original_node = ContentNode.objects.filter(tree_id=original_channel.main_tree.tree_id, node_id=self.original_source_node_id).first() or self
return original_node

def get_associated_presets(self):
key = "associated_presets_{}".format(self.kind_id)
cached_data = cache.get(key)
if cached_data:
return cached_data
presets = FormatPreset.objects.filter(kind=self.kind).values()
cache.set(key, presets, None)
return presets

def get_channel(self):
root = self.get_root()
Expand Down
197 changes: 94 additions & 103 deletions contentcuration/contentcuration/serializers.py

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
{{#if isempty}}
<em class="annotation">(empty)</em>
{{else}}
<em class="annotation">{{#format_count "Resource" node.metadata.resource_count}}{{/format_count}}</em>
<span id="menu_toggle_{{node.id}}" class="toggler glyphicon glyphicon-menu-right"></span>
{{/if}}
{{else}}
<em class="annotation">({{#format_file_size node.metadata.resource_size}}{{/format_file_size}})</em>
{{/if}}
</div>

{{#if isfolder}}
<div id="export_item_{{node.id}}_sub" class="subdirectory"> </div>
{{/if}}
{{/if}}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ var ExportModalView = BaseViews.BaseModalView.extend({
});

var self = this;
this.retrieve_nodes(this.model.get('children')).then(function(collection){
var size = collection.reduce(function(size, node){ return size + node.get('metadata').resource_size; }, 0);
this.model.calculate_size().then(function(size){
self.$("#export_size").text("(" + stringHelper.format_size(size) + ")");
});
},
Expand Down Expand Up @@ -62,7 +61,7 @@ var ExportListView = BaseViews.BaseListView.extend({
this.$el.html(this.template({id: this.model.get("id")}));
var self = this;
this.fetch_model(this.model).then(function(fetched){
self.collection.get_all_fetch(fetched.get("children")).then(function(fetchedCollection){
self.collection.get_all_fetch_simplified(fetched.get("children")).then(function(fetchedCollection){
self.load_content(fetchedCollection);
});
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
<div class="import_item_title truncate">{{node.title}}</div>
{{#if isfolder}}
{{#if node.children}}
{{#if isfolder}}
{{#unless is_channel}}<em class="import_metadata">{{#format_count "Resource" node.metadata.resource_count}}{{/format_count}}</em>{{/unless}}
{{else}}
<em class="import_metadata">{{#format_file_size node.metadata.resource_size}}{{/format_file_size}}</em>
{{/if}}
<div id="menu_toggle_{{node.id}}" class="toggler glyphicon glyphicon-triangle-top"></div>
{{!-- {{#unless is_channel}}<em class="import_metadata">{{#format_file_size node.metadata.resource_size}}{{/format_file_size}}</em>{{/unless}} --}}
{{else}}
<em class="import_metadata">(empty)</em>
{{/if}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,16 @@ var ImportView = BaseViews.BaseListView.extend({
}else{
totalCount += entry.get("metadata").total_count;
}

});
var data = this.importList.get_metadata();
totalCount = totalCount - data.count;

this.$("#import_file_count").html(totalCount + " Topic" + ((totalCount == 1)? ", " : "s, ") + data.count + " Resource" + ((data.count == 1)? " " : "s ") + stringHelper.format_size(data.size));
this.$("#import_file_count").html(totalCount + " Topic" + ((totalCount == 1)? ", " : "s, ") + data.count + " Resource" + ((data.count == 1)? "" : "s"));
var self = this;
this.$("#import_file_size").html("Calculating...")
collection.calculate_size().then(function(size){
self.$("#import_file_size").html(stringHelper.format_size(size));
});
},
import_content:function(){
var self = this;
Expand Down Expand Up @@ -172,7 +176,6 @@ var ImportList = BaseViews.BaseListView.extend({
this.metadata = {"count" : 0, "size":0};
this.views.forEach(function(entry){
self.metadata.count += entry.metadata.count;
self.metadata.size += entry.metadata.size;
});
return this.metadata;
}
Expand Down
87 changes: 52 additions & 35 deletions contentcuration/contentcuration/static/js/edit_channel/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,11 @@ var InvitationCollection = BaseCollection.extend({
});

/**** CHANNEL AND CONTENT MODELS ****/
function fetch_nodes_by_ids(ids){
var self = this;
function fetch_nodes(ids, url){
return new Promise(function(resolve, reject){
$.ajax({
method:"POST",
url: window.Urls.get_nodes_by_ids(),
url: url,
data: JSON.stringify(ids),
error: reject,
success: function(data) {
Expand All @@ -140,22 +139,10 @@ function fetch_nodes_by_ids(ids){
});
});
}
function fetch_nodes_by_ids_simplified(ids){
var self = this;
return new Promise(function(resolve, reject){
$.ajax({
method:"POST",
url: window.Urls.get_nodes_by_ids_simplified(),
data: JSON.stringify(ids),
error: reject,
success: function(data) {
resolve(new ContentNodeCollection(JSON.parse(data)));
}
});
});
function fetch_nodes_by_ids(ids){
return fetch_nodes(ids, window.Urls.get_nodes_by_ids());
}


var ContentNodeModel = BaseModel.extend({
root_list:"contentnode-list",
model_name:"ContentNodeModel",
Expand Down Expand Up @@ -214,6 +201,21 @@ var ContentNodeModel = BaseModel.extend({
data['randomize'] = (data['randomize'] !== undefined)? data['randomize'] : window.preferences.auto_randomize_questions;
this.set('extra_fields', data);
}
},
calculate_size: function(){
var self = this;
var promise = new Promise(function(resolve, reject){
$.ajax({
method:"POST",
url: window.Urls.get_total_size(),
data: JSON.stringify([self.id]),
error:reject,
success: function(data) {
resolve(JSON.parse(data).size);
}
});
});
return promise;
}
});

Expand Down Expand Up @@ -248,28 +250,47 @@ var ContentNodeCollection = BaseCollection.extend({
});
});
});
return promise;
},
get_all_fetch: function(ids, force_fetch){
force_fetch = (force_fetch)? true : false;
var self = this;
calculate_size: function(){
var self = this;
return new Promise(function(resolve, reject){
var idlists = _.partition(ids, function(id){return force_fetch || !self.get({'id': id});});
var returnCollection = new ContentNodeCollection(self.filter(function(n){ return idlists[1].indexOf(n.id) >= 0; }))
fetch_nodes_by_ids(idlists[0]).then(function(fetched){
returnCollection.add(fetched.toJSON());
resolve(returnCollection);
});
$.ajax({
method:"POST",
url: window.Urls.get_total_size(),
data: JSON.stringify(self.pluck('id')),
success: function(data) {
resolve(JSON.parse(data).size);
},
error:reject
});
});
},
has_all_data: function(){
return this.every(function(node){
return _.every(node.get('files'), function(file){
return typeof file == 'object';
});
});
},
get_all_fetch: function(ids, force_fetch){
return this.get_fetch_nodes(ids, window.Urls.get_nodes_by_ids(), force_fetch);
},
get_all_fetch_simplified: function(ids, force_fetch){
force_fetch = (force_fetch)? true : false;
return this.get_fetch_nodes(ids, window.Urls.get_nodes_by_ids_simplified(), force_fetch);
},
fetch_nodes_by_ids_complete: function(ids, force_fetch){
return this.get_fetch_nodes(ids, window.Urls.get_nodes_by_ids_complete(), force_fetch);
},
get_fetch_nodes: function(ids, url, force_fetch){
force_fetch = (force_fetch)? true : false;
var self = this;
return new Promise(function(resolve, reject){
var idlists = _.partition(ids, function(id){return force_fetch || !self.get({'id': id});});
var returnCollection = new ContentNodeCollection(self.filter(function(n){ return idlists[1].indexOf(n.id) >= 0; }))
fetch_nodes_by_ids_simplified(idlists[0]).then(function(fetched){
fetch_nodes(idlists[0], url).then(function(fetched){
returnCollection.add(fetched.toJSON());
self.add(fetched.toJSON());
self.sort();
resolve(returnCollection);
});
});
Expand All @@ -283,7 +304,7 @@ var ContentNodeCollection = BaseCollection.extend({
},
duplicate:function(target_parent){
var self = this;
var promise = new Promise(function(resolve, reject){
return new Promise(function(resolve, reject){
var sort_order =(target_parent) ? target_parent.get("metadata").max_sort_order + 1 : 1;
var parent_id = target_parent.get("id");

Expand All @@ -297,15 +318,11 @@ var ContentNodeCollection = BaseCollection.extend({
url: window.Urls.duplicate_nodes(),
data: JSON.stringify(data),
success: function(data) {
copied_list = JSON.parse(data).node_ids.split(" ");
self.get_all_fetch(copied_list).then(function(fetched){
resolve(fetched);
});
resolve(new ContentNodeCollection(JSON.parse(data)));
},
error:reject
});
});
return promise;
},
move:function(target_parent, max_order, min_order){
var self = this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ var MoveView = BaseViews.BaseListView.extend({
clipboard_node.set({'title': 'My Clipboard'});
fetched.add(clipboard_node);

// Render list
this.targetList = new MoveList({
model: null,
el: $("#target_list_area"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ var PreviewView = BaseViews.BaseView.extend({
checksum:this.current_preview.checksum,
subtitles : this.get_subtitles()
}));
if(force_load && this.current_preview.recommended_kind === "video"){
if(force_load && this.model.get('kind') === "video"){
$("#preview_window video").load();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
{{#if time}}&nbsp;<span class="glyphicon glyphicon-time" aria-hidden="true"></span>{{time}}{{/if}}
{{#if isfolder}}&nbsp;{{#format_count "Resource" node.metadata.resource_count}}{{/format_count}}{{/if}}
&nbsp;
{{#if isexercise}}
<span class="glyphicon glyphicon-check" aria-hidden="true"></span>
{{#format_count "Question" num_questions}}{{/format_count}}
{{else}}
<span class="glyphicon glyphicon-file" aria-hidden="true"></span>
{{#format_file_size node.metadata.resource_size}}{{/format_file_size}}
{{/if}}
{{#unless isfolder}}
{{#if isexercise}}
<span class="glyphicon glyphicon-check" aria-hidden="true"></span>
{{#format_count "Question" node.metadata.resource_count}}{{/format_count}}
{{else}}
<span class="glyphicon glyphicon-file" aria-hidden="true"></span>
{{#format_file_size node.metadata.resource_size}}{{/format_file_size}}
{{/if}}
{{/unless}}
&nbsp;
{{#if node.changed}}
<em class="unpublished">{{#if node.published}}Updated{{else}}New{{/if}}</em>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,7 @@ var ContentItem = BaseViews.BaseWorkspaceListNodeItemView.extend({
checked: this.checked,
isexercise: this.model.get("kind") === "exercise",
description_first: description[0],
description_overflow: description[1],
num_questions: _.where(this.model.get('assessment_items'), {'deleted': false}).length
description_overflow: description[1]
}));
this.handle_checked();
if(this.isSelected){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<div class="metadata_panel">
<div id="metadata_edit_details" class="input_area tab_panel">
<div class="section pull-left edit_details_section">
<div id="edit_details_wrapper"></div>
<div id="edit_details_wrapper"><div id="metadata_placeholder" class="container-fluid text-center">Loading content...</div></div>
</div>
</div>
<div id="metadata_questions" class="container-fluid tab_panel"></div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ var EditMetadataView = BaseViews.BaseEditableListView.extend({
_.bindAll(this, 'render_details', 'render_preview', 'render_questions', 'enable_submit', 'disable_submit',
'save_and_keep_open', 'save_nodes', 'save_and_finish','process_updated_collection', 'close_upload', 'copy_items');
this.bind_edit_functions();
this.collection = options.collection;
this.new_content = options.new_content;
this.new_exercise = options.new_exercise;
this.onsave = options.onsave;
Expand All @@ -80,10 +79,15 @@ var EditMetadataView = BaseViews.BaseEditableListView.extend({
},
render: function() {
this.$el.html(this.template({allow_edit: this.allow_edit}));
this.load_list();
if(this.collection.length > 1){
this.load_editor(this.edit_list.selected_items);
}

var self = this;
this.collection.fetch_nodes_by_ids_complete(this.collection.pluck('id'), !this.collection.has_all_data()).then(function(fetched){
self.collection.reset(fetched.toJSON());
self.load_list();
if(self.collection.length > 1){
self.load_editor(self.edit_list.selected_items);
}
});
},
render_details:function(){
this.switchPanel("details");
Expand Down
Loading

0 comments on commit 007d25c

Please sign in to comment.