Rework Item.get_all_variations to return a list of dicts

This commit is contained in:
Raphael Michel
2014-09-29 10:55:03 +02:00
parent 4ff339c620
commit 545d0267d4
2 changed files with 27 additions and 20 deletions

View File

@@ -467,10 +467,10 @@ class Item(models.Model):
def get_all_variations(self): def get_all_variations(self):
""" """
This method returns a list containing all variations of this This method returns a list containing all variations of this
item, where n is the number of properties connected to this item. The list contains one dictionary per variation, where
item. The list contains either tupels of PropertyValue objects the Proprty IDs are keys and the PropertyValue objects are
or ItemVariation objects. The returned list is assumed not to values. If an ItemVariation object exists, it is available in
be in any order. the dictionary via the special key 'variation'.
""" """
all_variations = self.variations.all().prefetch_related("values") all_variations = self.variations.all().prefetch_related("values")
all_properties = self.properties.all().prefetch_related("values") all_properties = self.properties.all().prefetch_related("values")
@@ -487,13 +487,14 @@ class Item(models.Model):
if len(comb) == 0: if len(comb) == 0:
continue continue
key = [] key = []
var = {}
for v in comb: for v in comb:
key.append((v.prop.pk, v.pk)) key.append((v.prop.pk, v.pk))
var[v.prop.pk] = v
key = hash(tuple(sorted(key))) key = hash(tuple(sorted(key)))
if key in variations_cache: if key in variations_cache:
result.append(variations_cache[key]) var['variation'] = variations_cache[key]
else: result.append(var)
result.append(comb)
return result return result

View File

@@ -43,9 +43,10 @@ class ItemVariationsTest(TestCase):
self.assertEqual(len(v), 3) self.assertEqual(len(v), 3)
values = [] values = []
for var in v: for var in v:
self.assertIs(type(var), tuple) self.assertIs(type(var), dict)
self.assertIs(type(var[0]), PropertyValue) self.assertIn(p.pk, var)
values.append(var[0].value) self.assertIs(type(var[p.pk]), PropertyValue)
values.append(var[p.pk].value)
self.assertEqual(sorted(values), sorted(['S', 'M', 'L'])) self.assertEqual(sorted(values), sorted(['S', 'M', 'L']))
# One property, one variation # One property, one variation
@@ -55,14 +56,18 @@ class ItemVariationsTest(TestCase):
self.assertIs(type(v), list) self.assertIs(type(v), list)
self.assertEqual(len(v), 3) self.assertEqual(len(v), 3)
values = [] values = []
num_variations = 0
for var in v: for var in v:
if type(var) == ItemVariation: self.assertIs(type(var), dict)
self.assertEqual(iv.pk, var.pk) if 'variation' in var and type(var['variation']) is ItemVariation:
values.append(iv.values.all()[0].value) self.assertEqual(iv.pk, var['variation'].pk)
elif type(var) == tuple: values.append(var['variation'].values.all()[0].value)
self.assertIs(type(var[0]), PropertyValue) num_variations += 1
values.append(var[0].value) elif p.pk in var:
self.assertIs(type(var[p.pk]), PropertyValue)
values.append(var[p.pk].value)
self.assertEqual(sorted(values), sorted(['S', 'M', 'L'])) self.assertEqual(sorted(values), sorted(['S', 'M', 'L']))
self.assertEqual(num_variations, 1)
# Two properties, one variation # Two properties, one variation
p2 = Property.objects.get(event=e, name='Color') p2 = Property.objects.get(event=e, name='Color')
@@ -74,13 +79,14 @@ class ItemVariationsTest(TestCase):
values = [] values = []
num_variations = 0 num_variations = 0
for var in v: for var in v:
if type(var) == ItemVariation: self.assertIs(type(var), dict)
self.assertEqual(iv.pk, var.pk) if 'variation' in var:
self.assertEqual(iv.pk, var['variation'].pk)
values.append(sorted([ivv.value for ivv in iv.values.all()])) values.append(sorted([ivv.value for ivv in iv.values.all()]))
self.assertEqual(sorted([ivv.value for ivv in iv.values.all()]), sorted(['S', 'black'])) self.assertEqual(sorted([ivv.value for ivv in iv.values.all()]), sorted(['S', 'black']))
num_variations += 1 num_variations += 1
elif type(var) == tuple: else:
values.append(sorted([pv.value for pv in var])) values.append(sorted([pv.value for pv in var.values()]))
self.assertEqual(sorted(values), sorted([ self.assertEqual(sorted(values), sorted([
['S', 'black'], ['S', 'black'],
['S', 'blue'], ['S', 'blue'],