|
|
@@ -1,10 +1,22 @@
|
|
1
|
1
|
"""
|
|
2
|
2
|
Models of products
|
|
3
|
3
|
"""
|
|
|
4
|
+import re
|
|
|
5
|
+
|
|
4
|
6
|
from django.db import models
|
|
5
|
7
|
from django.utils.translation import ugettext_lazy as _
|
|
6
|
8
|
|
|
7
|
9
|
|
|
|
10
|
+def _convert_to_underscores(str):
|
|
|
11
|
+ """
|
|
|
12
|
+ For converting a string in CamelCase or normal text with spaces
|
|
|
13
|
+ to the normal underscored variety
|
|
|
14
|
+ """
|
|
|
15
|
+ without_whitespace = re.sub('\s*', '', str)
|
|
|
16
|
+ s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', without_whitespace)
|
|
|
17
|
+ return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()
|
|
|
18
|
+
|
|
|
19
|
+
|
|
8
|
20
|
class AbstractItemClass(models.Model):
|
|
9
|
21
|
"""
|
|
10
|
22
|
Defines an item type (equivqlent to Taoshop's MediaType).
|
|
|
@@ -76,7 +88,9 @@ class AbstractItem(models.Model):
|
|
76
|
88
|
ordering = ['-date_created']
|
|
77
|
89
|
|
|
78
|
90
|
def __unicode__(self):
|
|
79
|
|
- return self.title
|
|
|
91
|
+ if self.is_variant():
|
|
|
92
|
+ return "%s (%s)" % (self.get_title(), self.get_attribute_summary())
|
|
|
93
|
+ return self.get_title()
|
|
80
|
94
|
|
|
81
|
95
|
def save(self, *args, **kwargs):
|
|
82
|
96
|
if self.is_top_level() and not self.title:
|
|
|
@@ -89,16 +103,22 @@ class AbstractAttributeType(models.Model):
|
|
89
|
103
|
"""
|
|
90
|
104
|
Defines an attribute. (Eg. size)
|
|
91
|
105
|
"""
|
|
92
|
|
- type = models.CharField(_('type'), max_length=128)
|
|
|
106
|
+ code = models.CharField(_('code'), max_length=128)
|
|
|
107
|
+ name = models.CharField(_('name'), max_length=128)
|
|
93
|
108
|
has_choices = models.BooleanField(default=False)
|
|
94
|
109
|
|
|
95
|
110
|
class Meta:
|
|
96
|
111
|
abstract = True
|
|
97
|
|
- ordering = ['type']
|
|
|
112
|
+ ordering = ['code']
|
|
98
|
113
|
|
|
99
|
114
|
def __unicode__(self):
|
|
100
|
115
|
return self.type
|
|
101
|
116
|
|
|
|
117
|
+ def save(self, *args, **kwargs):
|
|
|
118
|
+ if not self.code:
|
|
|
119
|
+ self.code = _convert_to_underscores(self.name)
|
|
|
120
|
+ super(AbstractAttributeType, self).save(*args, **kwargs)
|
|
|
121
|
+
|
|
102
|
122
|
|
|
103
|
123
|
class AbstractAttributeValueOption(models.Model):
|
|
104
|
124
|
"""
|
|
|
@@ -121,7 +141,7 @@ class AbstractItemAttributeValue(models.Model):
|
|
121
|
141
|
Eg: size = L
|
|
122
|
142
|
"""
|
|
123
|
143
|
product = models.ForeignKey('product.Item', related_name='attributes')
|
|
124
|
|
- attribute = models.ForeignKey('product.AttributeType')
|
|
|
144
|
+ type = models.ForeignKey('product.AttributeType')
|
|
125
|
145
|
value = models.CharField(max_length=255)
|
|
126
|
146
|
|
|
127
|
147
|
class Meta:
|