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):
offer_item = kwargs["instance"]
if offer_item.manage_replenishment:
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_unit_deposit != offer_item.unit_deposit.amount
):
offer_item.previous_producer_unit_price != offer_item.producer_unit_price.amount or
offer_item.previous_unit_deposit != offer_item.unit_deposit.amount
):
if offer_item.producer_invoice is None:
producer_invoice = invoice.ProducerInvoice.objects.filter(
permanence_id=offer_item.permanence_id,
......
......@@ -805,7 +805,7 @@ def recalculate_order_amount(permanence_id,
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 quantity_ordered <= DECIMAL_ZERO:
q_min = offer_item.customer_minimum_order_quantity
......@@ -860,19 +860,12 @@ def display_selected_value(customer, offer_item, quantity_ordered):
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:
# box_name = _not_lazy("Composition")
box_name = BOX_UNICODE
# Select one purchase
box_purchase = models.Purchase.objects.filter(
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 is not None:
if box_purchase.quantity_ordered > DECIMAL_ZERO:
qty_display = get_display(
qty=box_purchase.quantity_ordered,
......@@ -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,
is_box_content=False
).only("quantity_ordered").order_by('?').first()
option_dict = display_selected_value(customer, box_offer_item,
purchase.quantity_ordered if purchase is not None else DECIMAL_ZERO)
option_dict = display_selected_value(
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)
option_dict = display_selected_box_value(customer, box_offer_item)
to_json.append(option_dict)
transaction.savepoint_commit(sid)
else:
transaction.savepoint_rollback(sid)
......@@ -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
}
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)
else:
offer_item = models.OfferItem.objects.filter(id=offer_item_id).order_by('?').first()
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)
customer_invoice = models.CustomerInvoice.objects.filter(
......@@ -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,
basket_message=EMPTY_STRING, to_json=None):
if permanence.with_delivery_point:
if customer_invoice.delivery is not None:
label = customer_invoice.delivery.get_delivery_customer_display()
......@@ -1197,9 +1195,10 @@ def my_order_confirmation(permanence, customer_invoice, is_basket=False,
msg_confirmation = EMPTY_STRING
if apps.REPANIER_SETTINGS_CUSTOMERS_MUST_CONFIRM_ORDERS:
if is_basket:
if permanence.with_delivery_point and customer_invoice.delivery is None:
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.")
if customer_invoice.status == PERMANENCE_OPENED:
if permanence.with_delivery_point and customer_invoice.delivery is None:
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:
href = urlresolvers.reverse(
'basket_view', args=(permanence.id,)
......@@ -1231,18 +1230,31 @@ def my_order_confirmation(permanence, customer_invoice, is_basket=False,
else:
msg_html = EMPTY_STRING
if msg_html is None:
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 msg_confirmation == EMPTY_STRING:
msg_html = """
<div class="row">
<div class="panel panel-default">
<div class="panel-heading">
%s
<div class="clearfix"></div>
%s
</div>
</div>
</div>
""" % (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:
option_dict = {'id': "#span_btn_confirm_order", 'html': msg_html}
to_json.append(option_dict)
......@@ -1250,7 +1262,7 @@ def my_order_confirmation(permanence, customer_invoice, is_basket=False,
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)
else:
to_email = (customer.user.email,)
......
......@@ -21,7 +21,7 @@ def like_ajax(request):
user = request.user
if user.is_authenticated():
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:
product = offer_item.product
to_json = []
......
......@@ -57,30 +57,23 @@ def order_ajax(request):
customer_id=customer.id,
offer_item_id=offer_item_id,
is_box_content=False
).only(
"quantity_ordered", "offer_item",
).select_related(
"offer_item",
"offer_item"
).order_by('?').first()
to_json = []
if purchase is not None:
option_dict = display_selected_value(
customer, purchase.offer_item,
purchase.offer_item,
purchase.quantity_ordered)
to_json.append(option_dict)
else:
offer_item = OfferItem.objects.filter(
id=offer_item_id
).only(
"may_order", "customer_minimum_order_quantity",
"limit_order_quantity_to_stock",
"stock",
"quantity_invoiced",
"customer_alert_order_quantity",
).select_related(
"product"
).select_related("product").order_by('?').first()
).order_by('?').first()
option_dict = display_selected_value(
customer, offer_item,
offer_item,
DECIMAL_ZERO)
to_json.append(option_dict)
result = json.dumps(to_json, cls=DjangoJSONEncoder)
......
......@@ -16,7 +16,7 @@ from django.views.decorators.cache import never_cache
from django.views.decorators.http import require_GET
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, \
ProducerInvoice, Purchase
from repanier.tools import sboolean, sint, display_selected_value, \
......@@ -49,8 +49,6 @@ def order_init_ajax(request):
customer_invoice = CustomerInvoice.objects.filter(
permanence_id=permanence.id,
customer_id=customer.id
).only(
'is_order_confirm_send', 'delivery_id', 'total_price_with_tax'
).order_by('?').first()
if customer_invoice is None:
customer_invoice = CustomerInvoice.objects.create(
......@@ -80,12 +78,15 @@ def order_init_ajax(request):
REPANIER_SETTINGS_MAX_WEEK_WO_PARTICIPATION
if basket or (REPANIER_SETTINGS_CUSTOMERS_MUST_CONFIRM_ORDERS
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)
else:
basket_message = "%s" % (
_('The orders are closed.'),
)
if customer_invoice.delivery is not None:
basket_message = EMPTY_STRING
else:
basket_message = "%s" % (
_('The orders are closed.'),
)
my_order_confirmation(
permanence=permanence,
customer_invoice=customer_invoice,
......@@ -164,11 +165,7 @@ def order_init_ajax(request):
to_json.append(option_dict)
else:
customer = None
# my_name = _not_lazy('Anonymous')
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')
for request_offer_item in request_offer_items:
offer_item_id = sint(request_offer_item)
......@@ -179,45 +176,37 @@ def order_init_ajax(request):
customer_id=customer.id,
offer_item_id=offer_item_id,
is_box_content=False
).only(
"quantity_ordered", "offer_item",
).select_related(
"offer_item",
"offer_item"
).order_by('?').first()
if purchase is not None:
offer_item = purchase.offer_item
option_dict = display_selected_value(
customer, offer_item,
purchase.quantity_ordered)
to_json.append(option_dict)
if offer_item is not None:
option_dict = display_selected_value(
offer_item,
purchase.quantity_ordered)
to_json.append(option_dict)
else:
offer_item = OfferItem.objects.filter(
id=offer_item_id
).only(
"may_order", "customer_minimum_order_quantity",
"limit_order_quantity_to_stock",
"stock",
"quantity_invoiced",
"customer_alert_order_quantity",
"product"
).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(
).order_by('?').first()
if offer_item is not None:
option_dict = display_selected_value(
offer_item,
DECIMAL_ZERO)
to_json.append(option_dict)
box_purchase = Purchase.objects.filter(
customer_id=customer.id,
offer_item_id=offer_item_id,
is_box_content=True
).only(
"quantity_ordered", "offer_item",
).select_related(
"offer_item",
"offer_item"
).order_by('?').first()
if purchase is not None:
offer_item = purchase.offer_item
option_dict = display_selected_box_value(customer, offer_item)
to_json.append(option_dict)
if box_purchase is not None:
offer_item = box_purchase.offer_item
if offer_item is not None:
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)}
to_json.append(option_dict)
else:
......
......@@ -37,7 +37,7 @@ def order_select_ajax(request):
customer_id=customer.id,
offer_item_id=offer_item_id,
is_box_content=False
).only("quantity_ordered").order_by('?').first()
).order_by('?').first()
if purchase is not None:
q_previous_order = purchase.quantity_ordered
else:
......@@ -149,7 +149,7 @@ def order_select_ajax(request):
option_dict = {'value': '0', 'selected': 'selected', 'label': '%s' % closed}
to_json.append(option_dict)
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)
else:
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