def add_products(request, category_id): """Adds products (passed via request body) to category with passed id. """ category = Category.objects.get(pk=category_id) for product_id in request.POST.keys(): if product_id.startswith("page") or product_id.startswith("filter") or \ product_id.startswith("keep-session") or product_id.startswith("action"): continue try: category.products.add(product_id) except IntegrityError: continue product = Product.objects.get(pk=product_id) product_changed.send(product) category_changed.send(category) html = [["#products-inline", products_inline(request, category_id, as_string=True)]] result = simplejson.dumps({ "html": html, "message": _(u"Selected products have been added to category.") }, cls=LazyEncoder) return HttpResponse(result)
def add_products(request, manufacturer_id): """Adds products (passed via request body) to category with passed id. """ manufacturer = Manufacturer.objects.get(pk=manufacturer_id) for product_id in request.POST.keys(): if product_id.startswith("manufacturer_page") or product_id.startswith("manufacturer_filter") or \ product_id.startswith("keep-session") or product_id.startswith("action"): continue try: product = Product.objects.get(pk=product_id) product.manufacturer = manufacturer product.save() product_changed.send(product) except Product.DoesNotExist: continue manufacturer_changed.send(manufacturer) html = [["#products-inline", products_inline(request, manufacturer_id, as_string=True)]] result = json.dumps({ "html": html, "message": _(u"Selected products have been assigned to manufacturer.") }, cls=LazyEncoder) return HttpResponse(result, content_type='application/json')
def add_property(request, product_id): """Adds a new property to the product with given product id. """ product = Product.objects.get(pk=product_id) property_form = PropertyForm(data=request.POST) if property_form.is_valid(): property = property_form.save(commit=False) property.title = property.name property.type = PROPERTY_SELECT_FIELD property.local = True # it doesn't make sense to filter by local properties as every local # property has an own id. Maybe we can do this with an grouping id or # something like that property.filterable = False property.save() product_property = ProductsPropertiesRelation(product=product, property=property, position=999) product_property.save() # Refresh positions for i, product_property in enumerate(product.productsproperties.all()): product_property.position = i product_property.save() product_changed.send(product) pid = product.get_parent().pk invalidate_cache_group_id("properties-%s" % pid) html = [["#variants", manage_variants(request, product_id, as_string=True)]] result = json.dumps({"html": html, "message": _(u"Property has been added.")}, cls=LazyEncoder) return HttpResponse(result, content_type="application/json")
def add_property_option(request, product_id): """Adds a new option to the property with given property id. NOTE: The reason why to pass the product id here is to be able to redirect to the product. Properties can belong to more than one product. TODO: Do this with REFERER """ property_option_form = PropertyOptionForm(data=request.POST) if property_option_form.is_valid(): names = request.POST.get("name").split(",") position = 999 property_id = request.POST.get("property_id") for name in names: property_option = PropertyOption(name=name) property_option.property_id = property_id property_option.position = position property_option.save() position += 1 # Refresh positions for i, option in enumerate(PropertyOption.objects.filter(property=property_id)): option.position = i option.save() product_changed.send(Product.objects.get(pk=product_id)) html = [["#variants", manage_variants(request, product_id, as_string=True)]] result = simplejson.dumps({ "html": html, "message": _(u"Option has been added."), }, cls=LazyEncoder) return HttpResponse(result)
def update_attachments(request, product_id): """Saves/deletes attachments with given ids (passed by request body). """ product = lfs_get_object_or_404(Product, pk=product_id) action = request.POST.get("action") message = _(u"Attachment has been updated.") if action == "delete": message = _(u"Attachment has been deleted.") for key in request.POST.keys(): if key.startswith("delete-"): try: id = key.split("-")[1] attachment = ProductAttachment.objects.get(pk=id).delete() except (IndexError, ObjectDoesNotExist): pass elif action == "update": message = _(u"Attachment has been updated.") for attachment in product.attachments.all(): attachment.title = request.POST.get("title-%s" % attachment.id) attachment.position = request.POST.get("position-%s" % attachment.id) attachment.description = request.POST.get("description-%s" % attachment.id) attachment.save() # Refresh positions for i, attachment in enumerate(product.attachments.all()): attachment.position = (i + 1) * 10 attachment.save() product_changed.send(product, request=request) html = [["#attachments", manage_attachments(request, product_id, as_string=True)]] result = simplejson.dumps({"html": html, "message": message}, cls=LazyEncoder) return HttpResponse(result, mimetype="application/json")
def remove_products(request, category_id): """Removes product (passed via request body) from category with passed id. """ category = Category.objects.get(pk=category_id) for product_id in request.POST.keys(): if product_id.startswith("page") or product_id.startswith("filter") or \ product_id.startswith("keep-session") or product_id.startswith("action"): continue product = Product.objects.get(pk=product_id) product_changed.send(product) category.products.remove(product_id) category_changed.send(category) html = [["#products-inline", products_inline(request, category_id, as_string=True)]] result = json.dumps({ "html": html, "message": _(u"Selected products have been removed from category.") }, cls=LazyEncoder) return HttpResponse(result, mimetype='application/json')
def add_accessories(request, product_id): """Adds passed accessories to product with passed id. """ parent_product = Product.objects.get(pk=product_id) for temp_id in request.POST.keys(): if temp_id.startswith("product") is False: continue temp_id = temp_id.split("-")[1] accessory = Product.objects.get(pk=temp_id) product_accessory = ProductAccessories(product=parent_product, accessory=accessory) product_accessory.save() _update_positions(parent_product) product_changed.send(parent_product) html = [["#accessories-inline", manage_accessories_inline(request, product_id, as_string=True)]] result = json.dumps({ "html": html, "message": _(u"Accessories have been added.") }, cls=LazyEncoder) return HttpResponse(result, content_type='application/json')
def remove_products(request, manufacturer_id): """Removes product (passed via request body) from category with passed id. """ manufacturer = Manufacturer.objects.get(pk=manufacturer_id) for product_id in request.POST.keys(): if ( product_id.startswith("manufacturer_page") or product_id.startswith("manufacturer_filter") or product_id.startswith("keep-session") or product_id.startswith("action") ): continue product = Product.objects.get(pk=product_id) product.manufacturer = None product.save() product_changed.send(product) manufacturer_changed.send(manufacturer) html = [["#products-inline", products_inline(request, manufacturer_id, as_string=True)]] result = simplejson.dumps( {"html": html, "message": _(u"Selected products are no longer assigned to manufacturer.")}, cls=LazyEncoder ) return HttpResponse(result, mimetype="application/json")
def remove_accessories(request, product_id): """Removes passed accessories from product with passed id. """ parent_product = Product.objects.get(pk=product_id) if request.POST.get("action") == "remove": for temp_id in request.POST.keys(): if temp_id.startswith("accessory") is False: continue temp_id = temp_id.split("-")[1] accessory = Product.objects.get(pk=temp_id) product_accessory = ProductAccessories.objects.filter(product=parent_product, accessory=accessory) product_accessory.delete() _update_positions(parent_product) product_changed.send(parent_product) html = [["#accessories-inline", manage_accessories_inline(request, product_id, as_string=True)]] result = json.dumps({ "html": html, "message": _(u"Accessories have been removed.") }, cls=LazyEncoder) else: for temp_id in request.POST.keys(): if temp_id.startswith("quantity") is False: continue temp_id = temp_id.split("-")[1] accessory = Product.objects.get(pk=temp_id) product_accessory = ProductAccessories.objects.get(product=parent_product, accessory=accessory) # Update quantity quantity = request.POST.get("quantity-%s" % temp_id) product_accessory.quantity = quantity # Update position position = request.POST.get("position-%s" % temp_id) product_accessory.position = position product_accessory.save() product_changed.send(product_accessory.product) _update_positions(parent_product) html = [["#accessories-inline", manage_accessories_inline(request, product_id, as_string=True)]] result = json.dumps({ "html": html, "message": _(u"Accessories have been updated.") }, cls=LazyEncoder) return HttpResponse(result, content_type='application/json')
def update_images(request, product_id): """Saves/deletes images with given ids (passed by request body). """ product = lfs_get_object_or_404(Product, pk=product_id) action = request.POST.get("action") if action == "delete": message = _(u"Images has been deleted.") for key in request.POST.keys(): if key.startswith("delete-"): try: id = key.split("-")[1] image = Image.objects.get(pk=id).delete() except (IndexError, ObjectDoesNotExist): pass elif action == "update": message = _(u"Images has been updated.") for key, value in request.POST.items(): if key.startswith("title-"): id = key.split("-")[1] try: image = Image.objects.get(pk=id) except ObjectDoesNotExist: pass else: image.title = value image.save() elif key.startswith("position-"): try: id = key.split("-")[1] image = Image.objects.get(pk=id) except (IndexError, ObjectDoesNotExist): pass else: image.position = value image.save() # Refresh positions for i, image in enumerate(product.images.all()): image.position = (i + 1) * 10 image.save() product_changed.send(product, request=request) html = [["#images", manage_images(request, product_id, as_string=True)]] result = simplejson.dumps({ "html": html, "message": message, }, cls=LazyEncoder) return HttpResponse(result, mimetype='application/json')
def handle_upload(request, product_id): """ Handles upload of new DigitalAsset """ product = lfs_get_object_or_404(Product, pk=product_id) if request.method == "POST": for file_content in request.FILES.getlist("files"): digiproduct = DigitalAsset(file=file_content, product=product) digiproduct.file.save(file_content.name, file_content, save=True) product_changed.send(product, request=request) return manage_digital_products(request, product_id)
def edit_sub_type(request, product_id): """Edits the sub type of the variant with given product slug. """ product = Product.objects.get(pk=product_id) form = DisplayTypeForm(data=request.POST) if form.is_valid(): product.variants_display_type = request.POST.get("variants_display_type") product.save() # Send a signal to update cache product_changed.send(product) return HttpResponse(manage_variants(request, product_id))
def add_attachment(request, product_id): """Adds an attachment to product with passed product_id. """ product = lfs_get_object_or_404(Product, pk=product_id) if request.method == "POST": for file_content in request.FILES.getlist("files[]"): attachment = ProductAttachment(product=product, title=file_content.name[:50]) attachment.file.save(file_content.name, file_content, save=True) # Refresh positions for i, attachment in enumerate(product.attachments.all()): attachment.position = (i + 1) * 10 attachment.save() product_changed.send(product, request=request) return manage_attachments(request, product_id)
def add_image(request, product_id): """Adds an image to product with passed product_id. """ product = lfs_get_object_or_404(Product, pk=product_id) if request.method == "POST": for file_content in request.FILES.values(): image = Image(content=product, title=file_content.name) image.image.save(file_content.name, file_content, save=True) # Refresh positions for i, image in enumerate(product.images.all()): image.position = i+1 image.save() product_changed.send(product, request=request) return HttpResponse(manage_images(request, product_id, as_string=True))
def move_image(request, id): """Moves the image with passed id up or down. **Parameters:** id The id of the image which should be edited. **Query String:** direction The direction in which the image should be moved. One of 0 (up) or 1 (down). **Permission:** edit (of the belonging content object) """ image = Image.objects.get(pk=id) product = image.content direction = request.GET.get("direction", 0) if direction == "1": image.position += 15 else: image.position -= 15 if image.position < 0: image.position = 10 image.save() # Refresh positions for i, image in enumerate(product.images.all()): image.position = (i + 1) * 10 image.save() product_changed.send(product, request=request) html = [["#images", manage_images(request, product.id, as_string=True)]] result = simplejson.dumps({ "html": html, }, cls=LazyEncoder) return HttpResponse(result, mimetype='application/json')
def edit_sub_type(request, product_id): """Edits the sub type of the variant with given product slug. """ product = Product.objects.get(pk=product_id) form = DisplayTypeForm(data=request.POST) if form.is_valid(): product.variants_display_type = request.POST.get("variants_display_type") product.save() # Send a signal to update cache product_changed.send(product) html = (("#variants", manage_variants(request, product_id, as_string=True)),) result = json.dumps({"html": html, "message": _(u"Sub type has been saved.")}, cls=LazyEncoder) return HttpResponse(result, content_type="application/json")
def update_category_variant(request, product_id): """ Updates the category variant of the product with passed product_id. """ product = Product.objects.get(pk=product_id) form = CategoryVariantForm(instance=product, data=request.POST) if form.is_valid(): form.save() # Send a signal to update cache product_changed.send(product) html = (("#variants", manage_variants(request, product_id, as_string=True)),) result = json.dumps({"html": html, "message": _(u"Category variant has been saved.")}, cls=LazyEncoder) return HttpResponse(result, content_type="application/json")
def update_default_variant(request, product_id): """Updates the default variant of the product with passed product_id """ product = Product.objects.get(pk=product_id) form = DefaultVariantForm(instance=product, data=request.POST) if form.is_valid(): form.save() # Send a signal to update cache product_changed.send(product) result = simplejson.dumps({ "html": manage_variants(request, product_id, as_string=True), "message": _(u"Default variant has been saved."), }, cls=LazyEncoder) return HttpResponse(result)
def remove_products(request, category_id): """Removes product (passed via request body) from category with passed id. """ category = Category.objects.get(pk=category_id) for product_id in request.POST.keys(): if product_id.startswith("page") or product_id.startswith("filter") or \ product_id.startswith("keep-session"): continue product = Product.objects.get(pk=product_id) product_changed.send(product) category.products.remove(product_id) category_changed.send(category) inline = products_inline(request, category_id) return HttpResponse(inline)
def add_property_option(request, product_id): """Adds a new option to the property with given property id. NOTE: The reason why to pass the product id here is to be able to redirect to the product. Properties can belong to more than one product. TODO: Do this with REFERER """ property_option_form = PropertyOptionForm(data=request.POST) if property_option_form.is_valid(): names = request.POST.get("name").split(",") position = 999 property_id = request.POST.get("property_id") for name in names: property_option = PropertyOption(name=name) property_option.property_id = property_id property_option.position = position property_option.save() position += 1 # Refresh positions for i, option in enumerate(PropertyOption.objects.filter(property=property_id)): option.position = i option.save() message = _(u'Option has been added.') else: message = _(u'Invalid data. Correct it and try again.') product = Product.objects.get(pk=product_id) product_changed.send(product) pid = product.get_parent().pk invalidate_cache_group_id('properties-%s' % pid) html = [["#variants", manage_variants(request, product_id, as_string=True)]] result = json.dumps({ "html": html, "message": message, }, cls=LazyEncoder) return HttpResponse(result, mimetype='application/json')
def delete_property_option(request, product_id, option_id): """Deletes property option with passed option id. """ try: property_option = PropertyOption.objects.get(pk=option_id) product = Product.objects.get(pk=product_id) except ObjectDoesNotExist: pass else: property_option.delete() product_changed.send(product) html = (("#variants", manage_variants(request, product_id, as_string=True)),) result = simplejson.dumps({ "html": html, "message": _(u"Property has been deleted."), "close-dialog": True, }, cls=LazyEncoder) return HttpResponse(result)
def delete_property_option(request, product_id, option_id): """Deletes property option with passed option id. """ try: property_option = PropertyOption.objects.get(pk=option_id) product = Product.objects.get(pk=product_id) except ObjectDoesNotExist: pass else: property_option.delete() product_changed.send(product) pid = product.get_parent().pk invalidate_cache_group_id("properties-%s" % pid) html = (("#variants", manage_variants(request, product_id, as_string=True)),) result = json.dumps( {"html": html, "message": _(u"Property has been deleted."), "close-dialog": True}, cls=LazyEncoder ) return HttpResponse(result, content_type="application/json")
def delete_property(request, product_id, property_id): """Deletes property with passed property id. """ try: property = Property.objects.get(pk=property_id) product = Product.objects.get(pk=product_id) except ObjectDoesNotExist: pass else: property.delete() product_changed.send(product) pid = product.get_parent().pk invalidate_cache_group_id('properties-%s' % pid) html = (("#variants", manage_variants(request, product_id, as_string=True)),) result = simplejson.dumps({ "html": html, "message": _(u"Property has been deleted."), "close-dialog": True, }, cls=LazyEncoder) return HttpResponse(result)
def update_digiproducts(request, product_id): """ Just to delete digital products with given ids (passed by request body). Maybe later have some description. """ product = lfs_get_object_or_404(Product, pk=product_id) action = request.POST.get("action") message = _(u"Digital Product has been updated.") if action == "delete": message = _(u"Digital Product has been deleted.") for key in request.POST.keys(): if key.startswith("delete-"): try: id = key.split("-")[1] digiproduct = DigitalAsset.objects.get(pk=id).delete() except (IndexError, ObjectDoesNotExist): pass if action == 'update_donation_mode': product = lfs_get_object_or_404(Product, pk=product_id) message = _(u"Donation mode has been updated.") DigitalAsset.objects.filter(product=product).update( donation_mode=request.POST.get('donation_mode', False), minimum_price=request.POST.get('minimum_price', '1.0'), suggested_price=request.POST.get('suggested_price', '1.0'), ) product_changed.send(product, request=request) html = [["#lfs_downloads", manage_digital_products(request, product_id, as_string=True)]] result = simplejson.dumps({ "html": html, "message": message, }, cls=LazyEncoder) return HttpResponse(result)
def update_variants(request, product_id): """Updates/Deletes variants with passed ids (via request body) dependent on given action (also via request body). """ product = lfs_get_object_or_404(Product, pk=product_id) action = request.POST.get("action") if action == "delete": message = _(u"Variants have been deleted.") for key in request.POST.keys(): if key.startswith("delete-"): try: id = key.split("-")[1] variant = Product.objects.get(pk=id) except (IndexError, ObjectDoesNotExist): continue else: if product.default_variant == variant: product.default_variant = None product.save() variant.delete() elif action == "update": message = _(u"Variants have been saved.") for key, value in request.POST.items(): if key.startswith("variant-"): id = key.split("-")[1] try: variant = Product.objects.get(pk=id) except ObjectDoesNotExist: continue else: for name in ("slug", "sku", "price"): value = request.POST.get("%s-%s" % (name, id)) if value != "": setattr(variant, name, value) # name variant.name = request.POST.get("name-%s" % id) # active active = request.POST.get("active-%s" % id) if active: variant.active = True else: variant.active = False # active attributes for name in ("active_price", "active_sku", "active_name"): value = request.POST.get("%s-%s" % (name, id)) if value: setattr(variant, name, True) else: setattr(variant, name, False) # position position = request.POST.get("position-%s" % id) try: variant.variant_position = int(position) except ValueError: variant.variant_position = 10 # default variant try: product.default_variant_id = int( request.POST.get("default_variant")) except TypeError: pass else: product.save() variant.save() elif key.startswith("property"): # properties are marshalled as: property-variant_id|property_id temp = key.split("-")[1] variant_id, property_id = temp.split("|") variant = Product.objects.get(pk=variant_id) try: ppv = variant.property_values.get( property_id=property_id, type=PROPERTY_VALUE_TYPE_VARIANT) except ProductPropertyValue.DoesNotExist: # TODO: When creating new propertys (local or global), they are not copied onto existing variants. continue ppv.value = value ppv.save() # Refresh variant positions for i, variant in enumerate(product.variants.order_by("variant_position")): variant.variant_position = (i + 1) * 10 variant.save() # Send a signal to update cache product_changed.send(product) html = ( ("#variants", manage_variants(request, product_id, as_string=True)), ("#selectable-products-inline", _selectable_products_inline(request, product)), ) result = simplejson.dumps({ "html": html, "message": message, }, cls=LazyEncoder) return HttpResponse(result)
def update_variants(request, product_id): """Updates/Deletes variants with passed ids (via request body) dependent on given action (also via request body). """ product = lfs_get_object_or_404(Product, pk=product_id) message = '' action = request.POST.get("action") if action == "delete": message = _(u"Variants have been deleted.") for key in request.POST.keys(): if key.startswith("delete-"): try: prop_id = key.split("-")[1] variant = Product.objects.get(pk=prop_id) except (IndexError, ObjectDoesNotExist): continue else: if product.default_variant == variant: product.default_variant = None product.save() variant.delete() elif action == "update": # TODO: change all of these to formsets or something that will allow for error hangling/messages message = _(u"Variants have been saved.") for key, value in request.POST.items(): if key.startswith("variant-"): prop_id = key.split("-")[1] try: variant = Product.objects.get(pk=prop_id) except ObjectDoesNotExist: continue else: for name in ("sku", "price"): value = request.POST.get("%s-%s" % (name, prop_id)) if value != "": if name == 'price': value = float(value) setattr(variant, name, value) # handle slug - ensure it is unique slug = request.POST.get("slug-%s" % prop_id) if variant.slug != slug: counter = 1 new_slug = slug[:80] while Product.objects.exclude(pk=variant.pk).filter( slug=new_slug).exists(): new_slug = '%s-%s' % (slug[:( 79 - len(str(counter)))], counter) counter += 1 variant.slug = new_slug # name variant.name = request.POST.get("name-%s" % prop_id) # active active = request.POST.get("active-%s" % prop_id) if active: variant.active = True else: variant.active = False # active attributes for name in ("active_price", "active_sku", "active_name"): value = request.POST.get("%s-%s" % (name, prop_id)) if value: setattr(variant, name, True) else: setattr(variant, name, False) # position position = request.POST.get("position-%s" % prop_id) try: variant.variant_position = int(position) except ValueError: variant.variant_position = 10 # default variant try: product.default_variant_id = int( request.POST.get("default_variant")) except TypeError: pass else: product.save() variant.save() elif key.startswith("property"): # properties are marshalled as: property-variant_id|property_id temp = key.split("-")[1] variant_id, property_id = temp.split("|") try: variant = Product.objects.get(pk=variant_id) except Product.DoesNotExist: continue prop = Property.objects.get(pk=property_id) ppv = None ppv_filterable = None try: ppv = ProductPropertyValue.objects.get( product=variant, property_id=property_id, type=PROPERTY_VALUE_TYPE_VARIANT) except ProductPropertyValue.DoesNotExist: pass if prop.filterable: # it is possible that multiple values are selected for filter ppv_filterables = ProductPropertyValue.objects.filter( product=variant, property_id=property_id, type=PROPERTY_VALUE_TYPE_FILTER) if value != '': is_changed = True if not ppv: ppv = ProductPropertyValue.objects.create( product=variant, property_id=property_id, type=PROPERTY_VALUE_TYPE_VARIANT, value=value) else: is_changed = ppv.value != value ppv.value = value ppv.save() if prop.filterable and is_changed: ppv_filterables.delete() ProductPropertyValue.objects.create( product=variant, property_id=property_id, value=value, type=PROPERTY_VALUE_TYPE_FILTER) elif ppv: ppv.delete() ppv_filterables.delete() # Refresh variant positions for i, variant in enumerate(product.variants.order_by("variant_position")): variant.variant_position = (i + 1) * 10 variant.save() # Send a signal to update cache product_changed.send(product) pid = product.get_parent().pk invalidate_cache_group_id('properties-%s' % pid) html = ( ("#variants", manage_variants(request, product_id, as_string=True)), ("#selectable-products-inline", _selectable_products_inline(request, product)), ) result = json.dumps({ "html": html, "message": message, }, cls=LazyEncoder) return HttpResponse(result, content_type='application/json')
def update_variants(request, product_id): """Updates/Deletes variants with passed ids (via request body) dependent on given action (also via request body). """ product = lfs_get_object_or_404(Product, pk=product_id) action = request.POST.get("action") if action == "delete": for key in request.POST.keys(): if key.startswith("delete-"): try: id = key.split("-")[1] variant = Product.objects.get(pk=id) except (IndexError, ObjectDoesNotExist): continue else: variant.delete() elif action == "update": for key, value in request.POST.items(): if key.startswith("variant-"): id = key.split("-")[1] try: variant = Product.objects.get(pk=id) except ObjectDoesNotExist: continue else: for name in ("slug", "sku", "price"): value = request.POST.get("%s-%s" % (name, id)) if value != "": setattr(variant, name, value) # name variant.name = request.POST.get("name-%s" % id) # active active = request.POST.get("active-%s" % id) if active: variant.active = True else: variant.active = False # position position = request.POST.get("position-%s" % id) try: variant.variant_position = int(position) except ValueError: variant.variant_position = 10 variant.save() elif key.startswith("property"): # properties are marshalled as: property-variant_id|property_id try: temp = key.split("-")[1] variant_id, property_id = temp.split("|") variant = Product.objects.get(pk=variant_id) property = variant.get_option(property_id) property.option_id = value except (AttributeError, IndexError, ObjectDoesNotExist): continue else: property.save() # Refresh variant positions for i, variant in enumerate(product.variants.order_by("variant_position")): variant.variant_position = (i + 1) * 10 variant.save() # Send a signal to update cache product_changed.send(product) from lfs.manage.views.product.product import selectable_products_inline result = simplejson.dumps({ "properties": manage_variants(request, product_id, as_string=True), }) return HttpResponse(result)
product = lfs_get_object_or_404(Product, pk=product_id) if request.method == "POST": for file_content in request.FILES.getlist("file"): image = Image(content=product, title=file_content.name) try: image.image.save(file_content.name, file_content, save=True) except Exception, e: logger.info("Upload image: %s %s" % (file_content.name, e)) continue # Refresh positions for i, image in enumerate(product.images.all()): image.position = (i + 1) * 10 image.save() product_changed.send(product, request=request) result = simplejson.dumps({"name": file_content.name, "type": "image/jpeg", "size": "123456789"}) return HttpResponse(result, mimetype='application/json') @permission_required("core.manage_shop") def update_images(request, product_id): """Saves/deletes images with given ids (passed by request body). """ product = lfs_get_object_or_404(Product, pk=product_id) action = request.POST.get("action") if action == "delete": message = _(u"Images has been deleted.") for key in request.POST.keys():
def update_images(request, product_id): """Saves/deletes images with given ids (passed by request body). """ product = lfs_get_object_or_404(Product, pk=product_id) action = request.POST.get("action") if action == "delete": message = _(u"Images has been deleted.") for key in request.POST.keys(): if key.startswith("delete-"): try: id = key.split("-")[1] image = Image.objects.get(pk=id).delete() except (IndexError, ObjectDoesNotExist): pass elif action == "update": message = _(u"Images has been updated.") for key, value in request.POST.items(): if key.startswith("title-"): id = key.split("-")[1] try: image = Image.objects.get(pk=id) except ObjectDoesNotExist: pass else: image.title = value image.save() elif key.startswith("alt-"): id = key.split("-")[1] try: image = Image.objects.get(pk=id) except ObjectDoesNotExist: pass else: image.alt = value image.save() elif key.startswith("position-"): try: id = key.split("-")[1] image = Image.objects.get(pk=id) except (IndexError, ObjectDoesNotExist): pass else: image.position = value image.save() # Refresh positions for i, image in enumerate(product.images.all()): image.position = (i + 1) * 10 image.save() product_changed.send(product, request=request) html = [["#images-list", list_images(request, product_id, as_string=True)]] result = json.dumps({ "html": html, "message": message, }, cls=LazyEncoder) return HttpResponse(result, content_type='application/json')
def on_completion(self, uploaded_file, request): digiproduct = DigitalAsset(file=uploaded_file, product=self.product) digiproduct.save() product_changed.send(self.product, request=request)
product = lfs_get_object_or_404(Product, pk=product_id) if request.method == "POST": for file_content in request.FILES.getlist("file"): image = Image(content=product, title=file_content.name) try: image.image.save(file_content.name, file_content, save=True) except Exception, e: logger.info("Upload image: %s %s" % (file_content.name, e)) continue # Refresh positions for i, image in enumerate(product.images.all()): image.position = (i + 1) * 10 image.save() product_changed.send(product, request=request) result = json.dumps({ "name": file_content.name, "type": "image/jpeg", "size": "123456789" }) return HttpResponse(result, content_type='application/json') @permission_required("core.manage_shop") def update_images(request, product_id): """Saves/deletes images with given ids (passed by request body). """ product = lfs_get_object_or_404(Product, pk=product_id)
def update_variants(request, product_id): """Updates/Deletes variants with passed ids (via request body) dependent on given action (also via request body). """ product = lfs_get_object_or_404(Product, pk=product_id) action = request.POST.get("action") if action == "delete": message = _(u"Variants have been deleted.") for key in request.POST.keys(): if key.startswith("delete-"): try: id = key.split("-")[1] variant = Product.objects.get(pk=id) except (IndexError, ObjectDoesNotExist): continue else: if product.default_variant == variant: product.default_variant = None product.save() variant.delete() elif action == "update": message = _(u"Variants have been saved.") for key, value in request.POST.items(): if key.startswith("variant-"): id = key.split("-")[1] try: variant = Product.objects.get(pk=id) except ObjectDoesNotExist: continue else: for name in ("slug", "sku", "price"): value = request.POST.get("%s-%s" % (name, id)) if value != "": setattr(variant, name, value) # name variant.name = request.POST.get("name-%s" % id) # active active = request.POST.get("active-%s" % id) if active: variant.active = True else: variant.active = False # active attributes for name in ("active_price", "active_sku", "active_name"): value = request.POST.get("%s-%s" % (name, id)) if value: setattr(variant, name, True) else: setattr(variant, name, False) # position position = request.POST.get("position-%s" % id) try: variant.variant_position = int(position) except ValueError: variant.variant_position = 10 # default variant try: product.default_variant_id = int(request.POST.get("default_variant")) except TypeError: pass else: product.save() variant.save() elif key.startswith("property"): # properties are marshalled as: property-variant_id|property_id try: temp = key.split("-")[1] variant_id, property_id = temp.split("|") variant = Product.objects.get(pk=variant_id) property = variant.get_option(property_id) property.option_id = value except (AttributeError, IndexError, ObjectDoesNotExist): continue else: property.save() # Refresh variant positions for i, variant in enumerate(product.variants.order_by("variant_position")): variant.variant_position = (i + 1) * 10 variant.save() # Send a signal to update cache product_changed.send(product) html = ( ("#variants", manage_variants(request, product_id, as_string=True)), ("#selectable-products-inline", _selectable_products_inline(request, product)), ) result = simplejson.dumps({ "html": html, "message": message, }, cls=LazyEncoder) return HttpResponse(result)
def update_variants(request, product_id): """Updates/Deletes variants with passed ids (via request body) dependent on given action (also via request body). """ product = lfs_get_object_or_404(Product, pk=product_id) action = request.POST.get("action") if action == "delete": for key in request.POST.keys(): if key.startswith("delete-"): try: id = key.split("-")[1] variant = Product.objects.get(pk=id) except (IndexError, ObjectDoesNotExist): continue else: variant.delete() elif action == "update": for key, value in request.POST.items(): if key.startswith("variant-"): id = key.split("-")[1] try: variant = Product.objects.get(pk=id) except ObjectDoesNotExist: continue else: for name in ("slug", "sku", "price"): value = request.POST.get("%s-%s" % (name, id)) if value != "": setattr(variant, name, value) # name variant.name = request.POST.get("name-%s" % id) # active active = request.POST.get("active-%s" % id) if active: variant.active = True else: variant.active = False # position position = request.POST.get("position-%s" % id) try: variant.variant_position = int(position) except ValueError: variant.variant_position = 10 variant.save() elif key.startswith("property"): # properties are marshalled as: property-variant_id|property_id try: temp = key.split("-")[1] variant_id, property_id = temp.split("|") variant = Product.objects.get(pk=variant_id) property = variant.get_option(property_id) property.option_id = value except (AttributeError, IndexError, ObjectDoesNotExist): continue else: property.save() # Refresh variant positions for i, variant in enumerate(product.variants.order_by("variant_position")): variant.variant_position = (i+1) * 10 variant.save() # Send a signal to update cache product_changed.send(product) from lfs.manage.views.product.product import selectable_products_inline result = simplejson.dumps({ "properties" : manage_variants(request, product_id, as_string=True), }) return HttpResponse(result)
def update_variants(request, product_id): """Updates/Deletes variants with passed ids (via request body) dependent on given action (also via request body). """ product = lfs_get_object_or_404(Product, pk=product_id) message = "" action = request.POST.get("action") if action == "delete": message = _(u"Variants have been deleted.") for key in request.POST.keys(): if key.startswith("delete-"): try: prop_id = key.split("-")[1] variant = Product.objects.get(pk=prop_id) except (IndexError, ObjectDoesNotExist): continue else: if product.default_variant == variant: product.default_variant = None product.save() variant.delete() elif action == "update": # TODO: change all of these to formsets or something that will allow for error hangling/messages message = _(u"Variants have been saved.") for key, value in request.POST.items(): if key.startswith("variant-"): prop_id = key.split("-")[1] try: variant = Product.objects.get(pk=prop_id) except ObjectDoesNotExist: continue else: for name in ("sku", "price"): value = request.POST.get("%s-%s" % (name, prop_id)) if value != "": if name == "price": value = float(value) setattr(variant, name, value) # handle slug - ensure it is unique slug = request.POST.get("slug-%s" % prop_id) if variant.slug != slug: counter = 1 new_slug = slug[:80] while Product.objects.exclude(pk=variant.pk).filter(slug=new_slug).exists(): new_slug = "%s-%s" % (slug[: (79 - len(str(counter)))], counter) counter += 1 variant.slug = new_slug # name variant.name = request.POST.get("name-%s" % prop_id) # active active = request.POST.get("active-%s" % prop_id) if active: variant.active = True else: variant.active = False # active attributes for name in ("active_price", "active_sku", "active_name"): value = request.POST.get("%s-%s" % (name, prop_id)) if value: setattr(variant, name, True) else: setattr(variant, name, False) # position position = request.POST.get("position-%s" % prop_id) try: variant.variant_position = int(position) except ValueError: variant.variant_position = 10 # default variant try: product.default_variant_id = int(request.POST.get("default_variant")) except TypeError: pass else: product.save() variant.save() elif key.startswith("property"): # properties are marshalled as: property-variant_id|property_group_id|property_id temp = key.split("-")[1] variant_id, property_group_id, property_id = temp.split("|") if property_group_id == "0": # local properties are not bound to property groups property_group_id = None try: variant = Product.objects.get(pk=variant_id) except Product.DoesNotExist: continue prop = Property.objects.get(pk=property_id) ppv = None try: ppv = ProductPropertyValue.objects.get( product=variant, property_id=property_id, property_group_id=property_group_id, type=PROPERTY_VALUE_TYPE_VARIANT, ) except ProductPropertyValue.DoesNotExist: pass if prop.filterable: # it is possible that multiple values are selected for filter ppv_filterables = ProductPropertyValue.objects.filter( product=variant, property_group_id=property_group_id, property_id=property_id, type=PROPERTY_VALUE_TYPE_FILTER, ) if value != "": is_changed = True if not ppv: ppv = ProductPropertyValue.objects.create( product=variant, property_group_id=property_group_id, property_id=property_id, type=PROPERTY_VALUE_TYPE_VARIANT, value=value, ) else: is_changed = ppv.value != value ppv.value = value ppv.save() if prop.filterable and is_changed: ppv_filterables.delete() ProductPropertyValue.objects.create( product=variant, property_group_id=property_group_id, property_id=property_id, value=value, type=PROPERTY_VALUE_TYPE_FILTER, ) elif ppv: ppv.delete() ppv_filterables.delete() # Refresh variant positions for i, variant in enumerate(product.variants.order_by("variant_position")): variant.variant_position = (i + 1) * 10 variant.save() # Send a signal to update cache product_changed.send(product) pid = product.get_parent().pk invalidate_cache_group_id("properties-%s" % pid) html = ( ("#variants", manage_variants(request, product_id, as_string=True)), ("#selectable-products-inline", _selectable_products_inline(request, product)), ) result = json.dumps({"html": html, "message": message}, cls=LazyEncoder) return HttpResponse(result, content_type="application/json")
def remove_accessories(request, product_id): """Removes passed accessories from product with passed id. """ parent_product = Product.objects.get(pk=product_id) if request.POST.get("action") == "remove": for temp_id in request.POST.keys(): if temp_id.startswith("accessory") == False: continue temp_id = temp_id.split("-")[1] accessory = Product.objects.get(pk=temp_id) product_accessory = ProductAccessories.objects.filter( product=parent_product, accessory=accessory) product_accessory.delete() _update_positions(parent_product) product_changed.send(parent_product) html = [[ "#accessories-inline", manage_accessories_inline(request, product_id, as_string=True) ]] result = json.dumps( { "html": html, "message": _(u"Accessories have been removed.") }, cls=LazyEncoder) else: for temp_id in request.POST.keys(): if temp_id.startswith("quantity") == False: continue temp_id = temp_id.split("-")[1] accessory = Product.objects.get(pk=temp_id) product_accessory = ProductAccessories.objects.get( product=parent_product, accessory=accessory) # Update quantity quantity = request.POST.get("quantity-%s" % temp_id) product_accessory.quantity = quantity # Update position position = request.POST.get("position-%s" % temp_id) product_accessory.position = position product_accessory.save() product_changed.send(product_accessory.product) _update_positions(parent_product) html = [[ "#accessories-inline", manage_accessories_inline(request, product_id, as_string=True) ]] result = json.dumps( { "html": html, "message": _(u"Accessories have been updated.") }, cls=LazyEncoder) return HttpResponse(result, content_type='application/json')