From 6f3115aee8a97b2e19c194040e4967d1912661b5 Mon Sep 17 00:00:00 2001 From: Hiroshi Miura Date: Sun, 19 Jul 2015 14:33:03 +0900 Subject: [PATCH 1/3] reorder signup fields current implementation is not work on Django 1.7+ anymore. This modification activate feature in Django 1.7+ and drop support for Django 1.6 and before. Signed-off-by: Hiroshi Miura --- symposion/forms.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/symposion/forms.py b/symposion/forms.py index 7799e06a..dcf96e3d 100644 --- a/symposion/forms.py +++ b/symposion/forms.py @@ -1,3 +1,5 @@ +from collections import OrderedDict + from django import forms import account.forms @@ -12,7 +14,7 @@ class SignupForm(account.forms.SignupForm): def __init__(self, *args, **kwargs): super(SignupForm, self).__init__(*args, **kwargs) del self.fields["username"] - self.fields.keyOrder = [ + key_order = [ "email", "email_confirm", "first_name", @@ -20,6 +22,7 @@ class SignupForm(account.forms.SignupForm): "password", "password_confirm" ] + self.fields = reorder_fields(self.fields, key_order) def clean_email_confirm(self): email = self.cleaned_data.get("email") @@ -29,3 +32,18 @@ class SignupForm(account.forms.SignupForm): raise forms.ValidationError( "Email address must match previously typed email address") return email_confirm + + +def reorder_fields(fields, order): + """Reorder form fields by order, removing items not in order. + + >>> reorder_fields( + ... OrderedDict([('a', 1), ('b', 2), ('c', 3)]), + ... ['b', 'c', 'a']) + OrderedDict([('b', 2), ('c', 3), ('a', 1)]) + """ + for key, v in fields.items(): + if key not in order: + del fields[key] + + return OrderedDict(sorted(fields.items(), key=lambda k: order.index(k[0]))) From fa66316f8aca59ecdf70ea10adcdfe20fcc11690 Mon Sep 17 00:00:00 2001 From: Hiroshi Miura Date: Mon, 20 Jul 2015 09:39:11 +0900 Subject: [PATCH 2/3] form key ordering: detect django version - Django 1.6 and before use SortedDict instead of OrderedDict It detect version and return proper dict for key ordering. - field deletion is done by reorder funcion. Signed-off-by: Hiroshi Miura --- symposion/forms.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/symposion/forms.py b/symposion/forms.py index dcf96e3d..e207a0fd 100644 --- a/symposion/forms.py +++ b/symposion/forms.py @@ -1,6 +1,7 @@ from collections import OrderedDict from django import forms +from django import VERSION as django_VERSION import account.forms @@ -13,7 +14,6 @@ class SignupForm(account.forms.SignupForm): def __init__(self, *args, **kwargs): super(SignupForm, self).__init__(*args, **kwargs) - del self.fields["username"] key_order = [ "email", "email_confirm", @@ -46,4 +46,10 @@ def reorder_fields(fields, order): if key not in order: del fields[key] - return OrderedDict(sorted(fields.items(), key=lambda k: order.index(k[0]))) + if django_VERSION < (1, 7, 0): + # fields is SortedDict + fields.keyOrder.sort(key=lambda k: order.index(k[0])) + return fields + else: + # fields is OrderedDict + return OrderedDict(sorted(fields.items(), key=lambda k: order.index(k[0]))) From c22bcf9cf46469d63e3c3d5d32cb59074d069c83 Mon Sep 17 00:00:00 2001 From: Hiroshi Miura Date: Mon, 20 Jul 2015 12:03:54 +0900 Subject: [PATCH 3/3] fix OrderedDict import error in Py2.6 Signed-off-by: Hiroshi Miura --- symposion/forms.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/symposion/forms.py b/symposion/forms.py index e207a0fd..09fc5d4f 100644 --- a/symposion/forms.py +++ b/symposion/forms.py @@ -1,7 +1,9 @@ -from collections import OrderedDict +try: + from collections import OrderedDict +except ImportError: + OrderedDict = None from django import forms -from django import VERSION as django_VERSION import account.forms @@ -46,7 +48,7 @@ def reorder_fields(fields, order): if key not in order: del fields[key] - if django_VERSION < (1, 7, 0): + if not OrderedDict or hasattr(fields, "keyOrder"): # fields is SortedDict fields.keyOrder.sort(key=lambda k: order.index(k[0])) return fields