Skip to content

Commit

Permalink
create code to submit new lessons from admin page, refs #158
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiomontefuscolo committed Nov 11, 2013
1 parent 662f760 commit 4a18580
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 20 deletions.
2 changes: 1 addition & 1 deletion core/templates/admin/_base.html
Expand Up @@ -555,7 +555,7 @@ <h3 class="top">{{selectedUnit().title}}</h3>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal" ng-click="selectedLesson.restore()">Cancel</button>
<button type="button" class="btn btn-primary" data-dismiss="modal" ng-click="selectedLesson.$update()">Save changes</button>
<button type="button" class="btn btn-primary" data-dismiss="modal" ng-click="saveLesson()">Save changes</button>
</div>
</div>
</div>
Expand Down
3 changes: 2 additions & 1 deletion lesson/serializers.py
Expand Up @@ -49,6 +49,7 @@ class Meta:


class LessonSerializer(serializers.HyperlinkedModelSerializer):
course = serializers.SlugRelatedField(slug_field='slug')
units = UnitSerializer(many=True, allow_add_remove=True)
url = serializers.HyperlinkedIdentityField(
view_name='lesson',
Expand All @@ -57,4 +58,4 @@ class LessonSerializer(serializers.HyperlinkedModelSerializer):

class Meta:
model = Lesson
fields = ('id', 'course', 'slug', 'desc', 'notes', 'name', 'url', 'published', 'units',)
fields = ('id', 'course', 'desc', 'name', 'notes', 'position', 'published', 'slug', 'units', 'url',)
74 changes: 56 additions & 18 deletions static/js/admin/admin.js
Expand Up @@ -12,15 +12,14 @@
/^https?:\/\/(www\.)?youtube\.com\/.*/,
'data:text/html, <html style="background: white">'
]);
window.sce = $sceDelegateProvider;
}
]);

app.filter('markdown', ['$window', function($window) {
app.filter('markdown', function() {
return function(text) {
return text ? Markdown.getSanitizingConverter().makeHtml(text) : "";
};
}]);
});

app.directive('contenteditable', function(){
return {
Expand Down Expand Up @@ -128,26 +127,34 @@
}
]);

app.controller('LessonList',['$scope', '$rootScope', 'LessonListFactory', '$http',
function($scope, $rootScope, LessonListFactory, $http){
app.controller('LessonList',['$scope', '$rootScope', 'LessonListFactory', 'Lesson',
function($scope, $rootScope, LessonListFactory, Lesson){

$scope.newLesson = function() {
var newLesson = new Lesson({'units': []});
newLesson.position = $scope.lessons.length;
$scope.lessons.push(newLesson);
$rootScope.selectedLesson = newLesson;
};

$scope.select = function (lesson) {
$rootScope.selectedLesson = lesson;
return lesson;
};
$scope.countActivities = function(l) {
if( l.units ) {
return l.units.reduce(function(p,s){return p + (s.activity ? 1 : 0); }, 0);
if( !l.units ){
l.units = [];
return 0;
}
return 0;
return l.units.reduce(function(p,s){return p + (s.activity ? 1 : 0); }, 0);
};
$scope.countVideos = function(l) {
if( l.units ) {
return l.units.reduce(function(p,s){return p + (s.video ? 1 : 0); }, 0);
if( !l.units ) {
l.units = [];
return 0;
}
return 0;
return l.units.reduce(function(p,s){return p + (s.video ? 1 : 0); }, 0);
};

LessonListFactory.then(function(lessons){
$scope.lessons = lessons;
});
Expand Down Expand Up @@ -180,8 +187,12 @@
});
};
$scope.selectedUnit = function() {
if($rootScope.selectedLesson)
return $rootScope.selectedLesson.units[selectedUnitIndex];
if($rootScope.selectedLesson && 'units' in $rootScope.selectedLesson)
if($rootScope.selectedLesson.units.length > 0){
return $rootScope.selectedLesson.units[selectedUnitIndex];
} else {
return {};
}
};
$scope.activity = function() {
if($scope.selectedUnit() && $scope.selectedUnit().activity){
Expand Down Expand Up @@ -220,6 +231,13 @@
act.expected.push(false);
}
};
$scope.saveLesson = function(){
if('id' in $rootScope.selectedLesson){
$rootScope.selectedLesson.$update();
} else {
$rootScope.selectedLesson.$create();
}
};
LessonListFactory.then(function(lessons){
$scope.lessons = lessons;
});
Expand All @@ -231,7 +249,7 @@
* Factories
*/
app.factory('CourseDataFactory', ['$rootScope', '$q', '$resource',
function($rootScope, $q, $resource, $window) {
function($rootScope, $q, $resource) {
var Course = $resource('/api/course/:courseSlug/',{'courseSlug': courseSlug});
var deferred = $q.defer();

Expand All @@ -242,8 +260,8 @@
}
]);

app.factory('LessonListFactory', ['$rootScope', '$q', '$resource',
function($rootScope, $q, $resource, $window) {
app.factory('LessonListFactory', ['$q', '$resource',
function($q, $resource) {
var resourceConfig = {
'get': {
'method':'GET',
Expand All @@ -267,5 +285,25 @@
return deferred.promise;
}
]);

app.factory('Lesson',['$resource',
function($resource){
var resourceConfig = {
'get': {
'method':'GET',
'params':{'course__slug': courseSlug}
},
'create':{
'method':'POST',
'transformRequest': function(data, getter){
data.course = courseSlug;
return JSON.stringify(data);
}
},
'update':{
'method': 'PUT'
}
};
return $resource('/api/lessons/:id', {'id':'@id'}, resourceConfig);
}
]);
})(angular);

0 comments on commit 4a18580

Please sign in to comment.