Commit c43a83e6 authored by Patrick's avatar Patrick

Manual change of "is into offer" need to be disabled if the customer order

quantity of a product is limited by the stock. In this case, the stock
determines if the product is into offer, not a manual change of the switch.
parent c42741aa
...@@ -145,30 +145,33 @@ class Product(TranslatableModel): ...@@ -145,30 +145,33 @@ class Product(TranslatableModel):
def get_is_into_offer(self): def get_is_into_offer(self):
from django.contrib.admin.templatetags.admin_list import _boolean_icon from django.contrib.admin.templatetags.admin_list import _boolean_icon
switch_is_into_offer = urlresolvers.reverse( if self.limit_order_quantity_to_stock:
'is_into_offer', args=(self.id,) link = _boolean_icon(self.is_into_offer)
) else:
javascript = """ switch_is_into_offer = urlresolvers.reverse(
(function($) {{ 'is_into_offer', args=(self.id,)
var lien = '{LINK}'; )
$.ajax({{ javascript = """
url: lien, (function($) {{
cache: false, var lien = '{LINK}';
async: true, $.ajax({{
success: function (result) {{ url: lien,
$('#is_into_offer_{PRODUCT_ID}').html(result) cache: false,
}} async: true,
}}); success: function (result) {{
}})(django.jQuery); $('#is_into_offer_{PRODUCT_ID}').html(result)
""".format( }}
LINK=switch_is_into_offer, }});
PRODUCT_ID=self.id }})(django.jQuery);
) """.format(
link = '<a id="is_into_offer_%d" href="#" onclick="%s" class="btn">%s</a>' % ( LINK=switch_is_into_offer,
self.id, PRODUCT_ID=self.id
javascript, )
_boolean_icon(self.is_into_offer==True) link = '<a id="is_into_offer_%d" href="#" onclick="%s" class="btn">%s</a>' % (
) self.id,
javascript,
_boolean_icon(self.is_into_offer)
)
return link return link
get_is_into_offer.short_description = (_("is into offer")) get_is_into_offer.short_description = (_("is into offer"))
......
...@@ -10,7 +10,7 @@ from repanier.tools import cap ...@@ -10,7 +10,7 @@ from repanier.tools import cap
def flip_flop_is_into_offer(queryset): def flip_flop_is_into_offer(queryset):
for product in queryset.order_by('?'): for product in queryset.order_by('?'):
if product.is_active: if product.is_active and not product.limit_order_quantity_to_stock:
product.is_into_offer = not product.is_into_offer product.is_into_offer = not product.is_into_offer
product.save(update_fields=['is_into_offer']) product.save(update_fields=['is_into_offer'])
......
...@@ -20,8 +20,13 @@ def is_into_offer(request, product_id): ...@@ -20,8 +20,13 @@ def is_into_offer(request, product_id):
if request.is_ajax(): if request.is_ajax():
user = request.user user = request.user
if user.is_staff or user.is_superuser: if user.is_staff or user.is_superuser:
is_into_offer = not(Product.objects.filter(id=product_id).order_by('?').only( product = Product.objects.filter(id=product_id).order_by('?').only(
'is_into_offer').first().is_into_offer) 'is_into_offer', 'limit_order_quantity_to_stock').first()
Product.objects.filter(id=product_id).update(is_into_offer=is_into_offer) if product is not None:
return HttpResponse(mark_safe(_boolean_icon(is_into_offer))) if product.limit_order_quantity_to_stock:
new_is_into_offer = product.is_into_offer
else:
new_is_into_offer = not product.is_into_offer
Product.objects.filter(id=product_id).update(is_into_offer=new_is_into_offer)
return HttpResponse(mark_safe(_boolean_icon(new_is_into_offer)))
raise Http404 raise Http404
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment