Kai Diefenbach avatar Kai Diefenbach committed f18dee8

Check in before changing to multi value

Comments (0)

Files changed (6)

lfs/cart/models.py

             else:
                 price = self.product.get_price_gross()
                 for property in self.properties.all():
-                    value = int(float(property.value))
-                    option = PropertyOption.objects.get(pk=value)
-                    price += option.price
+                    if property.property.is_select_field:
+                        try:
+                            option = PropertyOption.objects.get(pk=int(float(property.value)))
+                        except (PropertyOption.DoesNotExist, AttributeError):
+                            pass
+                        else:
+                            price += option.price
 
         return price * self.amount
 

lfs/cart/views.py

                 except IndexError:
                     continue
                 try:
-                    value = value.replace(",", ".")
-                    value = float(value)
-                except ValueError:
-                    value = 0.0
-                property = Property.objects.get(pk=property_id)
+                    property = Property.objects.get(pk=property_id)
+                except Property.DoesNotExist:
+                    continue
+
+                if property.is_float_field:
+                    try:
+                        value = float(value)
+                    except ValueError:
+                        value = 0.0
+                elif property.is_integer_field:
+                    try:
+                        value = int(value)
+                    except ValueError:
+                        value = 0
 
                 properties_dict[property_id] = unicode(value)
 
 
     # Update Amounts
     for item in cart.items():
-        amount = request.POST.get("amount-cart-item_%s" % item.id, 0)        
+        amount = request.POST.get("amount-cart-item_%s" % item.id, 0)
         try:
             amount = float(amount)
         except ValueError:
             amount = 1
-        
+
         if item.product.active_packing_unit:
             item.amount = lfs.catalog.utils.calculate_real_amount(item.product, float(amount))
         else:

lfs/catalog/models.py

 
     Can belong to several products, products can have several groups
 
-    Attributes:
-        - name
-          The name of the property group.
-        - products
+    **Attributes**:
+    
+    name
+        The name of the property group.
+    
+    products
           The assigned products of the property group.
     """
     name = models.CharField(blank=True, max_length=50)
 
     def __unicode__(self):
         return self.name
+        
+    def get_configurable_properties(self):
+        """Returns all configurable properties of the property group.
+        """
+        return self.properties.filter(configurable=True)
+
+    def get_filterable_properties(self):
+        """Returns all filterable properties of the property group.
+        """
+        return self.properties.filter(filterable=True)
 
 class Property(models.Model):
     """Represents a property of a product like color or size.
         return self.type in (PROPERTY_FLOAT_FIELD, PROPERTY_INTEGER_FIELD)
 
     @property
-    def is_decimal_field(self):
+    def is_float_field(self):
         return self.type == PROPERTY_FLOAT_FIELD
 
     @property
     """Stores the value resp. selected option of a product/property combination.
     This is some kind of EAV.
 
-    Attributes:
-        - product
-          The product for which the value is stored.
-        - parent_id
-          If the product is an variant this stores the parent id of it, if the
-          product is no variant it stores the id of the product itself. This is
-          just used to calculate the filters properly.
-        - property
-          The property for which the value is stored.
-        - value
-          The value for the product/property pair. Dependent of the property
-          type the value is either a number, a text or an id of an option.
+    *Attributes*:
+
+    product
+        The product for which the value is stored.
+
+    parent_id
+        If the product is an variant this stores the parent id of it, if the
+        product is no variant it stores the id of the product itself. This is
+        just used to calculate the filters properly.
+
+    property
+        The property for which the value is stored.
+
+    value
+        The value for the product/property pair. Dependent of the property
+        type the value is either a number, a text or an id of an option.
     """
     product = models.ForeignKey(Product, verbose_name=_(u"Product"), related_name="property_values")
     parent_id = models.IntegerField(blank=True, null=True)

lfs/catalog/utils.py

 def get_current_top_category(request, obj):
     """Returns the current top category of a product.
     """
-    
+
     if obj.__class__.__name__.lower() == "product":
         category = get_current_product_category(request, obj)
     else:
         category = obj
-    
+
     if category is None:
         return category
 
 
         # Transform to float for later sorting, see below
         property = properties_mapping[row[0]]
-        if property.is_decimal_field:
+        if property.is_float_field:
             value = float(row[1])
         else:
             value = row[1]
         # Generate filter
         temp = []
         for f in filters:
