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 <miurahr@linux.com>
This commit is contained in:
Hiroshi Miura 2015-07-20 09:39:11 +09:00
parent 6f3115aee8
commit fa66316f8a

View file

@ -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])))