Skip to content

Commit

Permalink
resolves zetaops/zengine#16 (Form serialiser doesn't respect the orde…
Browse files Browse the repository at this point in the history
…r of fields) ˆˆˆˆ
  • Loading branch information
Evren Esat Özkan committed Nov 11, 2015
1 parent 120bb5b commit bd5d02e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
6 changes: 5 additions & 1 deletion pyoko/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,18 @@
class BaseField(object):
_TYPE = 'Field'
default_value = None
creation_counter = 0

def __init__(self, title='',
default=None,
required=True,
index=False,
type=None,
store=False,
choices=None):
choices=None,
order=None):
self._order = order or self.creation_counter
BaseField.creation_counter += 1
self.required = required
self.choices = choices
self.title = title
Expand Down
7 changes: 6 additions & 1 deletion pyoko/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#
# This file is licensed under the GNU General Public License v3
# (GPLv3). See LICENSE.txt for details.
from collections import OrderedDict

from .fields import *
import six

Expand Down Expand Up @@ -167,7 +169,7 @@ def _get_models(self, result):
})

def _get_fields(self, result, model_obj):
for name, field in model_obj._fields.items():
for name, field in model_obj._ordered_fields:
if name in ['deleted', 'timestamp'] or self._filter_out(name):
continue
result.append({'name': name,
Expand Down Expand Up @@ -232,10 +234,13 @@ def __init__(self, *args, **kwargs):
self._linked_models = {}
self._field_values = {}
self.key = None
self._ordered_fields = []
for key, val in self.__class__.__dict__.items():
if isinstance(val, BaseField):
val.name = key
self._fields[key] = val
for v in sorted(self._fields.items(), key=lambda x: x[1]._order):
self._ordered_fields.append((v[0], v[1]))
super(Form, self).__init__(*args, **kwargs)

def set_data(self, data):
Expand Down
4 changes: 4 additions & 0 deletions pyoko/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,10 @@ def __init__(self, **kwargs):
self._instantiate_nodes()
self._set_fields_values(kwargs)

@lazy_property
def _ordered_fields(self):
return sorted(self._fields.items(), key=lambda kv: kv[1]._order)

def set_tmp_key(self):
self.key = "TMP_%s_%s" % (self.__class__.__name__, uuid4().hex[:10])

Expand Down

0 comments on commit bd5d02e

Please sign in to comment.