Skip to content

Commit 36ee421

Browse files
committed
show sizes of resources; warn if they're too big
To encourage authors to think about the size of their exam packages, the question resources tab shows the size of each resource, the total size of all resources, and shows a warning next to resources that are bigger than 2MB.
1 parent 43b1e5d commit 36ee421

4 files changed

Lines changed: 38 additions & 1 deletion

File tree

editor/models.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -937,12 +937,15 @@ def delete(self, *args, **kwargs):
937937
super(Resource, self).delete(*args, **kwargs)
938938

939939
def as_json(self):
940+
p = Path(self.file.path)
941+
stat = p.stat()
940942
return {
941943
'url': self.resource_url,
942944
'name': self.filename,
943945
'pk': self.pk,
944946
'alt_text': self.alt_text,
945-
'mtime': os.path.getmtime(self.file.path),
947+
'mtime': stat.st_mtime,
948+
'size': self.file.size,
946949
}
947950

948951
class Licence(models.Model):

editor/static/js/editor.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2084,6 +2084,19 @@ $(document).ready(function() {
20842084
this.used = ko.observable(false);
20852085
}
20862086

2087+
Editor.filesizeformat = function(n) {
2088+
const kilo = 1 << 10;
2089+
const sizes = ['bytes','KB','MB','GB'];
2090+
let t = 1;
2091+
for(let size of sizes) {
2092+
if(n < kilo) {
2093+
return `${n.toFixed(1)} ${size}`;
2094+
}
2095+
n /= kilo;
2096+
}
2097+
return `${n.toFixed(0)} ${sizes.at(-1)}`;
2098+
}
2099+
20872100
var Resource = Editor.Resource = function(data) {
20882101
this.progress = ko.observable(0);
20892102
this.url = ko.observable('');
@@ -2104,6 +2117,11 @@ $(document).ready(function() {
21042117
this.alt_text = ko.observable('');
21052118

21062119
this.mtime = ko.observable('');
2120+
this.size = ko.observable(0);
2121+
2122+
this.too_big = ko.pureComputed(function() {
2123+
return this.size() > 2 * (1 << 20);
2124+
}, this);
21072125

21082126
this.timestamped_url = ko.pureComputed(function() {
21092127
return `${this.url()}?mtime=${this.mtime()}`;
@@ -2121,6 +2139,7 @@ $(document).ready(function() {
21212139
this.pk(data.pk);
21222140
this.alt_text(data.alt_text);
21232141
this.mtime(data.mtime);
2142+
this.size(data.size);
21242143
this.deleteURL = data.delete_url;
21252144
},
21262145

editor/static/js/question/edit.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ $(document).ready(function() {
6161
Editor.EditorItem.apply(this);
6262

6363
this.resources = ko.observableArray([]);
64+
this.total_resource_size = ko.pureComputed(() => {
65+
return this.resources().map(r => r.size()).reduce((a,b) => a+b,0);
66+
});
6467
this.extensions = ko.observableArray([]);
6568
this.statement = Editor.contentObservable('');
6669
this.advice = Editor.contentObservable('');

editor/templates/question/tabs/resources.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
<p><input id="fileupload" type="file" name="files[]"></p>
1111
</form>
1212
{% endif %}
13+
<!-- ko if: resources().length > 0 -->
14+
<p><strong>Total size of resources: </strong> <span data-bind="text: Editor.filesizeformat(total_resource_size())"></span></p>
15+
<!-- /ko -->
1316
<ul class="media-list" data-bind="foreach: resources">
1417
<li class="media resource">
1518
{% if editable %}<button type="button" class="pull-left btn btn-lg btn-link text-warning delete" data-bind="click: $root.deleteResource" title="Delete this resource"><span class="glyphicon glyphicon-remove"></span></button>{% endif %}
@@ -35,6 +38,15 @@
3538
<!-- ko if: filetype()=='img' -->
3639
{% property 'alt_text' 'Text alternative' placeholder="'Describe this image for someone who can\'t see it'" id="'input-resource-alt_text-'+$index()" dynamic_id=True %}
3740
<!-- /ko -->
41+
42+
<p><strong>Size:</strong> <span data-bind="text: Editor.filesizeformat(size())"></span></p>
43+
44+
<!-- ko if: too_big -->
45+
<div class="alert alert-warning">
46+
<p>This file is quite large. It will take a long time for students to download it.</p>
47+
<!-- ko if: filetype()=='img' --><p>Consider resizing this image to make it smaller.</p><!-- /ko -->
48+
</div>
49+
<!-- /ko -->
3850
{% endwith %}
3951
</form>
4052
</div>

0 commit comments

Comments
 (0)