Commit c14612ea authored by Patrick's avatar Patrick

Django 1.10 select_related usage/behavior is changed

parent 4d659bd5
...@@ -407,9 +407,9 @@ def offer_item_pre_save(sender, **kwargs): ...@@ -407,9 +407,9 @@ def offer_item_pre_save(sender, **kwargs):
offer_item = kwargs["instance"] offer_item = kwargs["instance"]
if offer_item.manage_replenishment: if offer_item.manage_replenishment:
if (offer_item.previous_add_2_stock != offer_item.add_2_stock or if (offer_item.previous_add_2_stock != offer_item.add_2_stock or
offer_item.previous_producer_unit_price != offer_item.producer_unit_price.amount or offer_item.previous_producer_unit_price != offer_item.producer_unit_price.amount or
offer_item.previous_unit_deposit != offer_item.unit_deposit.amount offer_item.previous_unit_deposit != offer_item.unit_deposit.amount
): ):
if offer_item.producer_invoice is None: if offer_item.producer_invoice is None:
producer_invoice = invoice.ProducerInvoice.objects.filter( producer_invoice = invoice.ProducerInvoice.objects.filter(
permanence_id=offer_item.permanence_id, permanence_id=offer_item.permanence_id,
......
...@@ -805,7 +805,7 @@ def recalculate_order_amount(permanence_id, ...@@ -805,7 +805,7 @@ def recalculate_order_amount(permanence_id,
purchase.save() purchase.save()
def display_selected_value(customer, offer_item, quantity_ordered): def display_selected_value(offer_item, quantity_ordered):
if offer_item.may_order: if offer_item.may_order:
if quantity_ordered <= DECIMAL_ZERO: if quantity_ordered <= DECIMAL_ZERO:
q_min = offer_item.customer_minimum_order_quantity q_min = offer_item.customer_minimum_order_quantity
...@@ -860,19 +860,12 @@ def display_selected_value(customer, offer_item, quantity_ordered): ...@@ -860,19 +860,12 @@ def display_selected_value(customer, offer_item, quantity_ordered):
return option_dict return option_dict
def display_selected_box_value(customer, offer_item): def display_selected_box_value(customer, offer_item, box_purchase):
if offer_item.is_box_content: if offer_item.is_box_content:
# box_name = _not_lazy("Composition") # box_name = _not_lazy("Composition")
box_name = BOX_UNICODE box_name = BOX_UNICODE
# Select one purchase # Select one purchase
box_purchase = models.Purchase.objects.filter( if box_purchase is not None:
customer_id=customer.id,
offer_item_id=offer_item.id,
# counter=0,
is_box_content=True
).only("quantity_ordered").order_by('?')
if box_purchase.exists():
box_purchase = box_purchase.first()
if box_purchase.quantity_ordered > DECIMAL_ZERO: if box_purchase.quantity_ordered > DECIMAL_ZERO:
qty_display = get_display( qty_display = get_display(
qty=box_purchase.quantity_ordered, qty=box_purchase.quantity_ordered,
...@@ -974,12 +967,18 @@ def update_or_create_purchase(customer=None, offer_item_id=None, value_id=None, ...@@ -974,12 +967,18 @@ def update_or_create_purchase(customer=None, offer_item_id=None, value_id=None,
offer_item_id=box_offer_item.id, offer_item_id=box_offer_item.id,
is_box_content=False is_box_content=False
).only("quantity_ordered").order_by('?').first() ).only("quantity_ordered").order_by('?').first()
option_dict = display_selected_value(customer, box_offer_item, option_dict = display_selected_value(
purchase.quantity_ordered if purchase is not None else DECIMAL_ZERO) box_offer_item,
purchase.quantity_ordered if purchase is not None else DECIMAL_ZERO
)
to_json.append(option_dict)
box_purchase = models.Purchase.objects.filter(
customer_id=customer.id,
offer_item_id=box_offer_item.id,
is_box_content=True
).only("quantity_ordered").order_by('?').first()
option_dict = display_selected_box_value(customer, box_offer_item, box_purchase)
to_json.append(option_dict) to_json.append(option_dict)
option_dict = display_selected_box_value(customer, box_offer_item)
to_json.append(option_dict)
transaction.savepoint_commit(sid) transaction.savepoint_commit(sid)
else: else:
transaction.savepoint_rollback(sid) transaction.savepoint_rollback(sid)
...@@ -1021,12 +1020,12 @@ def update_or_create_purchase(customer=None, offer_item_id=None, value_id=None, ...@@ -1021,12 +1020,12 @@ def update_or_create_purchase(customer=None, offer_item_id=None, value_id=None,
'html': '<option value="0" selected>%s</option>' % sold_out 'html': '<option value="0" selected>%s</option>' % sold_out
} }
else: else:
option_dict = display_selected_value(customer, offer_item, DECIMAL_ZERO) option_dict = display_selected_value(offer_item, DECIMAL_ZERO)
to_json.append(option_dict) to_json.append(option_dict)
else: else:
offer_item = models.OfferItem.objects.filter(id=offer_item_id).order_by('?').first() offer_item = models.OfferItem.objects.filter(id=offer_item_id).order_by('?').first()
if offer_item is not None: if offer_item is not None:
option_dict = display_selected_value(customer, offer_item, purchase.quantity_ordered) option_dict = display_selected_value(offer_item, purchase.quantity_ordered)
to_json.append(option_dict) to_json.append(option_dict)
customer_invoice = models.CustomerInvoice.objects.filter( customer_invoice = models.CustomerInvoice.objects.filter(
...@@ -1070,7 +1069,6 @@ def my_basket(is_order_confirm_send, order_amount, to_json): ...@@ -1070,7 +1069,6 @@ def my_basket(is_order_confirm_send, order_amount, to_json):
def my_order_confirmation(permanence, customer_invoice, is_basket=False, def my_order_confirmation(permanence, customer_invoice, is_basket=False,
basket_message=EMPTY_STRING, to_json=None): basket_message=EMPTY_STRING, to_json=None):
if permanence.with_delivery_point: if permanence.with_delivery_point:
if customer_invoice.delivery is not None: if customer_invoice.delivery is not None:
label = customer_invoice.delivery.get_delivery_customer_display() label = customer_invoice.delivery.get_delivery_customer_display()
...@@ -1197,9 +1195,10 @@ def my_order_confirmation(permanence, customer_invoice, is_basket=False, ...@@ -1197,9 +1195,10 @@ def my_order_confirmation(permanence, customer_invoice, is_basket=False,
msg_confirmation = EMPTY_STRING msg_confirmation = EMPTY_STRING
if apps.REPANIER_SETTINGS_CUSTOMERS_MUST_CONFIRM_ORDERS: if apps.REPANIER_SETTINGS_CUSTOMERS_MUST_CONFIRM_ORDERS:
if is_basket: if is_basket:
if permanence.with_delivery_point and customer_invoice.delivery is None: if customer_invoice.status == PERMANENCE_OPENED:
btn_disabled = "disabled" if permanence.with_delivery_point and customer_invoice.delivery is None:
msg_confirmation = '<span class="glyphicon glyphicon-floppy-disk"></span>&nbsp;&nbsp;%s' % _("Confirm this order and receive an email containing its summary.") btn_disabled = "disabled"
msg_confirmation = '<span class="glyphicon glyphicon-floppy-disk"></span>&nbsp;&nbsp;%s' % _("Confirm this order and receive an email containing its summary.")
else: else:
href = urlresolvers.reverse( href = urlresolvers.reverse(
'basket_view', args=(permanence.id,) 'basket_view', args=(permanence.id,)
...@@ -1231,18 +1230,31 @@ def my_order_confirmation(permanence, customer_invoice, is_basket=False, ...@@ -1231,18 +1230,31 @@ def my_order_confirmation(permanence, customer_invoice, is_basket=False,
else: else:
msg_html = EMPTY_STRING msg_html = EMPTY_STRING
if msg_html is None: if msg_html is None:
msg_html = """ if msg_confirmation == EMPTY_STRING:
<div class="row"> msg_html = """
<div class="panel panel-default"> <div class="row">
<div class="panel-heading"> <div class="panel panel-default">
%s <div class="panel-heading">
<button id="btn_confirm_order" class="btn btn-info" %s onclick="btn_receive_order_email();">%s</button> %s
<div class="clearfix"></div> <div class="clearfix"></div>
%s %s
</div> </div>
</div> </div>
</div> </div>
""" % (msg_delivery, btn_disabled, msg_confirmation, basket_message) """ % (msg_delivery, basket_message)
else:
msg_html = """
<div class="row">
<div class="panel panel-default">
<div class="panel-heading">
%s
<button id="btn_confirm_order" class="btn btn-info" %s onclick="btn_receive_order_email();">%s</button>
<div class="clearfix"></div>
%s
</div>
</div>
</div>
""" % (msg_delivery, btn_disabled, msg_confirmation, basket_message)
if to_json is not None: if to_json is not None:
option_dict = {'id': "#span_btn_confirm_order", 'html': msg_html} option_dict = {'id': "#span_btn_confirm_order", 'html': msg_html}
to_json.append(option_dict) to_json.append(option_dict)
...@@ -1250,7 +1262,7 @@ def my_order_confirmation(permanence, customer_invoice, is_basket=False, ...@@ -1250,7 +1262,7 @@ def my_order_confirmation(permanence, customer_invoice, is_basket=False,
def my_order_confirmation_email_send_to(customer): def my_order_confirmation_email_send_to(customer):
if customer is not None and customer.email2 is not None and len(customer.email2) > 0: if customer is not None and customer.email2:
to_email = (customer.user.email, customer.email2) to_email = (customer.user.email, customer.email2)
else: else:
to_email = (customer.user.email,) to_email = (customer.user.email,)
......
...@@ -21,7 +21,7 @@ def like_ajax(request): ...@@ -21,7 +21,7 @@ def like_ajax(request):
user = request.user user = request.user
if user.is_authenticated(): if user.is_authenticated():
offer_item_id = sint(request.GET.get('offer_item', 0)) offer_item_id = sint(request.GET.get('offer_item', 0))
offer_item = OfferItem.objects.filter(id=offer_item_id).order_by('?').only("product").first() offer_item = OfferItem.objects.filter(id=offer_item_id).order_by('?').select_related("product").first()
if offer_item is not None: if offer_item is not None:
product = offer_item.product product = offer_item.product
to_json = [] to_json = []
......
...@@ -57,30 +57,23 @@ def order_ajax(request): ...@@ -57,30 +57,23 @@ def order_ajax(request):
customer_id=customer.id, customer_id=customer.id,
offer_item_id=offer_item_id, offer_item_id=offer_item_id,
is_box_content=False is_box_content=False
).only(
"quantity_ordered", "offer_item",
).select_related( ).select_related(
"offer_item", "offer_item"
).order_by('?').first() ).order_by('?').first()
to_json = [] to_json = []
if purchase is not None: if purchase is not None:
option_dict = display_selected_value( option_dict = display_selected_value(
customer, purchase.offer_item, purchase.offer_item,
purchase.quantity_ordered) purchase.quantity_ordered)
to_json.append(option_dict) to_json.append(option_dict)
else: else:
offer_item = OfferItem.objects.filter( offer_item = OfferItem.objects.filter(
id=offer_item_id id=offer_item_id
).only( ).select_related(
"may_order", "customer_minimum_order_quantity",
"limit_order_quantity_to_stock",
"stock",
"quantity_invoiced",
"customer_alert_order_quantity",
"product" "product"
).select_related("product").order_by('?').first() ).order_by('?').first()
option_dict = display_selected_value( option_dict = display_selected_value(
customer, offer_item, offer_item,
DECIMAL_ZERO) DECIMAL_ZERO)
to_json.append(option_dict) to_json.append(option_dict)
result = json.dumps(to_json, cls=DjangoJSONEncoder) result = json.dumps(to_json, cls=DjangoJSONEncoder)
......
...@@ -16,7 +16,7 @@ from django.views.decorators.cache import never_cache ...@@ -16,7 +16,7 @@ from django.views.decorators.cache import never_cache
from django.views.decorators.http import require_GET from django.views.decorators.http import require_GET
from repanier.const import DECIMAL_ZERO, PERMANENCE_WAIT_FOR_DONE, PERMANENCE_OPENED, DECIMAL_ONE, \ from repanier.const import DECIMAL_ZERO, PERMANENCE_WAIT_FOR_DONE, PERMANENCE_OPENED, DECIMAL_ONE, \
PERMANENCE_CLOSED, REPANIER_MONEY_ZERO PERMANENCE_CLOSED, REPANIER_MONEY_ZERO, EMPTY_STRING
from repanier.models import Customer, Permanence, CustomerInvoice, PermanenceBoard, Staff, OfferItem, \ from repanier.models import Customer, Permanence, CustomerInvoice, PermanenceBoard, Staff, OfferItem, \
ProducerInvoice, Purchase ProducerInvoice, Purchase
from repanier.tools import sboolean, sint, display_selected_value, \ from repanier.tools import sboolean, sint, display_selected_value, \
...@@ -49,8 +49,6 @@ def order_init_ajax(request): ...@@ -49,8 +49,6 @@ def order_init_ajax(request):
customer_invoice = CustomerInvoice.objects.filter( customer_invoice = CustomerInvoice.objects.filter(
permanence_id=permanence.id, permanence_id=permanence.id,
customer_id=customer.id customer_id=customer.id
).only(
'is_order_confirm_send', 'delivery_id', 'total_price_with_tax'
).order_by('?').first() ).order_by('?').first()
if customer_invoice is None: if customer_invoice is None:
customer_invoice = CustomerInvoice.objects.create( customer_invoice = CustomerInvoice.objects.create(
...@@ -80,12 +78,15 @@ def order_init_ajax(request): ...@@ -80,12 +78,15 @@ def order_init_ajax(request):
REPANIER_SETTINGS_MAX_WEEK_WO_PARTICIPATION REPANIER_SETTINGS_MAX_WEEK_WO_PARTICIPATION
if basket or (REPANIER_SETTINGS_CUSTOMERS_MUST_CONFIRM_ORDERS if basket or (REPANIER_SETTINGS_CUSTOMERS_MUST_CONFIRM_ORDERS
and customer_invoice.is_order_confirm_send): and customer_invoice.is_order_confirm_send):
if customer_invoice.status <= PERMANENCE_CLOSED: if customer_invoice.status <= PERMANENCE_OPENED:
basket_message = calc_basket_message(customer, permanence, customer_invoice.status) basket_message = calc_basket_message(customer, permanence, customer_invoice.status)
else: else:
basket_message = "%s" % ( if customer_invoice.delivery is not None:
_('The orders are closed.'), basket_message = EMPTY_STRING
) else:
basket_message = "%s" % (
_('The orders are closed.'),
)
my_order_confirmation( my_order_confirmation(
permanence=permanence, permanence=permanence,
customer_invoice=customer_invoice, customer_invoice=customer_invoice,
...@@ -164,11 +165,7 @@ def order_init_ajax(request): ...@@ -164,11 +165,7 @@ def order_init_ajax(request):
to_json.append(option_dict) to_json.append(option_dict)
else: else:
customer = None customer = None
# my_name = _not_lazy('Anonymous')
my_basket(False, REPANIER_MONEY_ZERO, to_json) my_basket(False, REPANIER_MONEY_ZERO, to_json)
# option_dict = {'id': "#my_name", 'html': my_name}
# to_json.append(option_dict)
request_offer_items = request.GET.getlist('oi') request_offer_items = request.GET.getlist('oi')
for request_offer_item in request_offer_items: for request_offer_item in request_offer_items:
offer_item_id = sint(request_offer_item) offer_item_id = sint(request_offer_item)
...@@ -179,45 +176,37 @@ def order_init_ajax(request): ...@@ -179,45 +176,37 @@ def order_init_ajax(request):
customer_id=customer.id, customer_id=customer.id,
offer_item_id=offer_item_id, offer_item_id=offer_item_id,
is_box_content=False is_box_content=False
).only(
"quantity_ordered", "offer_item",
).select_related( ).select_related(
"offer_item", "offer_item"
).order_by('?').first() ).order_by('?').first()
if purchase is not None: if purchase is not None:
offer_item = purchase.offer_item offer_item = purchase.offer_item
option_dict = display_selected_value( if offer_item is not None:
customer, offer_item, option_dict = display_selected_value(
purchase.quantity_ordered) offer_item,
to_json.append(option_dict) purchase.quantity_ordered)
to_json.append(option_dict)
else: else:
offer_item = OfferItem.objects.filter( offer_item = OfferItem.objects.filter(
id=offer_item_id id=offer_item_id
).only( ).order_by('?').first()
"may_order", "customer_minimum_order_quantity", if offer_item is not None:
"limit_order_quantity_to_stock", option_dict = display_selected_value(
"stock", offer_item,
"quantity_invoiced", DECIMAL_ZERO)
"customer_alert_order_quantity", to_json.append(option_dict)
"product" box_purchase = Purchase.objects.filter(
).select_related("product").order_by('?').first()
option_dict = display_selected_value(
customer, offer_item,
DECIMAL_ZERO)
to_json.append(option_dict)
purchase = Purchase.objects.filter(
customer_id=customer.id, customer_id=customer.id,
offer_item_id=offer_item_id, offer_item_id=offer_item_id,
is_box_content=True is_box_content=True
).only(
"quantity_ordered", "offer_item",
).select_related( ).select_related(
"offer_item", "offer_item"
).order_by('?').first() ).order_by('?').first()
if purchase is not None: if box_purchase is not None:
offer_item = purchase.offer_item offer_item = box_purchase.offer_item
option_dict = display_selected_box_value(customer, offer_item) if offer_item is not None:
to_json.append(option_dict) option_dict = display_selected_box_value(customer, offer_item, box_purchase)
to_json.append(option_dict)
option_dict = {'id': ".btn_like%s" % offer_item_id, 'html': offer_item.get_like(user)} option_dict = {'id': ".btn_like%s" % offer_item_id, 'html': offer_item.get_like(user)}
to_json.append(option_dict) to_json.append(option_dict)
else: else:
......
...@@ -37,7 +37,7 @@ def order_select_ajax(request): ...@@ -37,7 +37,7 @@ def order_select_ajax(request):
customer_id=customer.id, customer_id=customer.id,
offer_item_id=offer_item_id, offer_item_id=offer_item_id,
is_box_content=False is_box_content=False
).only("quantity_ordered").order_by('?').first() ).order_by('?').first()
if purchase is not None: if purchase is not None:
q_previous_order = purchase.quantity_ordered q_previous_order = purchase.quantity_ordered
else: else:
...@@ -149,7 +149,7 @@ def order_select_ajax(request): ...@@ -149,7 +149,7 @@ def order_select_ajax(request):
option_dict = {'value': '0', 'selected': 'selected', 'label': '%s' % closed} option_dict = {'value': '0', 'selected': 'selected', 'label': '%s' % closed}
to_json.append(option_dict) to_json.append(option_dict)
else: else:
option_dict = display_selected_value(customer, offer_item, purchase.quantity_ordered) option_dict = display_selected_value(offer_item, purchase.quantity_ordered)
to_json.append(option_dict) to_json.append(option_dict)
else: else:
option_dict = {'value': '0', 'selected': 'selected', 'label': '---'} option_dict = {'value': '0', 'selected': 'selected', 'label': '---'}
......
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