Bläddra i källkod

Added custom manager for baskets and template params for views

master
David Winterbottom 15 år sedan
förälder
incheckning
47007b7ea0

+ 2
- 0
examples/defaultshop/README.md Visa fil

@@ -0,0 +1,2 @@
1
+This is an example project that uses oscar with all its default settings.  
2
+Nothing is overridden or customised.

+ 11
- 1
oscar/basket/abstract_models.py Visa fil

@@ -5,6 +5,14 @@ from django.db import models
5 5
 from django.utils.translation import ugettext as _
6 6
 from django.core.exceptions import ObjectDoesNotExist
7 7
 
8
+# Basket statuses
9
+OPEN, MERGED, SUBMITTED = ("Open", "Merged", "Submitted")
10
+
11
+
12
+class OpenBasketManager(models.Manager):
13
+    def get_query_set(self):
14
+        return super(OpenBasketManager, self).get_query_set().filter(status=OPEN)
15
+
8 16
 
9 17
 class AbstractBasket(models.Model):
10 18
     """
@@ -12,7 +20,6 @@ class AbstractBasket(models.Model):
12 20
     """
13 21
     # Baskets can be anonymously owned (which are then merged
14 22
     owner = models.ForeignKey(User, related_name='baskets', null=True)
15
-    OPEN, MERGED, SUBMITTED = ("Open", "Merged", "Submitted")
16 23
     STATUS_CHOICES = (
17 24
         (OPEN, _("Open - currently active")),
18 25
         (MERGED, _("Merged - superceded by another basket")),
@@ -26,6 +33,9 @@ class AbstractBasket(models.Model):
26 33
     class Meta:
27 34
         abstract = True
28 35
     
36
+    # Custom manager for searching open baskets only
37
+    open = OpenBasketManager()
38
+    
29 39
     def is_empty(self):
30 40
         return self.get_num_lines() == 0
31 41
     

+ 1
- 1
oscar/basket/views.py Visa fil

@@ -28,7 +28,7 @@ def _get_user_basket(request):
28 28
             basket_hash = request.COOKIES[COOKIE_KEY_HASH]
29 29
             if basket_hash == _get_basket_hash(basket_id):
30 30
                 try:
31
-                    b = Basket.objects.get(pk=basket_id)
31
+                    b = Basket.open.get(pk=basket_id)
32 32
                 except Basket.DoesNotExist, e:
33 33
                     b = None
34 34
     return b    

+ 24
- 4
oscar/product/abstract_models.py Visa fil

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

+ 8
- 2
oscar/product/templates/item.html Visa fil

@@ -23,10 +23,16 @@
23 23
     </tr>
24 24
     <tr>
25 25
         <th>Product type</th>
26
-        <td>{% if item.is_group %}Product group{% else %}Stand-alone{% endif %}
27
-        
26
+        <td>
27
+        {% if item.is_group %}Product group{% else %}{% if item.is_variant %}Variant{% else %}Stand-alone{% endif %}{% endif %}
28 28
         </td>
29 29
     </tr>
30
+    {% for attribute in item.attributes.all %}
31
+    <tr>
32
+        <th>{{ attribute.attribute.type }}</th>
33
+        <th>{{ attribute.value }}</th>
34
+    </tr>
35
+    {% endfor %}
30 36
 </table>
31 37
 
32 38
 <form action="/shop/basket/add-item/" method="post">

+ 6
- 4
oscar/product/views.py Visa fil

@@ -9,13 +9,15 @@ from django.core.urlresolvers import reverse
9 9
 from oscar.product.models import Item
10 10
 from oscar.basket.forms import AddToBasketForm
11 11
 
12
-def item(request, product_id):
12
+def item(request, product_id, template_file='item.html'):
13 13
     """ 
14 14
     Single product page
15 15
     """
16 16
     item = get_object_or_404(Item, pk=product_id)
17
+#    a = attributes = item.attributes.all()
18
+#    assert False
17 19
     form = AddToBasketForm({'product_id': product_id, 'quantity': 1})
18
-    return render_to_response('item.html', locals(), context_instance=RequestContext(request))
20
+    return render_to_response(template_file, locals(), context_instance=RequestContext(request))
19 21
 
20
-def all(request):
21
-    return render_to_response('browse-all.html', locals())
22
+def all(request, template_file='browse-all.html'):
23
+    return render_to_response(template_file, locals())

Laddar…
Avbryt
Spara