-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathmodels.js
More file actions
448 lines (368 loc) · 13.6 KB
/
Copy pathmodels.js
File metadata and controls
448 lines (368 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*global _, Backbone, jQuery */
var Shareabouts = Shareabouts || {};
(function(S, $) {
'use strict';
var normalizeModelArguments = function(key, val, options) {
var attrs;
if (key === null || _.isObject(key)) {
attrs = key;
options = val;
} else if (key !== null) {
(attrs = {})[key] = val;
}
options = options ? _.clone(options) : {};
return {
options: options,
attrs: attrs
};
};
S.PaginatedCollection = Backbone.Collection.extend({
resultsAttr: 'results',
parse: function(response) {
this.metadata = response.metadata;
return response[this.resultsAttr];
},
fetchNextPage: function(success, error) {
var collection = this;
if (this.metadata.next) {
collection.fetch({
remove: false,
url: collection.metadata.next,
success: success,
error: error
});
}
},
fetchAllPages: function(options) {
var self = this,
onFirstPageSuccess, onPageComplete,
onPageSuccess, onPageError,
onAllSuccess, onAnyError,
attemptedPages = 0, totalPages = 1;
options = options || {};
options.data = options.data || {};
if (options.error) {
onAnyError = _.once(options.error);
}
onFirstPageSuccess = function(obj, data) {
// Calculate the total number of pages based on the size of the rist
// page, assuming all pages except the last will be the same size.
var pageSize = data[self.resultsAttr].length, i;
totalPages = Math.ceil(data.metadata.length / pageSize);
if (options.success) {
onAllSuccess = _.after(totalPages, options.success);
}
// Fetch all the rest of the pages in parallel.
if (data.metadata.next) {
for (i = 2; i <= totalPages; i++) {
self.fetch(_.defaults({
remove: false,
data: _.defaults({ page: i }, options.data),
complete: onPageComplete,
success: onPageSuccess,
error: onPageError
}, options));
}
}
onPageSuccess.apply(this, arguments);
};
onPageComplete = function() {
attemptedPages++;
if (options.pageComplete) { options.pageComplete.apply(this, arguments); }
if (attemptedPages === totalPages && options.complete) { options.complete.apply(this, arguments); }
};
onPageSuccess = function() {
if (options.pageSuccess) { options.pageSuccess.apply(this, arguments); }
if (onAllSuccess) { onAllSuccess.apply(this, arguments); }
};
onPageError = function() {
if (options.pageError) { options.pageError.apply(this, arguments); }
if (onAnyError) { onAnyError.apply(this, arguments); }
};
this.fetch(_.defaults({
// Note that success gets called before complete, which is imprtant
// because complete should know whether correct total number of pages.
// However, if the request for the first page fails, complete will
// assume one page.
success: onFirstPageSuccess,
error: onPageError,
complete: onPageComplete
}, options));
}
});
S.SubmissionCollection = S.PaginatedCollection.extend({
initialize: function(models, options) {
this.options = options;
},
url: function() {
var submissionType = this.options.submissionType,
placeId = this.options.placeModel && this.options.placeModel.id;
if (!submissionType) { throw new Error('submissionType option' +
' is required.'); }
if (!placeId) { throw new Error('Place model id is not defined. You ' +
'must save the place before saving ' +
'its ' + submissionType + '.'); }
return '/api/places/' + placeId + '/' + submissionType;
},
comparator: 'created_datetime'
});
S.PlaceModel = Backbone.Model.extend({
initialize: function() {
var attachmentData;
this.submissionSets = {};
_.each(this.get('submission_sets'), function(submissions, name) {
var models = [];
// It's a summary if it's not an array of objects
if (_.isArray(submissions)) {
models = submissions;
}
this.submissionSets[name] = new S.SubmissionCollection(models, {
submissionType: name,
placeModel: this
});
}, this);
attachmentData = this.get('attachments') || [];
this.attachmentCollection = new S.AttachmentCollection(attachmentData, {
thingModel: this
});
this.attachmentCollection.each(function(attachment) {
attachment.set({saved: true});
});
},
set: function(key, val, options) {
var args = normalizeModelArguments(key, val, options);
if (_.isArray(args.attrs.attachments) && this.attachmentCollection && !args.options.ignoreAttachments) {
this.attachmentCollection.reset(args.attrs.attachments);
}
_.each(args.attrs.submission_sets, function(submissions, name) {
// It's a summary if it's not an array of objects
if (this.submissionSets && this.submissionSets[name] && _.isArray(submissions)) {
this.submissionSets[name].reset(submissions);
}
}, this);
return S.PlaceModel.__super__.set.call(this, args.attrs, args.options);
},
save: function(key, val, options) {
// Overriding save so that we can handle adding attachments
var self = this,
realSuccessHandler,
args = normalizeModelArguments(key, val, options),
attrs = args.attrs;
options = args.options;
// If this is a new model, then we need to save it first before we can
// attach anything to it.
if (this.isNew()) {
realSuccessHandler = options.success || $.noop;
// Attach files after the model is succesfully saved
options.success = function() {
self.saveAttachments();
realSuccessHandler.apply(this, arguments);
};
} else {
// Model is already saved, attach away!
self.saveAttachments();
}
options.ignoreAttachments = true;
S.PlaceModel.__super__.save.call(this, attrs, options);
},
saveAttachments: function() {
this.attachmentCollection.each(function(attachment) {
if (attachment.isNew()) {
attachment.save();
}
});
},
parse: function(response) {
var properties = _.clone(response.properties);
properties.geometry = _.clone(response.geometry);
return properties;
},
sync: function(method, model, options) {
var attrs;
if (method === 'create' || method === 'update') {
attrs = {
'type': 'Feature',
'geometry': model.get('geometry'),
'properties': _.omit(model.toJSON(), 'geometry')
};
options.data = JSON.stringify(attrs);
options.contentType = 'application/json';
}
return Backbone.sync(method, model, options);
}
});
S.PlaceCollection = S.PaginatedCollection.extend({
url: '/api/places',
model: S.PlaceModel,
resultsAttr: 'features',
fetchByIds: function(ids, options) {
var base = _.result(this, 'url');
if (ids.length === 1) {
this.fetchById(ids[0], options);
} else {
ids = _.map(ids, function(id) { return encodeURIComponent(id); });
options = options ? _.clone(options) : {};
options.url = base + (base.charAt(base.length - 1) === '/' ? '' : '/') + ids.join(',');
this.fetch(_.extend(
{remove: false},
options
));
}
},
fetchById: function(id, options) {
options = options ? _.clone(options) : {};
var self = this,
place = new S.PlaceModel(),
success = options.success;
place.id = id;
place.collection = self;
options.success = function() {
var args = Array.prototype.slice.call(arguments);
self.add(place);
if (success) {
success.apply(this, args);
}
};
place.fetch(options);
}
});
// This does not support editing at this time, which is why it is not a
// ShareaboutsModel
S.AttachmentModel = Backbone.Model.extend({
idAttribute: 'name',
initialize: function(attributes, options) {
this.options = options;
},
isNew: function() {
return this.get('saved') !== true;
},
// TODO: We should be overriding sync instead of save here. The only
// override for save should be to always use wait=True.
save: function(key, val, options) {
// Overriding save so that we can handle adding attachments
var args = normalizeModelArguments(key, val, options),
attrs = _.extend(this.attributes, args.attrs);
return this._attachBlob(attrs.blob, attrs, args.options);
},
_attachBlob: function(blob, attrs, options) {
var formData = new FormData(),
self = this,
progressHandler = S.Util.wrapHandler('progress', this, options.progress),
myXhr = $.ajaxSettings.xhr(),
key;
formData.append('file', blob);
for (key in attrs) {
if (!_(['file', 'blob', 'undefined']).contains(key)) {
formData.append(key, attrs[key]);
}
}
options = options || {};
$.ajax({
url: this.collection.url(),
type: 'POST',
xhr: function() { // custom xhr
if(myXhr.upload){ // check if upload property exists
myXhr.upload.addEventListener('progress', progressHandler, false); // for handling the progress of the upload
}
return myXhr;
},
//Ajax events
success: function() {
var args = Array.prototype.slice.call(arguments);
// Set the save attribute on the incoming data so that we know it's
// not new.
args[0].saved = true;
self.set({saved: true});
if (options.success) {
options.success.apply(this, args);
}
},
error: options.error,
// Form data
data: formData,
//Options to tell JQuery not to process data or worry about content-type
cache: false,
contentType: false,
processData: false
});
}
});
S.AttachmentCollection = Backbone.Collection.extend({
model: S.AttachmentModel,
initialize: function(models, options) {
this.options = options;
},
url: function() {
var thingModel = this.options.thingModel,
thingUrl = thingModel.url();
return thingUrl + '/attachments';
}
});
S.ActionCollection = S.PaginatedCollection.extend({
url: '/api/actions',
comparator: function(a, b) {
if (a.get('created_datetime') > b.get('created_datetime')) {
return -1;
} else {
return 1;
}
}
});
}(Shareabouts, jQuery));
/*global jQuery */
/*****************************************************************************
CSRF Validation
---------------
Django protects against Cross Site Request Forgeries (CSRF) by default. This
type of attack occurs when a malicious Web site contains a link, a form button
or some javascript that is intended to perform some action on your Web site,
using the credentials of a logged-in user who visits the malicious site in their
browser.
Since the API proxy view sends requests that write data to the Shareabouts
service authenticated as the owner of this dataset, we want to protect the API
view against CSRF. In order to ensure that AJAX POST/PUT/DELETE requests that
are made via jQuery will not be caught by the CSRF protection, we use the
following code. For more information, see:
https://docs.djangoproject.com/en/1.4/ref/contrib/csrf/
*/
jQuery(document).ajaxSend(function(event, xhr, settings) {
function getCookie(name) {
var cookieValue = null;
if (document.cookie && document.cookie !== '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) === (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
function sameOrigin(url) {
// url could be relative or scheme relative or absolute
var host = document.location.host; // host + port
var protocol = document.location.protocol;
var sr_origin = '//' + host;
var origin = protocol + sr_origin;
// Allow absolute or scheme relative URLs to same origin
return (url == origin || url.slice(0, origin.length + 1) == origin + '/') ||
(url == sr_origin || url.slice(0, sr_origin.length + 1) == sr_origin + '/') ||
// or any other URL that isn't scheme relative or absolute i.e relative.
!(/^(\/\/|http:|https:).*/.test(url));
}
function safeMethod(method) {
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
if (!safeMethod(settings.type) && sameOrigin(settings.url)) {
xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
}
// If this is a DELETE request, explicitly set the data to be sent so that
// the browser will calculate a value for the Content-Length header.
if (settings.type === 'DELETE') {
xhr.setRequestHeader("Content-Type", "application/json");
settings.data = '{}';
}
});