PageRenderTime 115ms CodeModel.GetById 36ms RepoModel.GetById 7ms app.codeStats 0ms

/client/static/js/models/users.js

https://github.com/pandaproject/panda
JavaScript | 216 lines | 160 code | 30 blank | 26 comment | 34 complexity | 55440b58a2925e915972cdb4d78c182a MD5 | raw file
  1. PANDA.models.User = Backbone.Model.extend({
  2. /*
  3. Equivalent of django.contrib.auth.models.User.
  4. */
  5. urlRoot: PANDA.API + "/user",
  6. notifications: null,
  7. datasets: null,
  8. exports: null,
  9. subscriptions: null,
  10. initialize: function(attributes) {
  11. if ("notifications" in attributes) {
  12. this.notifications = new PANDA.collections.Notifications(attributes.notifications);
  13. } else {
  14. this.notifications = new PANDA.collections.Notifications();
  15. }
  16. if ("subscriptions" in attributes) {
  17. this.subscriptions = new PANDA.collections.SearchSubscriptions(attributes.subscriptions);
  18. } else {
  19. this.subscriptions = new PANDA.collections.SearchSubscriptions();
  20. }
  21. if ("datasets" in attributes) {
  22. this.datasets = new PANDA.collections.Datasets(attributes.datasets);
  23. } else {
  24. this.datasets = new PANDA.collections.Datasets();
  25. }
  26. if ("exports" in attributes) {
  27. this.exports = new PANDA.collections.Exports(attributes.exports);
  28. } else {
  29. this.exports = new PANDA.collections.Exports();
  30. }
  31. },
  32. parse: function(response) {
  33. /*
  34. * Extract embedded models from serialized data.
  35. */
  36. if ("notifications" in response) {
  37. this.notifications = new PANDA.collections.Notifications(response.notifications);
  38. } else {
  39. this.notifications = new PANDA.collections.Notifications();
  40. }
  41. if ("subscriptions" in response) {
  42. this.subscriptions = new PANDA.collections.SearchSubscriptions(response.subscriptions);
  43. } else {
  44. this.subscriptions = new PANDA.collections.SearchSubscriptions();
  45. }
  46. if ("datasets" in response) {
  47. this.datasets = new PANDA.collections.Datasets(response.datasets);
  48. } else {
  49. this.datasets = new PANDA.collections.Datasets();
  50. }
  51. if ("exports" in response) {
  52. this.exports = new PANDA.collections.Exports(response.exports);
  53. } else {
  54. this.exports = new PANDA.collections.Exports();
  55. }
  56. return response
  57. },
  58. patch: function(attributes, success_callback, error_callback) {
  59. /*
  60. * Update a user in place using the PATCH verb.
  61. *
  62. * A special-case for the user edit page so that readonly attributes
  63. * are not lost.
  64. */
  65. this.set(attributes || {});
  66. Redd.ajax({
  67. url: this.url() + "?patch=true",
  68. type: "PUT",
  69. data: JSON.stringify(this.toJSON()),
  70. contentType: "application/json",
  71. dataType: "json",
  72. async: false,
  73. success: _.bind(function(response) {
  74. this.set(response);
  75. if (success_callback) {
  76. success_callback(this);
  77. }
  78. }, this),
  79. error: _.bind(function(xhr, status, error) {
  80. if (error_callback) {
  81. error_callback(this, xhr.responseText);
  82. }
  83. }, this)
  84. });
  85. },
  86. refresh_notifications: function(success_callback, error_callback) {
  87. /*
  88. * Refresh notifications list from the server.
  89. */
  90. this.notifications.fetch({
  91. data: {
  92. limit: PANDA.settings.NOTIFICATIONS_TO_SHOW
  93. },
  94. success: _.bind(function(response) {
  95. if (success_callback) {
  96. success_callback(this, response);
  97. }
  98. }, this),
  99. error: function(xhr, textStatus) {
  100. error = JSON.parse(xhr.responseText);
  101. if (error_callback) {
  102. error_callback(this, error);
  103. }
  104. }
  105. });
  106. },
  107. mark_notifications_read: function() {
  108. /*
  109. * Mark all notifications as read.
  110. */
  111. var now = moment().format("YYYY-MM-DDTHH:mm:ss");
  112. var patch = { objects: [] };
  113. this.notifications.each(function(note) {
  114. if (!note.get("read_at")) {
  115. note.set({ read_at: now });
  116. patch.objects.push({ resource_uri: note.id, read_at: now });
  117. }
  118. });
  119. if (patch.objects.length == 0) {
  120. return;
  121. }
  122. $.ajax({
  123. url: this.notifications.url() + "?patch=true",
  124. contentType: "application/json",
  125. dataType: "json",
  126. type: "PUT",
  127. data: JSON.stringify(patch)
  128. });
  129. },
  130. set_show_login_help: function(value, success_callback, error_callback) {
  131. $.ajax({
  132. url: this.url() + "login_help/",
  133. contentType: "application/json",
  134. dataType: "json",
  135. type: "POST",
  136. data: JSON.stringify({ "show_login_help": value }),
  137. success: _.bind(function(response) {
  138. if (success_callback) {
  139. success_callback(this, response);
  140. }
  141. }, this),
  142. error: function(xhr, textStatus) {
  143. error = JSON.parse(xhr.responseText);
  144. if (error_callback) {
  145. error_callback(this, error);
  146. }
  147. }
  148. });
  149. },
  150. toJSON: function(full) {
  151. /*
  152. * Append embedded models to serialized data.
  153. *
  154. * NOTE: never serialize embedded data from search results.
  155. */
  156. var js = Backbone.Model.prototype.toJSON.call(this);
  157. if (full) {
  158. js["notifications"] = this.notifications.toJSON();
  159. } else {
  160. js["notifications"] = this.notifications.map(function(note) { return note.id });
  161. }
  162. if (full) {
  163. js["subscriptions"] = this.subscriptions.toJSON();
  164. } else {
  165. js["subscriptions"] = this.subscriptions.map(function(sub) { return sub.id });
  166. }
  167. if (full) {
  168. js["datasets"] = this.datasets.toJSON();
  169. } else {
  170. js["datasets"] = this.datasets.map(function(dataset) { return dataset.id });
  171. }
  172. if (full) {
  173. js["exports"] = this.exports.toJSON();
  174. } else {
  175. js["exports"] = this.exports.map(function(exp) { return exp.id });
  176. }
  177. return js;
  178. }
  179. });
  180. PANDA.collections.Users = Backbone.Collection.extend({
  181. /*
  182. A collection of django.contrib.auth.models.User equivalents.
  183. */
  184. model: PANDA.models.User,
  185. urlRoot: PANDA.API + "/user"
  186. });