Pārlūkot izejas kodu

Merged in 3 more commits of Jon's updating products

master
David Winterbottom 15 gadus atpakaļ
vecāks
revīzija
2b73c88c40
2 mainītis faili ar 21 papildinājumiem un 4 dzēšanām
  1. 2
    0
      oscar/product/admin.py
  2. 19
    4
      oscar/product/models.py

+ 2
- 0
oscar/product/admin.py Parādīt failu

@@ -2,6 +2,7 @@ from product.models import AttributeType
2 2
 from product.models import Type
3 3
 from product.models import Item
4 4
 from product.models import Attribute
5
+from product.models import AttributeTypeMembership
5 6
 from django.contrib import admin
6 7
 
7 8
 class AttributeInline(admin.TabularInline):
@@ -13,6 +14,7 @@ class ItemAdmin(admin.ModelAdmin):
13 14
 
14 15
 
15 16
 admin.site.register(AttributeType)
17
+admin.site.register(AttributeTypeMembership)
16 18
 admin.site.register(Type)
17 19
 admin.site.register(Item, ItemAdmin)
18 20
 admin.site.register(Attribute)

+ 19
- 4
oscar/product/models.py Parādīt failu

@@ -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
 

Notiek ielāde…
Atcelt
Saglabāt