|
@@ -1,7 +1,7 @@
|
1
|
1
|
from django.db import models
|
2
|
2
|
|
3
|
3
|
class AttributeType(models.Model):
|
4
|
|
- """Defines a product atrribute type"""
|
|
4
|
+ """Defines a product attribute type"""
|
5
|
5
|
name = models.CharField(max_length = 128)
|
6
|
6
|
|
7
|
7
|
def __unicode__(self):
|
|
@@ -11,11 +11,25 @@ class AttributeType(models.Model):
|
11
|
11
|
class Type(models.Model):
|
12
|
12
|
"""Defines a product type"""
|
13
|
13
|
name = models.CharField(max_length = 128)
|
14
|
|
- attribute_types = models.ManyToManyField('product.AttributeType')
|
|
14
|
+ attribute_types = models.ManyToManyField('product.AttributeType', through = 'product.AttributeTypeMembership')
|
15
|
15
|
|
16
|
16
|
def __unicode__(self):
|
17
|
17
|
return self.name
|
18
|
18
|
|
|
19
|
+class AttributeTypeMembership(models.Model):
|
|
20
|
+ RELATIONSHIP_CHOICES = (
|
|
21
|
+ ('optional', 'optional'),
|
|
22
|
+ ('required', 'required'),
|
|
23
|
+ ('required_basket', 'required for purchase'),
|
|
24
|
+ )
|
|
25
|
+ type = models.ForeignKey('product.Type')
|
|
26
|
+ attribute_type = models.ForeignKey('product.AttributeType')
|
|
27
|
+ relation_type = models.CharField(max_length = 16, choices = RELATIONSHIP_CHOICES, default = 'optional')
|
|
28
|
+
|
|
29
|
+ def __unicode__(self):
|
|
30
|
+ return "%s -> %s (%s)" % (self.type.name, self.attribute_type.name, self.relation_type)
|
|
31
|
+
|
|
32
|
+
|
19
|
33
|
class Item(models.Model):
|
20
|
34
|
"""The base product object"""
|
21
|
35
|
name = models.CharField(max_length = 128)
|
|
@@ -38,11 +52,12 @@ class Item(models.Model):
|
38
|
52
|
A boolean if the product is valid
|
39
|
53
|
"""
|
40
|
54
|
required_attribute_names = []
|
41
|
|
- for attribute_type in self.type.attribute_types.all():
|
|
55
|
+ for attribute_type in self.type.attribute_types.filter(attributetypemembership__relation_type = 'required'):
|
42
|
56
|
required_attribute_names.append(attribute_type.name)
|
43
|
57
|
|
44
|
58
|
for attribute in self.attribute_set.all():
|
45
|
|
- required_attribute_names.remove(attribute.attribute_type.name)
|
|
59
|
+ if attribute.attribute_type.name in required_attribute_names:
|
|
60
|
+ required_attribute_names.remove(attribute.attribute_type.name)
|
46
|
61
|
|
47
|
62
|
return 0 == len(required_attribute_names)
|
48
|
63
|
|