-            if len(f[1]) == 1:
+            if not isinstance(f[1], list):
                 temp.append("property_id='%s' AND value='%s'" % (f[0], f[1]))
             else:
                 temp.append("property_id='%s' AND value_as_float BETWEEN '%s' AND '%s'" % (f[0], f[1][0], f[1][1]))
             categories.extend(category.get_all_children())
         products = lfs.catalog.models.Product.objects.filter(
             active=True,
-            categories__in=categories, 
+            categories__in=categories,
             sub_type__in=[STANDARD_PRODUCT, PRODUCT_WITH_VARIANTS, CONFIGURABLE_PRODUCT]).distinct()
 
     if price_filter:
         amount += 1
 
     return amount
-    
+
 def calculate_packages(product, quantity):
     """
     """
     return math.ceil(quantity / product.packing_unit)
- 
+
 def calculate_real_amount(product, quantity):
     """
     """

lfs/manage/views/product/properties.py

     """
     product = get_object_or_404(Product, pk=product_id)
 
-    # Generate list of product property groups; used for enter value
-    product_property_groups = []
+    # Generate list of properties. For entering values.
+    configurables = []
+    filterables = []
     for property_group in product.property_groups.all():
         properties = []
         for property in property_group.properties.order_by("groupspropertiesrelation"):
                 "display_select_field" : property.type == PROPERTY_SELECT_FIELD,
             })
 
-        product_property_groups.append({
-            "id"   : property_group.id,
-            "name" : property_group.name,
-            "properties" : properties,
-        })
+        if property.configurable:
+            configurables.append({
+                "id"   : property_group.id,
+                "name" : property_group.name,
+                "properties" : properties,
+            })
+
+        if not property.configurable and property.filterable:
+            filterables.append({
+                "id"   : property_group.id,
+                "name" : property_group.name,
+                "properties" : properties,
+            })
 
     # Generate list of all property groups; used for group selection
-    product_property_group_ids = [p["id"] for p in product_property_groups]
+    product_property_group_ids = [p.id for p in product.property_groups.all()]
     shop_property_groups = []
     for property_group in PropertyGroup.objects.all():
 
 
     return render_to_string(template_name, RequestContext(request, {
         "product" : product,
-        "product_property_groups" : product_property_groups,
+        "filterables" : filterables,
+        "configurables" : configurables,
+        "product_property_groups" : product.property_groups.all(),
         "shop_property_groups" : shop_property_groups,
     }))
 

lfs/templates/manage/product/properties.html

 <form action="{% url lfs_update_product_properties product.id %}"
       method="post">
 
-    {% if product.is_configurable_product %}
-        <h2 class="heading-middle">{% trans 'Property Default Values' %}</h2>
-    {% else %}
-        <h2 class="heading-middle">{% trans 'Property Values' %}</h2>
-    {% endif %}
+    <h2 class="heading-middle">{% trans 'Default Values' %}</h2>
 
-    {% for property_group in product_property_groups %}
+    {% for property_group in configurables %}
         <div>
             <h3>
                 {{ property_group.name }}
                value="{% trans 'Update properties' %}" />
     </div>
 </form>
+<form action="{% url lfs_update_product_properties product.id %}"
+      method="post">
+    
+    <h2>{% trans "Filter Values" %}</h2>
+    {% for property_group in filterables %}
+        <div>
+            <h3>
+                {{ property_group.name }}
+            </h3>
+            {% for property in property_group.properties %}
+                <div>
+                    {% if property.display_text_field %}
+                        <div class="field">
+                            <div class="label">
+                                {{ property.name }}:
+                            </div>
+                            <input type="text"
+                                   name="property-{{ property.id }}"
+                                   value="{{ property.value }}" />
+                        </div>
+                    {% endif %}
+                    {% if property.display_select_field %}
+                        <div class="field">
+                            <div class="label">
+                                {{ property.name }}:
+                            </div>
+                            <select name="property-{{ property.id }}">
+                                {% for option in property.options %}
+                                    <option value="{{ option.id }}"
+                                            {% if option.selected %}selected="selected"{% endif %}>
+                                        {{ option.name }}
+                                    </option>
+                                {% endfor %}
+                            </select>
+                        </div>
+                    {% endif %}
+                </div>
+            {% endfor %}
+        </div>
+    {% endfor %}
+    <div class="lfs-buttons">
+        <input type="submit"
+               value="{% trans 'Update properties' %}" />
+    </div>
+</form>
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.