Commits

Andriy Kornatskyy  committed 1351e5c

Added ajax json processing to signin/signup handlers.

  • Participants
  • Parent commits a6127b7

Comments (0)

Files changed (4)

File demos/public/content/static/js/core.js

 
 String.prototype.format = function() {
-    var pattern = /\{\d+\}/g;
     var args = arguments;
-    return this.replace(pattern, function(capture) { return args[capture.match(/\d+/)]; });
+    return this.replace(/\{\d+\}/g, function(capture) {
+        return args[capture.match(/\d+/)];
+    });
 }
+
+function JSONForm(data, form) {
+    $('input[type="password"]', form).val('');
+    $(form).prev('span.error-message').remove();
+    $('span.error', form).remove();
+    $('.error', form).removeClass('error');
+    $.each(data.errors, function(key, value) {
+        if (key == '__ERROR__') {
+            form.before('<span class="error-message">{0}</span>'.format(
+                    value.pop()))
+        }
+        else {
+            $('label[for="{0}"]'.format(key), form).addClass('error')
+            var field = $('#' + key.replace(/_/g, '-'), form);
+            field.addClass('error');
+            field.after('<span class="error">{0}</span>'.format(
+                value.pop()));
+        }
+    });
+}
+
+function ajaxForm(selector, dataType) {
+    if (!dataType) dataType = 'json'
+    $(selector || 'input[type="submit"]').live('click', function(e) {
+        submit = $(this);
+        submit.attr('disabled', 'disabled');
+        var form = submit.parents('form:first');
+        var data = null;
+        if (this.name) {
+            data = form.serializeArray();
+            data.push({name: this.name, value: ''});
+            data = $.param(data);
+        }
+        else
+            data = form.serialize();
+        $.ajax({
+            type: form.attr('method') || 'get',
+            url: form.attr('action'),
+            data: data,
+            dataType: dataType,
+            success: function(data, textStatus, jqXHR) {
+                if (jqXHR.status == 207) {
+                    window.location.replace(jqXHR.getResponseHeader('Location'));
+                } else if (data.see_other) {
+                    window.location.replace(d.see_other);
+                } else if (dataType == 'json'){
+                    submit.removeAttr('disabled');
+                    JSONForm(data, form);
+                }
+            }
+        });
+        return false;
+    });
+}

File demos/public/content/templates/membership/signin.html

                 ${model.remember_me.error()}
                 </p>
                 <p>
-                <input type="submit" name="signin" value="Sign In" />
+                <input type="submit" value="Sign In" />
                 </p>
             </fieldset>
             <i><b>demo</b> user password is P@ssw0rd. You can press 'Ctrl'
         </div>
     </form>
 </div>
+<%block name="script">
+<script type="text/javascript">
+    $(document).ready(function() {
+        ajaxForm();
+    })
+</script>
+</%block>

File demos/public/content/templates/membership/signup.html

                 ${registration.answer.error()}
                 </p>
                 <p>
-                <input name="register" type="submit" value="Register" />
+                <input type="submit" value="Register" />
                 </p>
             </fieldset>
         </div>
     </form>
 </div>
+<%block name="script">
+<script type="text/javascript">
+    $(document).ready(function() {
+        ajaxForm();
+    })
+</script>
+</%block>

File demos/public/src/membership/web/views.py

                 or not self.validate(credential, credential_validator)
                 or not self.factory.membership.authenticate(credential)):
             credential.password = u('')
+            if self.request.ajax:
+                return self.json_response({'errors': self.errors})
             return self.get(credential)
         self.principal = Principal(
                 id=credential.username,
                 roles=tuple(self.factory.membership.roles(
                     credential.username)))
         del self.xsrf_token
-        return self.redirect_for('default')
+        return self.see_other_for('default')
 
 
 class SignOutHandler(BaseHandler):
                 or not self.factory.membership.create_account(registration)):
             registration.credential.password = u('')
             self.model.confirm_password = u('')
+            if self.request.ajax:
+                return self.json_response({'errors': self.errors})
             return self.get(registration)
         self.principal = Principal(
                 id=registration.credential.username,
                 roles=tuple(self.factory.membership.roles(
                     registration.credential.username)))
         del self.resubmission
-        return self.redirect_for('default')
+        return self.see_other_for('default')
 
 
 class MembersOnlyHandler(BaseHandler):