From c2a668de6124055a302881ba2738de4cd98a2157 Mon Sep 17 00:00:00 2001
From: Ben Adida <ben@adida.net>
Date: Mon, 14 Mar 2011 15:58:49 -0700
Subject: [PATCH] made pretty_results and datatypes more robust in the face of
 nulls

---
 helios/datatypes/__init__.py |  9 ++++++++-
 helios/models.py             | 11 +++++++----
 2 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/helios/datatypes/__init__.py b/helios/datatypes/__init__.py
index 98b3ad7..7eaf0e4 100644
--- a/helios/datatypes/__init__.py
+++ b/helios/datatypes/__init__.py
@@ -171,7 +171,10 @@ class LDObject(object):
                 self.structured_fields[f] = sub_ld_object
 
                 # set the field on the wrapped object too
-                self._setattr_wrapped(f, sub_ld_object.wrapped_obj)
+                if sub_ld_object != None:
+                    self._setattr_wrapped(f, sub_ld_object.wrapped_obj)
+                else:
+                    self._setattr_wrapped(f, None)
             else:
                 # a simple type
                 new_val = self.process_value_in(f, d[f])
@@ -203,6 +206,10 @@ class LDObject(object):
 
     @classmethod
     def fromDict(cls, d, type_hint=None):
+        # null objects
+        if d == None:
+            return None
+
         # the LD type is either in d or in type_hint
         # FIXME: get this from the dictionary itself
         ld_type = type_hint
diff --git a/helios/models.py b/helios/models.py
index b0d722f..86d6acf 100644
--- a/helios/models.py
+++ b/helios/models.py
@@ -480,9 +480,12 @@ class Election(HeliosModel):
     counts = sorted(enumerate(result), key=lambda(x): x[1])
     counts.reverse()
     
+    the_max = question['max'] or 1
+    the_min = question['min'] or 0
+
     # if there's a max > 1, we assume that the top MAX win
-    if question['max'] > 1:
-      return [c[0] for c in counts[:question['max']]]
+    if the_max > 1:
+      return [c[0] for c in counts[:the_max]]
 
     # if max = 1, then depends on absolute or relative
     if question['result_type'] == 'absolute':
@@ -490,8 +493,8 @@ class Election(HeliosModel):
         return [counts[0][0]]
       else:
         return []
-
-    if question['result_type'] == 'relative':
+    else:
+      # assumes that anything non-absolute is relative
       return [counts[0][0]]    
 
   @property
-- 
GitLab