Commit 1a576b47 by swdanielli

move error msg to server and add UnderscoreJs

parent 5348193d
...@@ -440,6 +440,7 @@ class RecommenderXBlock(XBlock): ...@@ -440,6 +440,7 @@ class RecommenderXBlock(XBlock):
tracker.emit('upload_screenshot', tracker.emit('upload_screenshot',
{'uploadedFileName': 'FILE_TYPE_ERROR'}) {'uploadedFileName': 'FILE_TYPE_ERROR'})
response.status = 415 response.status = 415
response.body = json.dumps({'error': 'Please upload an image in GIF/JPG/PNG'})
return response return response
# Check whether file size exceeds threshold (30MB) # Check whether file size exceeds threshold (30MB)
...@@ -448,6 +449,7 @@ class RecommenderXBlock(XBlock): ...@@ -448,6 +449,7 @@ class RecommenderXBlock(XBlock):
tracker.emit('upload_screenshot', tracker.emit('upload_screenshot',
{'uploadedFileName': 'FILE_SIZE_ERROR'}) {'uploadedFileName': 'FILE_SIZE_ERROR'})
response.status = 413 response.status = 413
response.body = json.dumps({'error': 'Size of uploaded file exceeds threshold'})
return response return response
try: try:
...@@ -463,6 +465,7 @@ class RecommenderXBlock(XBlock): ...@@ -463,6 +465,7 @@ class RecommenderXBlock(XBlock):
tracker.emit('upload_screenshot', tracker.emit('upload_screenshot',
{'uploadedFileName': 'IMPROPER_S3_SETUP'}) {'uploadedFileName': 'IMPROPER_S3_SETUP'})
response.status = 404 response.status = 404
response.body = json.dumps({'error': 'The configuration of Amazon S3 is not properly set'})
return response return response
response = Response() response = Response()
...@@ -707,6 +710,7 @@ class RecommenderXBlock(XBlock): ...@@ -707,6 +710,7 @@ class RecommenderXBlock(XBlock):
response.headers['Content-Type'] = 'text/plain' response.headers['Content-Type'] = 'text/plain'
if not self.get_user_is_staff(): if not self.get_user_is_staff():
response.status = 403 response.status = 403
response.body = json.dumps({'error': 'Only staff can import resources'})
tracker.emit('import_resources', {'Status': 'NOT_A_STAFF'}) tracker.emit('import_resources', {'Status': 'NOT_A_STAFF'})
return response return response
...@@ -731,6 +735,7 @@ class RecommenderXBlock(XBlock): ...@@ -731,6 +735,7 @@ class RecommenderXBlock(XBlock):
return response return response
except: except:
response.status = 415 response.status = 415
response.body = json.dumps({'error': 'Please submit the JSON file obtained with the download resources button'})
tracker.emit('import_resources', {'Status': 'FILE_FORMAT_ERROR', 'data': raw_data}) tracker.emit('import_resources', {'Status': 'FILE_FORMAT_ERROR', 'data': raw_data})
return response return response
...@@ -819,6 +824,7 @@ class RecommenderXBlock(XBlock): ...@@ -819,6 +824,7 @@ class RecommenderXBlock(XBlock):
frag.add_css(self.resource_string("static/css/tooltipster.css")) frag.add_css(self.resource_string("static/css/tooltipster.css"))
frag.add_css(self.resource_string("static/css/recommender.css")) frag.add_css(self.resource_string("static/css/recommender.css"))
frag.add_css(self.resource_string("static/css/introjs.css")) frag.add_css(self.resource_string("static/css/introjs.css"))
frag.add_javascript(self.resource_string("static/js/src/underscore-min.js"))
frag.add_javascript(self.resource_string("static/js/src/jquery.tooltipster.min.js")) frag.add_javascript(self.resource_string("static/js/src/jquery.tooltipster.min.js"))
frag.add_javascript(self.resource_string("static/js/src/cats.js")) frag.add_javascript(self.resource_string("static/js/src/cats.js"))
frag.add_javascript(self.resource_string("static/js/src/recommender.js")) frag.add_javascript(self.resource_string("static/js/src/recommender.js"))
......
...@@ -63,12 +63,6 @@ var tooltipsCatsText = { ...@@ -63,12 +63,6 @@ var tooltipsCatsText = {
'.resourceRankingForRemovalButton.removeMode': '<span>Click to view resources in ordinary decreasing-vote order</span>' '.resourceRankingForRemovalButton.removeMode': '<span>Click to view resources in ordinary decreasing-vote order</span>'
}; };
var uploadFileErrorText = {
415: 'Please upload an image in GIF/JPG/PNG',
404: 'The configuration of Amazon S3 is not properly set',
413: 'Size of uploaded file exceeds threshold'
};
var importResourceErrorText = { var importResourceErrorText = {
403: 'Only staff can import resources', 403: 'Only staff can import resources',
415: 'Please submit the JSON file obtained with the download resources button' 415: 'Please submit the JSON file obtained with the download resources button'
......
if (typeof Logger === 'undefined') { if (typeof Logger === 'undefined') {
var Logger = { var Logger = {
log: function(a) { return; } log: function(a, b) { return; }
} }
} }
...@@ -27,16 +27,10 @@ function RecommenderXBlock(runtime, element, init_data) { ...@@ -27,16 +27,10 @@ function RecommenderXBlock(runtime, element, init_data) {
* @returns {dictionary} The dictionary for logging an event. * @returns {dictionary} The dictionary for logging an event.
*/ */
function generateLog(status, information) { function generateLog(status, information) {
if (!information) { return _.find([
return { 'status': status, 'element': $(element).attr('data-usage-id') }; {'status': status, 'element': $(element).attr('data-usage-id')},
} {'status': status, 'element': $(element).attr('data-usage-id'), 'information': information}
else { ], function(log) { return information === log.information; });
return {
'status': status,
'information': information,
'element': $(element).attr('data-usage-id')
}
}
} }
/** /**
...@@ -238,8 +232,9 @@ function RecommenderXBlock(runtime, element, init_data) { ...@@ -238,8 +232,9 @@ function RecommenderXBlock(runtime, element, init_data) {
backToView(); backToView();
Logger.log('mit.recommender.importResource', generateLog(loggerStatus['importResource']['complete'], result)); Logger.log('mit.recommender.importResource', generateLog(loggerStatus['importResource']['complete'], result));
}, },
error: function(result, status) { error: function(result) {
alert(importResourceErrorText[result.status]); var data = JSON.parse(result.responseText)
if (data.error) { alert(data.error); }
resetImportResourcePage(); resetImportResourcePage();
}, },
}); });
...@@ -454,7 +449,8 @@ function RecommenderXBlock(runtime, element, init_data) { ...@@ -454,7 +449,8 @@ function RecommenderXBlock(runtime, element, init_data) {
* 404: The filesystem (e.g., Amazon S3) is not properly set * 404: The filesystem (e.g., Amazon S3) is not properly set
* 413: Size of uploaded file exceeds threshold * 413: Size of uploaded file exceeds threshold
*/ */
alert(uploadFileErrorText[result.status]); var data = JSON.parse(result.responseText)
if (data.error) { alert(data.error); }
$("input[name='file']", formDiv).val(''); $("input[name='file']", formDiv).val('');
if (writeType === writeDatabaseEnum.ADD) { enableAddSubmit(); } if (writeType === writeDatabaseEnum.ADD) { enableAddSubmit(); }
else if (writeType === writeDatabaseEnum.EDIT) { enableEditSubmit(); } else if (writeType === writeDatabaseEnum.EDIT) { enableEditSubmit(); }
...@@ -671,7 +667,7 @@ function RecommenderXBlock(runtime, element, init_data) { ...@@ -671,7 +667,7 @@ function RecommenderXBlock(runtime, element, init_data) {
data['title'] = $('.editTitle', element).val(); data['title'] = $('.editTitle', element).val();
data['descriptionText'] = $('.editDescriptionText', element).val(); data['descriptionText'] = $('.editDescriptionText', element).val();
data['description'] = '' data['description'] = ''
if (data['url'] === '' || data['title'] === '') { return; } if (!data.url || data.title) { return; }
var formDiv = $('.editResourceForm', element); var formDiv = $('.editResourceForm', element);
var file = new FormData($(formDiv)[0]); var file = new FormData($(formDiv)[0]);
......
...@@ -20,6 +20,9 @@ function RecommenderXBlock(runtime, element) { ...@@ -20,6 +20,9 @@ function RecommenderXBlock(runtime, element) {
success: function(result) { success: function(result) {
alert('The configurations have been updated'); alert('The configurations have been updated');
} }
error: function(result) {
alert('An internal error happened. We cannot set the configurations right now. Please try again later.');
}
}); });
}); });
} }
......
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