From 892471d8e59f0f793d9e09fc76e25c695d721f41 Mon Sep 17 00:00:00 2001 From: mdipierro Date: Sun, 17 Feb 2013 09:49:44 -0600 Subject: [PATCH] fixed issue 1340, bugs in as_dict, thanks Alan --- VERSION | 2 +- gluon/dal.py | 21 ++++++++++++++------- gluon/tests/test_dal.py | 22 +++++++++++----------- 3 files changed, 26 insertions(+), 19 deletions(-) diff --git a/VERSION b/VERSION index 5a07c73b..502482d1 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -Version 2.4.1-alpha.2+timestamp.2013.02.17.00.59.22 +Version 2.4.1-alpha.2+timestamp.2013.02.17.09.49.04 diff --git a/gluon/dal.py b/gluon/dal.py index d1e5a2a6..7fb1bfc8 100644 --- a/gluon/dal.py +++ b/gluon/dal.py @@ -9325,13 +9325,12 @@ class Field(Expression): float, tuple, bool, type(None)) def flatten(obj): - newobj = None if flat: if isinstance(obj, flatten.__class__): return str(type(obj)) elif isinstance(obj, type): try: - newobj = str(obj).split("'")[1] + return str(obj).split("'")[1] except IndexError: return str(obj) elif not isinstance(obj, SERIALIZABLE_TYPES): @@ -9340,11 +9339,14 @@ class Field(Expression): newobj = dict() for k, v in obj.items(): newobj[k] = flatten(v) + return newobj elif isinstance(obj, (list, tuple, set)): - newobj = [flatten(v) for v in obj] + return [flatten(v) for v in obj] else: - newobj = obj - return newobj + return obj + elif isinstance(obj, (dict, set)): + return obj.copy() + else: return obj def filter_requires(t, r, options=True): if sanitize and any([keyword in str(t).upper() for @@ -9355,9 +9357,14 @@ class Field(Expression): if options and hasattr(r, "options"): if callable(r.options): r.options() - newr = r.__dict__ + newr = r.__dict__.copy() else: - newr = r + newr = r.copy() + + # remove options if not required + if not options and newr.has_key("labels"): + [newr.update({key:None}) for key in + ("labels", "theset") if (key in newr)] for k, v in newr.items(): if k == "other": diff --git a/gluon/tests/test_dal.py b/gluon/tests/test_dal.py index 696a5e4e..fa7c081b 100644 --- a/gluon/tests/test_dal.py +++ b/gluon/tests/test_dal.py @@ -660,7 +660,10 @@ class TestDALDictImportExport(unittest.TestCase): "items":{"staff":{"items": {"name": {"default":"Michael"}, "food": - {"default":"Spam"}}}, + {"default":"Spam"}, + "show": + {"type": "reference show"} + }}, "show":{"items": {"name": {"default":mpfc}, "rating": @@ -669,15 +672,18 @@ class TestDALDictImportExport(unittest.TestCase): assert "staff" in db4.tables assert "name" in db4.staff assert db4.show.rating.type == "double" - assert db4.show.insert() is not None + assert (db4.show.insert(), db4.show.insert(name="Loriot"), + db4.show.insert(name="Il Mattatore")) == (1, 2, 3) assert db4(db4.show).select().first().id == 1 assert db4(db4.show).select().first().name == mpfc dbdict5 = {"uri": 'sqlite:memory:'} db5 = DAL(dbdict5) assert db5.tables in ([], None) + assert not (str(db5) in ("", None)) - dbdict6 = {"items":{"staff":{}, + dbdict6 = {"uri": 'sqlite:memory:', + "items":{"staff":{}, "show":{"items": {"name": {}, "rating": {"type":"double"}}}}} @@ -685,14 +691,8 @@ class TestDALDictImportExport(unittest.TestCase): assert len(db6["staff"].fields) == 1 assert "name" in db6["show"].fields - # the following would fail (see issue 1332) - # assert db6.staff.insert() is not None - # assert db6(db6.staff).select().first().id == 1 - - dbdict7 = {} - db7 = DAL(dbdict7) - db7.tables() in (None, []) - assert not str(db7) in ("", None) + assert db6.staff.insert() is not None + assert db6(db6.staff).select().first().id == 1 db6.staff.drop() db6.show.drop()