Fabian Peter Hammerle 7 years ago
parent
commit
bb6d0079a3
2 changed files with 13 additions and 58 deletions
  1. 13 57
      dingguo/__init__.py
  2. 0 1
      tests/test_yaml.py

+ 13 - 57
dingguo/__init__.py

@@ -204,51 +204,21 @@ class Order(_YamlInitConstructor):
             order_date = order_date.date()
         assert type(order_date) is datetime.date
         self.order_date = order_date
-        assert customer_id is None or type(customer_id) is unicode
-        self.customer_id = customer_id
+        if customer_id is not None:
+            assert type(customer_id) is unicode
+            self.customer_id = customer_id
         if items is None:
-            items = []
-        assert type(items) is list
-        self.items = items
+            self.items = []
+        else:
+            assert type(items) is list
+            assert all([isinstance(i, Item) for i in items])
+            self.items = items
         if discounts is None:
-            discounts = []
-        assert type(discounts) is list
-        self.discounts = discounts
-
-    def dict_repr(self):
-        return {k: v for (k, v) in {
-            'articles': self.items,
-            'customer_id': self.customer_id,
-            'discounts': self.discounts,
-            'order_date': self.order_date.strftime('%Y-%m-%d'),
-            'order_id': self.order_id,
-            'platform': self.platform,
-            }.items() if v is not None}
-
-    @staticmethod
-    def from_dict(attr):
-        order = Order(
-                platform = attr['platform'],
-                order_id = attr['order_id'],
-                order_date = datetime.datetime.strptime(attr['order_date'], '%Y-%m-%d'),
-                )
-
-        if 'customer_id' in attr:
-            order.customer_id = attr['customer_id']
-
-        for item in attr['articles']:
-            if type(item) is dict:
-                item = Item.from_dict(item)
-            assert isinstance(item, Item)
-            order.items.append(item)
-
-        for discount in attr['discounts']:
-            if type(discount) is dict:
-                discount = Discount.from_dict(discount)
-            assert isinstance(discount, Discount)
-            order.discounts.append(discount)
-
-        return order
+            self.discounts = []
+        else:
+            assert type(discounts) is list
+            assert all([isinstance(d, Discount) for d in discounts])
+            self.discounts = discounts
 
     def __eq__(self, other):
         return (type(self) == type(other)
@@ -271,20 +241,6 @@ class Item(_YamlInitConstructor):
         assert type(price_brutto) is Sum
         self.price_brutto = price_brutto
 
-    def dict_repr(self):
-        return {
-            'name': self.name,
-            'price_brutto': self.price_brutto.value,
-            'price_brutto_currency': self.price_brutto.currency,
-            }
-
-    @staticmethod
-    def from_dict(attr):
-        return Item(
-                name = attr['name'],
-                price_brutto = Sum(attr['price_brutto'], attr['price_brutto_currency']),
-                )
-
     def __eq__(self, other):
         return (type(self) == type(other)
                 and vars(self) == vars(other))

+ 0 - 1
tests/test_yaml.py

@@ -237,7 +237,6 @@ platformπ:
     order_id: id
     platform: platformπ
   order_b: !order
-    customer_id: null
     discounts: []
     items: []
     order_date: 2015-05-08