Beispiel #1
0
 def get_form(self, variation, data=None) -> ItemVariationForm:
     """
     Return the dict for one given variation. Variations are expected to be
     dictionaries in the format of Item.get_all_variations()
     """
     # Values are all dictionary ite
     values = [
         i[1] for i in sorted(
             [it for it in variation.items() if it[0] != 'variation'])
     ]
     if 'variation' in variation:
         form = ItemVariationForm(
             data,
             instance=variation['variation'],
             prefix=",".join([str(i.identity) for i in values]),
         )
     else:
         inst = ItemVariation(item=self.object)
         inst.item_id = self.object.identity
         inst.creation = True
         form = ItemVariationForm(
             data,
             instance=inst,
             prefix=",".join([str(i.identity) for i in values]),
         )
     form.values = values
     return form
Beispiel #2
0
    def _clean_value(self, value):
        # Build up a cache of variations having an ItemVariation object
        # For implementation details, see ItemVariation.get_all_variations()
        # which uses a very similar method
        all_variations = self.item.variations.all().prefetch_related("values")
        variations_cache = {
            var.to_variation_dict().identify(): var.identity
            for var in all_variations
        }

        cleaned_value = []

        # Wrap this in a transaction to prevent strange database state if we
        # get a ValidationError half-way through
        with transaction.atomic():
            for pk in value:
                if ":" in pk:
                    # A combination of PropertyValues was given

                    # Hash the combination in the same way as in our cache above
                    key = ",".join(
                        [pair.split(":")[1] for pair in sorted(pk.split(","))])

                    if key in variations_cache:
                        # An ItemVariation object already exists for this variation,
                        # so use this. (This might occur if the variation object was
                        # created _after_ the user loaded the form but _before_ he
                        # submitted it.)
                        cleaned_value.append(str(variations_cache[key]))
                        continue

                    # No ItemVariation present, create one!
                    var = ItemVariation()
                    var.item_id = self.item.identity
                    var.save()
                    # Add the values to the ItemVariation object
                    try:
                        var.add_values_from_string(pk)
                    except:
                        raise ValidationError(
                            self.error_messages['invalid_pk_value'],
                            code='invalid_pk_value',
                            params={'pk': value},
                        )
                    variations_cache[key] = var.identity
                    cleaned_value.append(str(var.identity))
                else:
                    # An ItemVariation id was given
                    cleaned_value.append(pk)
        return cleaned_value
Beispiel #3
0
    def _clean_value(self, value):
        # Build up a cache of variations having an ItemVariation object
        # For implementation details, see ItemVariation.get_all_variations()
        # which uses a very similar method
        all_variations = self.item.variations.all().prefetch_related("values")
        variations_cache = {
            var.to_variation_dict().identify(): var.identity for var in all_variations
        }

        cleaned_value = []

        # Wrap this in a transaction to prevent strange database state if we
        # get a ValidationError half-way through
        with transaction.atomic():
            for pk in value:
                if ":" in pk:
                    # A combination of PropertyValues was given

                    # Hash the combination in the same way as in our cache above
                    key = ",".join([pair.split(":")[1] for pair in sorted(pk.split(","))])

                    if key in variations_cache:
                        # An ItemVariation object already exists for this variation,
                        # so use this. (This might occur if the variation object was
                        # created _after_ the user loaded the form but _before_ he
                        # submitted it.)
                        cleaned_value.append(str(variations_cache[key]))
                        continue

                    # No ItemVariation present, create one!
                    var = ItemVariation()
                    var.item_id = self.item.identity
                    var.save()
                    # Add the values to the ItemVariation object
                    try:
                        var.add_values_from_string(pk)
                    except:
                        raise ValidationError(
                            self.error_messages['invalid_pk_value'],
                            code='invalid_pk_value',
                            params={'pk': value},
                        )
                    variations_cache[key] = var.identity
                    cleaned_value.append(str(var.identity))
                else:
                    # An ItemVariation id was given
                    cleaned_value.append(pk)
        return cleaned_value
Beispiel #4
0
 def get_form(self, variation, data=None) -> ItemVariationForm:
     """
     Return the dict for one given variation. Variations are expected to be
     dictionaries in the format of Item.get_all_variations()
     """
     # Values are all dictionary ite
     values = [i[1] for i in sorted([it for it in variation.items() if it[0] != 'variation'])]
     if 'variation' in variation:
         form = ItemVariationForm(
             data,
             instance=variation['variation'],
             prefix=",".join([str(i.identity) for i in values]),
         )
     else:
         inst = ItemVariation(item=self.object)
         inst.item_id = self.object.identity
         inst.creation = True
         form = ItemVariationForm(
             data,
             instance=inst,
             prefix=",".join([str(i.identity) for i in values]),
         )
     form.values = values
     return form