Skip to content
This repository
Browse code

Add custom reset password form. Closes #682.

  • Loading branch information...
commit 73594d71a141e3de058b20714875bde9373e5174 1 parent 88499c8
Christopher Groskopf onyxfish authored
18 client/static/css/panda.css
@@ -76,6 +76,24 @@ h3 { font-weight: normal; padding-bottom: 4px; }
76 76 font-weight: normal;
77 77 }
78 78
  79 +/* Reset password */
  80 +
  81 +#reset {
  82 + margin-top: 75px;
  83 + width: 400px;
  84 + margin-left: auto;
  85 + margin-right: auto;
  86 + background-color: #404040;
  87 +}
  88 +
  89 +#reset, #reset h2, #reset label { color: #fff; }
  90 +
  91 +#reset h2 {
  92 + display: block;
  93 + margin-bottom: 10px;
  94 + font-weight: normal;
  95 +}
  96 +
79 97 /* Login */
80 98
81 99 #login {
5 client/static/js/routers/index.js
... ... @@ -1,6 +1,7 @@
1 1 PANDA.routers.Index = Backbone.Router.extend({
2 2 routes: {
3 3 "activate/:activation_key": "activate",
  4 + "reset\-password/:activation_key": "reset_password",
4 5 "login": "login",
5 6 "logout": "logout",
6 7 "": "search",
@@ -39,6 +40,10 @@ PANDA.routers.Index = Backbone.Router.extend({
39 40 this.controller.goto_activate(activation_key);
40 41 },
41 42
  43 + reset_password: function(activation_key) {
  44 + this.controller.goto_reset_password(activation_key);
  45 + },
  46 +
42 47 login: function() {
43 48 this.controller.goto_login();
44 49 },
100 client/static/js/views/reset_password.js
... ... @@ -0,0 +1,100 @@
  1 +PANDA.views.ResetPassword = Backbone.View.extend({
  2 + events: {
  3 + "submit #reset-form": "reset_password"
  4 + },
  5 +
  6 + activation_key: null,
  7 +
  8 + initialize: function() {
  9 + _.bindAll(this);
  10 + },
  11 +
  12 + reset: function(activation_key) {
  13 + this.activation_key = activation_key;
  14 +
  15 + $.ajax({
  16 + url: '/check_activation_key/' + activation_key,
  17 + dataType: 'json',
  18 + type: 'GET',
  19 + success: _.bind(function(data, status, xhr) {
  20 + data = $.parseJSON(xhr.responseText);
  21 +
  22 + this.render(data);
  23 + }, this),
  24 + error: _.bind(function(xhr, status, error) {
  25 + this.render({});
  26 +
  27 + try {
  28 + errors = $.parseJSON(xhr.responseText);
  29 + } catch(e) {
  30 + errors = { "__all__": "Unknown error" };
  31 + }
  32 +
  33 + $("#reset-form").show_errors(errors, "Password reset failed!");
  34 + }, this)
  35 + });
  36 + },
  37 +
  38 + render: function(data) {
  39 + var context = PANDA.utils.make_context(data)
  40 + this.$el.html(PANDA.templates.reset_password(context));
  41 + },
  42 +
  43 + validate: function() {
  44 + var data = $("#reset-form").serializeObject();
  45 + var errors = {};
  46 +
  47 + if (!data["password"]) {
  48 + errors["password"] = ["This field is required."]
  49 + }
  50 +
  51 + if (!data["reenter_password"]) {
  52 + errors["reenter_password"] = ["This field is required."]
  53 + }
  54 +
  55 + if (data["password"] != data["reenter_password"]) {
  56 + if ("password" in errors || "reenter_password" in errors) {
  57 + // Skip
  58 + } else {
  59 + errors["reenter_password"] = ["Passwords do not match."]
  60 + }
  61 + }
  62 +
  63 + return errors;
  64 + },
  65 +
  66 + reset_password: function() {
  67 + var errors = this.validate();
  68 +
  69 + if (!_.isEmpty(errors)) {
  70 + $("#reset-form").show_errors(errors, "Password reset failed!");
  71 +
  72 + return false;
  73 + }
  74 +
  75 + $.ajax({
  76 + url: '/activate/',
  77 + dataType: 'json',
  78 + type: 'POST',
  79 + data: $("#reset-form").serialize(),
  80 + success: function(data, status, xhr) {
  81 + Redd.goto_login();
  82 + },
  83 + error: function(xhr, status, error) {
  84 + Redd.set_current_user(null);
  85 +
  86 + try {
  87 + errors = $.parseJSON(xhr.responseText);
  88 + } catch(e) {
  89 + errors = { "__all__": "Unknown error" };
  90 + }
  91 +
  92 + $("#reset-form").show_errors(errors, "Password reset failed!");
  93 + }
  94 + });
  95 +
  96 + return false;
  97 + }
  98 +});
  99 +
  100 +
7 client/static/js/views/root.js
@@ -329,6 +329,13 @@ PANDA.views.Root = Backbone.View.extend({
329 329 this._router.navigate("activate/" + activation_key);
330 330 },
331 331
  332 + goto_reset_password: function(activation_key) {
  333 + this.current_content_view = this.get_or_create_view("ResetPassword");
  334 + this.current_content_view.reset(activation_key);
  335 +
  336 + this._router.navigate("reset-password/" + activation_key);
  337 + },
  338 +
332 339 goto_login: function(next) {
333 340 this.current_content_view = this.get_or_create_view("Login");
334 341 this.current_content_view.reset(next);
28 client/static/templates/reset_password.jst
... ... @@ -0,0 +1,28 @@
  1 +<div class="row-fluid">
  2 + <div id="reset">
  3 + <form id="reset-form">
  4 + <img src="<%= PANDA.settings.STATIC_URL %>img/panda_and_ire.png" />
  5 + <h2>Reset your password</h2>
  6 + <div class="alert"></div>
  7 + <% if (typeof(email) !== 'undefined') { %>
  8 + <fieldset>
  9 + <input type="hidden" name="email" value="<%= email %>" />
  10 + <input type="hidden" name="first_name" value="<%= first_name %>" />
  11 + <input type="hidden" name="last_name" value="<%= last_name %>" />
  12 + <input type="hidden" name="activation_key" value="<%= activation_key %>" />
  13 + <div class="control-group">
  14 + <label for="password">Password</label>
  15 + <input type="password" name="password" />
  16 + <span class="help-inline"></span>
  17 + </div>
  18 + <div class="control-group">
  19 + <label for="reenter_password">Re-enter password</label>
  20 + <input type="password" name="reenter_password" />
  21 + <span class="help-inline"></span>
  22 + </div>
  23 + <input type="submit" class="btn primary" value="Reset password" />
  24 + </fieldset>
  25 + <% } %>
  26 + </form>
  27 + </div>
  28 +</div>
1  client/templates/index.html
@@ -111,6 +111,7 @@
111 111 <script type="text/javascript" src="{{ settings.STATIC_URL }}js/views/not_found.js"></script>
112 112 <script type="text/javascript" src="{{ settings.STATIC_URL }}js/views/server_error.js"></script>
113 113 <script type="text/javascript" src="{{ settings.STATIC_URL }}js/views/activate.js"></script>
  114 + <script type="text/javascript" src="{{ settings.STATIC_URL }}js/views/reset_password.js"></script>
114 115 <script type="text/javascript" src="{{ settings.STATIC_URL }}js/views/login.js"></script>
115 116 <script type="text/javascript" src="{{ settings.STATIC_URL }}js/views/home.js"></script>
116 117 <script type="text/javascript" src="{{ settings.STATIC_URL }}js/views/datasets_search.js"></script>
2  panda/views.py
@@ -194,7 +194,7 @@ def forgot_password(request):
194 194 user_profile.save()
195 195
196 196 email_subject = 'Forgotten password'
197   - email_body = 'PANDA received a request to change your password.\n\nTo set your new password follow this link:\n\nhttp://%s/#activate/%s\n\nIf you did not request this email you should notify your adminstrator.' % (config_value('DOMAIN', 'SITE_DOMAIN'), user_profile.activation_key)
  197 + email_body = 'PANDA received a request to change your password.\n\nTo set your new password follow this link:\n\nhttp://%s/#reset-password/%s\n\nIf you did not request this email you should notify your adminstrator.' % (config_value('DOMAIN', 'SITE_DOMAIN'), user_profile.activation_key)
198 198
199 199 send_mail(email_subject,
200 200 email_body,

0 comments on commit 73594d7

Please sign in to comment.
Something went wrong with that request. Please try again.