root / snf-cyclades-app / synnefo / ui / static / snf / js / glance_models.js @ f70c8d47
History | View | Annotate | Download (4.1 kB)
1 |
;(function(root){
|
---|---|
2 |
|
3 |
// root
|
4 |
var root = root;
|
5 |
|
6 |
// setup namepsaces
|
7 |
var snf = root.synnefo = root.synnefo || {};
|
8 |
var glance = snf.glance = snf.glance || {};
|
9 |
var models = glance.models = glance.models || {}
|
10 |
var storage = glance.storage = glance.storage || {};
|
11 |
var util = snf.util = snf.util || {};
|
12 |
|
13 |
// shortcuts
|
14 |
var bb = root.Backbone;
|
15 |
var slice = Array.prototype.slice
|
16 |
|
17 |
models.GlanceImage = snf.models.Image.extend({ |
18 |
api_type: 'glance', |
19 |
|
20 |
get_size: function() { |
21 |
return this.get('size') / 1024 / 1024; |
22 |
}, |
23 |
|
24 |
get_readable_size: function() { |
25 |
return this.get('size') > 0 ? util.readablizeBytes(this.get('size')) : "unknown"; |
26 |
}, |
27 |
|
28 |
display_size: function() { |
29 |
return this.get_readable_size(); |
30 |
} |
31 |
|
32 |
}) |
33 |
|
34 |
models.GlanceImages = snf.models.Images.extend({ |
35 |
model: models.GlanceImage,
|
36 |
api_type: 'glance', |
37 |
|
38 |
type_selections: {'personal':'My images', |
39 |
'shared': 'Shared with me', |
40 |
'public': 'Public'}, |
41 |
type_selections_order: ['system', 'personal', 'shared', 'public'], |
42 |
display_metadata: ['created_at', 'updated_at', 'filename', 'format', |
43 |
'size', 'status'], |
44 |
read_method: 'head', |
45 |
|
46 |
// custom glance api parser
|
47 |
parse: function (resp, xhr) { |
48 |
if (_.isArray(resp)) {
|
49 |
resp = {'images': {'values': resp }}; |
50 |
} |
51 |
return models.GlanceImages.__super__.parse.call(this, resp, xhr); |
52 |
}, |
53 |
|
54 |
_read_image_from_request: function(image, msg, xhr) { |
55 |
var img = {};
|
56 |
img['metadata'] = {values:{}}; |
57 |
|
58 |
var headers = snf.util.parseHeaders(xhr.getAllResponseHeaders().toLowerCase());
|
59 |
|
60 |
_.each(headers, function(value, key) {
|
61 |
if (key.indexOf("x-image-meta") == -1) { |
62 |
return
|
63 |
} |
64 |
|
65 |
if (key.indexOf("x-image-meta-property") == -1) { |
66 |
img[key.replace("x-image-meta-","").replace(/-/g,"_")] = _.trim(value); |
67 |
} else {
|
68 |
img.metadata.values[key.replace('x-image-meta-property-',"").replace(/-/g,"_")] = _.trim(value); |
69 |
} |
70 |
|
71 |
}) |
72 |
|
73 |
return img;
|
74 |
}, |
75 |
|
76 |
parse_meta: function(img) { |
77 |
if (img.properties) {
|
78 |
img.metadata = {}; |
79 |
img.metadata.values = img.properties; |
80 |
} |
81 |
|
82 |
// fixes plankton regression (returns lowercase meta keys)
|
83 |
if (img.metadata.values.os && !img.metadata.values.OS) {
|
84 |
img.metadata.values.OS = img.metadata.values.os; |
85 |
} |
86 |
|
87 |
img = models.GlanceImages.__super__.parse_meta.call(this, img);
|
88 |
return img;
|
89 |
}, |
90 |
|
91 |
get_system_images: function() { |
92 |
return _.filter(this.active(), function(i) { |
93 |
return _.include(_.keys(snf.config.system_images_owners),
|
94 |
i.get_owner()); |
95 |
}) |
96 |
}, |
97 |
|
98 |
get_personal_images: function() { |
99 |
return _.filter(this.active(), function(i) { |
100 |
return i.get_owner() == snf.user.username
|
101 |
}); |
102 |
}, |
103 |
|
104 |
get_public_images: function() { |
105 |
return _.filter(this.active(), function(i){ return i.is_public() }) |
106 |
}, |
107 |
|
108 |
get_shared_images: function() { |
109 |
return _.filter(this.active(), function(i){ |
110 |
return !_.include(_.keys(snf.config.system_images_owners),
|
111 |
i.get_owner()) && |
112 |
i.get_owner() != snf.user.username && |
113 |
!i.is_public(); |
114 |
}); |
115 |
} |
116 |
|
117 |
}) |
118 |
|
119 |
// replace images storage collection
|
120 |
snf.glance.register = function() { |
121 |
// storage initialization
|
122 |
snf.storage.glance = {}; |
123 |
snf.storage.glance.images = new models.GlanceImages();
|
124 |
|
125 |
// use glance images
|
126 |
snf.storage.images = snf.storage.glance.images; |
127 |
} |
128 |
|
129 |
})(this);
|
130 |
|