From f9aecb2778957573ed074824c3ff51deafb39108 Mon Sep 17 00:00:00 2001 From: Peter Fogg Date: Tue, 20 Aug 2013 12:23:45 -0400 Subject: [PATCH] Add support for PNGs as course images. Also change the file uploader to accept multiple file types. --- .../coffee/spec/models/upload_spec.coffee | 2 +- cms/static/js/models/uploads.js | 51 +++++++++++++++---- .../js/views/settings/main_settings_view.js | 3 +- 3 files changed, 43 insertions(+), 13 deletions(-) diff --git a/cms/static/coffee/spec/models/upload_spec.coffee b/cms/static/coffee/spec/models/upload_spec.coffee index e4be3b9a80..2b77c2147d 100644 --- a/cms/static/coffee/spec/models/upload_spec.coffee +++ b/cms/static/coffee/spec/models/upload_spec.coffee @@ -28,6 +28,6 @@ describe "CMS.Models.FileUpload", -> it "can accept non-PDF files when explicitly set", -> file = {"type": "image/png"} - @model.set("mimeType": "image/png") + @model.set("mimeTypes": ["image/png"]) @model.set("selectedFile", file) expect(@model.isValid()).toBeTruthy() diff --git a/cms/static/js/models/uploads.js b/cms/static/js/models/uploads.js index 131c554afd..aca115cccf 100644 --- a/cms/static/js/models/uploads.js +++ b/cms/static/js/models/uploads.js @@ -7,22 +7,53 @@ CMS.Models.FileUpload = Backbone.Model.extend({ "uploadedBytes": 0, "totalBytes": 0, "finished": false, - "mimeType": "application/pdf", - "fileType": "PDF" + "mimeTypes": ["application/pdf"] }, - // NOTE: validation functions should return non-internationalized error - // messages. The messages will be passed through gettext in the template. validate: function(attrs, options) { - if(attrs.selectedFile && attrs.selectedFile.type !== this.attributes.mimeType) { + if(attrs.selectedFile && !_.contains(this.attributes.mimeTypes, attrs.selectedFile.type)) { return { message: _.template( - gettext("Only {fileType} files can be uploaded. Please select a file ending in .{fileExtension} to upload."), - { - fileType: this.attributes.fileType, - fileExtension: this.attributes.fileType.toLowerCase() - }), + gettext("Only <%= fileTypes %> files can be uploaded. Please select a file ending in <%= fileExtensions %> to upload."), + this.formatValidTypes() + ), attributes: {selectedFile: true} }; } + }, + // Return a list of this uploader's valid file types + fileTypes: function() { + return _.map( + this.attributes.mimeTypes, + function(type) { + return type.split('/')[1].toUpperCase(); + } + ); + }, + // Return strings for the valid file types and extensions this + // uploader accepts, formatted as natural language + formatValidTypes: function() { + if(this.attributes.mimeTypes.length === 1) { + return { + fileTypes: this.fileTypes()[0], + fileExtensions: this.fileTypes()[0].toLowerCase() + }; + } + var or = gettext('or'); + var formatTypes = function(types) { + return _.template('<%= initial %> <%= or %> <%= last %>', { + initial: _.initial(types).join(', '), + or: or, + last: _.last(types) + }); + }; + return { + fileTypes: formatTypes(this.fileTypes()), + fileExtensions: formatTypes( + _.map(this.fileTypes(), + function(type) { + return '.' + type.toLowerCase(); + }) + ) + }; } }); diff --git a/cms/static/js/views/settings/main_settings_view.js b/cms/static/js/views/settings/main_settings_view.js index 36bee79d80..7304f8e7c0 100644 --- a/cms/static/js/views/settings/main_settings_view.js +++ b/cms/static/js/views/settings/main_settings_view.js @@ -239,8 +239,7 @@ CMS.Views.Settings.Details = CMS.Views.ValidatingView.extend({ var upload = new CMS.Models.FileUpload({ title: gettext("Upload your course image."), message: gettext("Files must be in JPG format."), - mimeType: "image/jpeg", - fileType: "JPG" + mimeTypes: ['image/jpeg', 'image/png'] }); var self = this; var modal = new CMS.Views.UploadDialog({