Commit 6ffa43ef authored by Patrick's avatar Patrick

Infinite scroll on order screen using...

Infinite scroll on order screen using https://simpleisbetterthancomplex.com/tutorial/2017/03/13/how-to-create-infinite-scroll-with-django.html
And also some refactoring of this screen
parent 146fb095
......@@ -18,5 +18,5 @@ from box import BoxContent
# proxies
from box import Box
from invoice import CustomerSend
from offeritem import OfferItemSend, OfferItemClosed
from offeritem import OfferItemSend, OfferItemClosed, OfferItemWoReceiver
from permanence import PermanenceInPreparation, PermanenceDone
\ No newline at end of file
......@@ -235,6 +235,17 @@ def offer_item_pre_save(sender, **kwargs):
offer_item.previous_unit_deposit = offer_item.unit_deposit.amount
@python_2_unicode_compatible
class OfferItemWoReceiver(OfferItem):
def __str__(self):
return EMPTY_STRING
class Meta:
proxy = True
verbose_name = _("offer's item")
verbose_name_plural = _("offer's items")
@python_2_unicode_compatible
class OfferItemSend(OfferItem):
def __str__(self):
......
......@@ -115,21 +115,21 @@
<a name="content"></a><section>
{% if current_page.is_home %}
<div class="container-info" id="containerInfo" style="display: none"></div>
{% addtoblock "lastjs" %}{% spaceless %}
<script type="text/javascript">
$(document).ready(function () {
lien = '{% url 'home_info' %}';
$.ajax({
url: lien,
cache: false,
async: false,
success: function (result) {
$("#containerInfo").html(result).show();
}
});
});
</script>
{% endspaceless %}{% endaddtoblock %}
{% addtoblock "lastjs" %}{% spaceless %}
<script type="text/javascript">
$(document).ready(function () {
lien = '{% url 'home_info' %}';
$.ajax({
url: lien,
cache: false,
async: false,
success: function (result) {
$("#containerInfo").html(result).show();
}
});
});
</script>
{% endspaceless %}{% endaddtoblock %}
{% endif %}
{% block base_content%}{% endblock %}
</section>
......
This diff is collapsed.
{% extends 'cms_page.html' %}
{% extends 'cms_order_page.html' %}
{% load cms_tags sekizai_tags i18n l10n %}
{% block sub_content %}
<div class="col-xs-12">
<nav class="bs-docs-sidebar">
<div class="fixed">
{# q : {{ q }}<br/>#}
{# producer_id : {{ producer_id }}<br/>#}
{# departementforcustomer_id : {{ departementforcustomer_id }}<br/>#}
{# box_id : {{ box_id }}<br/>#}
{# is_like : {{ is_like }}{% if is_like %} is_like==True{% endif %}<br/>#}
{# is_basket : {{ is_basket }}<br/>#}
{# is_box : {{ is_box }}<br/>#}
<form action="{% url "order_view" permanence_id %}" id="search_form" class="form-horizontal navbar-form" role="search" method="get">
{% csrf_token %}
<div class="input-group">
{% if q != None %}
<input type="search" autosave="repanier" class="form-control" value="{{ q }}" name="q">
{% else %}
<input type="search" autosave="repanier" class="form-control" placeholder="{% trans "Search" %}"
name="q">
{% endif %}
<div class="input-group-btn">
<button class="btn btn-default" type="submit"><i class="glyphicon glyphicon-search"></i></button>
</div>
</div>
<input type="hidden" name="producer" value="{{ producer_id }}">
<input type="hidden" name="departementforcustomer" value="{{ departementforcustomer_id }}">
<input type="hidden" name="box" value="{{ box_id }}">
<input type="hidden" name="is_like" value="{{ is_like }}">
<input type="hidden" name="is_basket" value="{{ is_basket }}">
</form>
<br/>
<ul class="nav nav-stacked" id="sidebar">
<li>
<a href="{% url "order_view" permanence_id %}?{% if not is_like %}is_like=yes{% endif %}{% if q %}&q={{ q }}{% endif %}{% if is_basket %}&is_basket=yes{% endif %}" {% if is_like %}class="bs-docs-sidebar-active"{% endif %}>{% trans "My likes" %} <span class="glyphicon glyphicon-heart"></span></a>
</li>
{% if box_set %}
{% for box in box_set.all %}
<li><a href="{% url "order_view" permanence_id %}?box={{ box.id|unlocalize }}{% if q %}&q={{ q }}{% endif %}{% if is_like %}&is_like=yes{% endif %}{% if is_basket %}&is_basket=yes{% endif %}"
{% if box.id == box_id|add:0 %}class="bs-docs-sidebar-active"{% endif %}>{{ box.long_name | truncatechars:20 }} 📦{# <span class="glyphicon glyphicon-gift"></span> #}</a>
{% endfor %}
{% endif %}
{% if producer_set %}
<li>
<a href="{% url "order_view" permanence_id %}?departementforcustomer={{ departementforcustomer_id }}{% if q %}&q={{ q }}{% endif %}{% if is_like %}&is_like=yes{% endif %}{% if is_basket %}&is_basket=yes{% endif %}"
{% if producer_id == "all" %}class="bs-docs-sidebar-active"{% endif %}>{% trans "All producers" %}</a>
<ul class="nav nav-stacked">
{% for producer in producer_set.all %}
<li><a href="{% url "order_view" permanence_id %}?producer={{ producer.id|unlocalize }}&departementforcustomer={{ departementforcustomer_id }}{% if q %}&q={{ q }}{% endif %}{% if is_like %}&is_like=yes{% endif %}{% if is_basket %}&is_basket=yes{% endif %}"
{% if producer.id == producer_id|add:0 %}class="bs-docs-sidebar-active"{% endif %}>{{ producer.short_profile_name | truncatechars:15 }}&nbsp;<span id="order_procent{{ producer.id|unlocalize }}" class="badge"></span><span id="order_closed{{ producer.id|unlocalize }}" class="text-warning"></span></a>
</li>
{% endfor %}
</ul>
</li>
{% else %}{% if not departementforcustomer_set %}{% if is_like or is_basket or is_box or q %}
<li>
<a href="{% url "order_view" permanence_id %}">{% trans "All products" %}</a>
</li>
{% endif %}{% endif %}{% endif %}
{% if departementforcustomer_set %}
<li>
<a href="{% url "order_view" permanence_id %}?producer={{ producer_id }}{% if q %}&q={{ q }}{% endif %}{% if is_like %}&is_like=yes{% endif %}{% if is_basket %}&is_basket=yes{% endif %}"
{% if departementforcustomer_id == "all" %}class="bs-docs-sidebar-active"{% endif %}>{% trans "All departments" %}</a>
<ul class="nav nav-stacked">
{% for departementforcustomer in departementforcustomer_set.all %}
{% if departementforcustomer.level > 0 %}
{% ifchanged departementforcustomer.parent_id %}
{% if departementforcustomer.parent.level == 0 %}</ul>{% endif %}
<li><a href="{% url "order_view" permanence_id %}?producer={{ producer_id }}&departementforcustomer={{ departementforcustomer.parent_id|unlocalize }}{% if q %}&q={{ q }}{% endif %}{% if is_like %}&is_like=yes{% endif %}{% if is_basket %}&is_basket=yes{% endif %}"
{% if departementforcustomer.parent_id == departementforcustomer_id|add:0 %}class="bs-docs-sidebar-active"{% endif %}>{{ departementforcustomer.parent.short_name }}</a>
</li>
{% if departementforcustomer.parent.level == 0 %}<ul class="nav nav-stacked">{% endif %}
{% endifchanged %}
<li>
<a href="{% url "order_view" permanence_id %}?producer={{ producer_id }}&departementforcustomer={{ departementforcustomer.id|unlocalize }}{% if q %}&q={{ q }}{% endif %}{% if is_like %}&is_like=yes{% endif %}{% if is_basket %}&is_basket=yes{% endif %}"
{% if departementforcustomer.id == departementforcustomer_id|add:0 %}class="bs-docs-sidebar-active"{% endif %}>{{ departementforcustomer.short_name }}</a>
</li>
{% else %}
</ul><li>
<a href="{% url "order_view" permanence_id %}?producer={{ producer_id }}&departementforcustomer={{ departementforcustomer.id|unlocalize }}{% if q %}&q={{ q }}{% endif %}{% if is_like %}&is_like=yes{% endif %}{% if is_basket %}&is_basket=yes{% endif %}"
{% if departementforcustomer.id == departementforcustomer_id|add:0 %}class="bs-docs-sidebar-active"{% endif %}>{{ departementforcustomer.short_name }}</a>
</li><ul class="nav nav-stacked">
{% endif %}
{% endfor %}
</ul>
</li>
{% endif %}
</ul>
</div>
</nav>
</div>
{% if not is_anonymous %}
{% addtoblock "lastjs" %}
<script type="text/javascript">
$(document).ready(function () {
$("#link_to_order_or_filter_view").html('<a href="{% url "order_view" permanence_id %}?producer={{ producer_id }}&departementforcustomer={{ departementforcustomer_id }}&box={{ box_id }}{% if q %}&q={{ q }}{% endif %}{% if is_like %}&is_like=yes{% endif %}{% if is_basket %}&is_basket=yes{% endif %}" class="btn btn-default" style="margin-left: 10px;"><span class="glyphicon glyphicon-eye-close" aria-hidden="true"></span></a>');
var lien = '{% url "my_cart_amount_ajax" permanence_id %}';
$.ajax({
url: lien,
cache: false,
dataType: 'json',
async: true,
success: function (result) {
$.each(result, function (key, val) {
$(val.id).each(function( index ) { $( this ).html(val.html)});
});
}
});
$("#li_my_basket").show();
});
</script>
{% endaddtoblock %}
{% endif %}
{% endblock %}
{% block notification %}{% endblock notification %}
{% block login %}{% endblock login %}
{% block may_order %}{% endblock may_order %}
{% block permanence_description %}{% endblock permanence_description %}
{% block confirm_basket %}{% endblock confirm_basket %}
{% block product_filter %}{{ block.super }}{% endblock product_filter %}
{% block product_list %}{% endblock product_list %}
{% block order_modal %}{% endblock order_modal %}
{% block my_cart_amount_js %}{{ block.super }}{% endblock my_cart_amount_js %}
{% block my_order_js %}{% endblock my_order_js %}
{% block link_to_filter_js %}{% endblock link_to_filter_js %}
{% block link_to_order_js %}{{ block.super }}{% endblock link_to_order_js %}
This diff is collapsed.
......@@ -8,8 +8,8 @@ from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _
from repanier.const import EMPTY_STRING, PERMANENCE_CLOSED, DECIMAL_ZERO
from repanier.models import PermanenceBoard, CustomerInvoice
from repanier.tools import sint
from repanier.models import PermanenceBoard, CustomerInvoice, Purchase
from repanier.tools import sint, display_selected_value, display_selected_box_value
register = template.Library()
......@@ -189,3 +189,58 @@ def repanier_select_task(context, *args, **kwargs):
permanence_board_id=permanence_board.id
)
return mark_safe(result)
@register.simple_tag(takes_context=True)
def repanier_select_offer_item(context, *args, **kwargs):
request = context['request']
user = request.user
result = EMPTY_STRING
if user.is_authenticated and not(user.is_staff or user.is_superuser):
offer_item = kwargs.get('offer_item', None)
str_id = str(offer_item.id)
if offer_item.may_order:
purchase = Purchase.objects.filter(
customer_id=user.customer,
offer_item_id=offer_item.id,
is_box_content=False
).order_by('?').first()
if purchase is not None:
option_dict = display_selected_value(
offer_item,
purchase.quantity_ordered)
else:
option_dict = display_selected_value(
offer_item,
DECIMAL_ZERO)
# print(option_dict)
result = '<select name="offer_item{str_id}" id="offer_item{str_id}" {onchange} onclick="show_select_order_list_ajax({str_id})" class="form-control">{option}</select>'.format(
str_id=str_id,
onchange='onchange="order_ajax(%s)"' % str_id if user.is_authenticated else 'disabled',
option=option_dict['html']
)
box_purchase = Purchase.objects.filter(
customer_id=user.customer,
offer_item_id=offer_item.id,
is_box_content=True
).order_by('?').first()
if box_purchase is not None:
offer_item = box_purchase.offer_item
option_dict = display_selected_box_value(offer_item, box_purchase)
result = result + option_dict['html']
return mark_safe(result)
@register.simple_tag(takes_context=True)
def repanier_btn_like(context, *args, **kwargs):
request = context['request']
user = request.user
result = EMPTY_STRING
if not(user.is_staff or user.is_superuser):
offer_item = kwargs.get('offer_item', None)
str_id = str(offer_item.id)
result = '<br/><span class="btn_like{str_id}" style="cursor: pointer;">{html}</span>'.format(
str_id=str_id,
html=offer_item.get_like(user)
)
return mark_safe(result)
\ No newline at end of file
......@@ -674,7 +674,7 @@ def display_selected_value(offer_item, quantity_ordered):
}
else:
option_dict = {
'id' : "#box_offer_item%d" % offer_item.id,
'id' : "#offer_item%d" % offer_item.id,
'html': EMPTY_STRING
}
return option_dict
......@@ -713,7 +713,7 @@ def display_selected_box_value(offer_item, box_purchase):
else:
option_dict = {
'id' : "#box_offer_item%d" % offer_item.id,
'html': ''
'html': EMPTY_STRING
}
return option_dict
......@@ -1136,7 +1136,7 @@ def my_order_confirmation(permanence, customer_invoice, is_basket=False,
'decrease': number_format((DECIMAL_ONE - customer_invoice.price_list_multiplier) * 100, 2)
}
msg_delivery = '%s<b><i><select name="delivery" id="delivery" onmouseover="delivery_select_ajax()" onchange="delivery_ajax()" class="form-control"><option value="%d" selected>%s</option></select></i></b><br/>%s%s' % (
msg_delivery = '%s<b><i><select name="delivery" id="delivery" onclick="show_select_delivery_list_ajax()" onchange="delivery_ajax()" class="form-control"><option value="%d" selected>%s</option></select></i></b><br/>%s%s' % (
_("Delivery point"),
delivery_id,
label,
......
......@@ -3,6 +3,7 @@ from __future__ import unicode_literals
import json
from django.contrib.auth.decorators import login_required
from django.core.serializers.json import DjangoJSONEncoder
from django.db.models import Q
from django.http import Http404
......@@ -17,75 +18,72 @@ from repanier.tools import sint, sboolean, calc_basket_message, my_order_confirm
@never_cache
@require_GET
@login_required
def delivery_ajax(request):
if request.is_ajax():
user = request.user
if user.is_authenticated:
permanence_id = sint(request.GET.get('permanence', 0))
basket = sboolean(request.GET.get('basket', False))
permanence = Permanence.objects.filter(
id=permanence_id
).only("id", "status").order_by('?').first()
if permanence is None:
raise Http404
customer = Customer.objects.filter(
user_id=user.id, is_active=True, may_order=True
).only(
"id", "delivery_point", "balance"
).order_by('?').first()
if customer is None:
raise Http404
customer_invoice = CustomerInvoice.objects.filter(
customer_id=customer.id,
permanence_id=permanence_id
).order_by('?').first()
if customer_invoice is None:
raise Http404
delivery_id = sint(request.GET.get('delivery', 0))
if customer.delivery_point is not None:
qs = DeliveryBoard.objects.filter(
Q(
id=delivery_id,
permanence_id=permanence_id,
delivery_point_id=customer.delivery_point_id,
# delivery_point__closed_group=True, -> This is always the case
# when delivery_point_id == customer.delivery_point_id
status=PERMANENCE_OPENED
) | Q(
id=delivery_id,
permanence_id=permanence_id,
delivery_point__closed_group=False,
status=PERMANENCE_OPENED
)
).order_by('?')
else:
qs = DeliveryBoard.objects.filter(
id=delivery_id,
permanence_id=permanence_id,
delivery_point__closed_group=False,
status=PERMANENCE_OPENED
).order_by('?')
delivery = qs.first()
if delivery is not None:
to_json = []
if (customer_invoice.status == PERMANENCE_OPENED and not customer_invoice.is_order_confirm_send) \
or (customer_invoice.total_price_with_tax == DECIMAL_ZERO):
customer_invoice.status = PERMANENCE_OPENED
customer_invoice.set_delivery(delivery)
customer_invoice.save()
# IMPORTANT : Set the status of the may be already existing purchase to "Open" so that
# the total_price_with_tax will be correctly calculated on the customer order screen.
Purchase.objects.filter(customer_invoice=customer_invoice).order_by('?').update(status=PERMANENCE_OPENED)
# basket_message = calc_basket_message(
# customer, permanence, PERMANENCE_OPENED
# )
my_order_confirmation(
permanence=permanence,
customer_invoice=customer_invoice,
is_basket=basket,
# basket_message=basket_message,
to_json=to_json
)
return HttpResponse(json.dumps(to_json, cls=DjangoJSONEncoder), content_type="application/json")
raise Http404
if not request.is_ajax():
raise Http404
user = request.user
permanence_id = sint(request.GET.get('permanence', 0))
permanence = Permanence.objects.filter(
id=permanence_id
).only("id", "status").order_by('?').first()
if permanence is None:
raise Http404
customer = Customer.objects.filter(
user_id=user.id, is_active=True, may_order=True
).only(
"id", "delivery_point", "balance"
).order_by('?').first()
if customer is None:
raise Http404
customer_invoice = CustomerInvoice.objects.filter(
customer_id=customer.id,
permanence_id=permanence_id
).order_by('?').first()
if customer_invoice is None:
raise Http404
delivery_id = sint(request.GET.get('delivery', 0))
if customer.delivery_point is not None:
qs = DeliveryBoard.objects.filter(
Q(
id=delivery_id,
permanence_id=permanence_id,
delivery_point_id=customer.delivery_point_id,
# delivery_point__closed_group=True, -> This is always the case
# when delivery_point_id == customer.delivery_point_id
status=PERMANENCE_OPENED
) | Q(
id=delivery_id,
permanence_id=permanence_id,
delivery_point__closed_group=False,
status=PERMANENCE_OPENED
)
).order_by('?')
else:
qs = DeliveryBoard.objects.filter(
id=delivery_id,
permanence_id=permanence_id,
delivery_point__closed_group=False,
status=PERMANENCE_OPENED
).order_by('?')
delivery = qs.first()
if delivery is None:
raise Http404
to_json = []
if (customer_invoice.status == PERMANENCE_OPENED and not customer_invoice.is_order_confirm_send) \
or (customer_invoice.total_price_with_tax == DECIMAL_ZERO):
customer_invoice.status = PERMANENCE_OPENED
customer_invoice.set_delivery(delivery)
customer_invoice.save()
# IMPORTANT : Set the status of the may be already existing purchase to "Open" so that
# the total_price_with_tax will be correctly calculated on the customer order screen.
Purchase.objects.filter(customer_invoice=customer_invoice).order_by('?').update(status=PERMANENCE_OPENED)
is_basket = sboolean(request.GET.get('is_basket', False))
my_order_confirmation(
permanence=permanence,
customer_invoice=customer_invoice,
is_basket=is_basket,
to_json=to_json
)
return HttpResponse(json.dumps(to_json, cls=DjangoJSONEncoder), content_type="application/json")
......@@ -26,13 +26,9 @@ def my_cart_amount_ajax(request, permanence_id):
if not request.is_ajax():
raise Http404
user = request.user
customer = Customer.objects.filter(
user_id=user.id, is_active=True, may_order=True).order_by('?').first()
if customer is None:
raise Http404
customer_invoice = CustomerInvoice.objects.filter(
permanence_id=permanence_id,
customer_id=customer.id
customer__user_id=user.id
).order_by('?').first()
if customer_invoice is None:
raise Http404
......
This diff is collapsed.
......@@ -4,6 +4,7 @@ from __future__ import unicode_literals
import datetime
import json
from django.contrib.auth.decorators import login_required
from django.core.serializers.json import DjangoJSONEncoder
from django.http import Http404
from django.http import HttpResponse
......@@ -18,17 +19,16 @@ from repanier.const import DECIMAL_ZERO, PERMANENCE_WAIT_FOR_INVOICED, PERMANENC
from repanier.models import Customer, Permanence, CustomerInvoice, PermanenceBoard, Staff, OfferItem, \
ProducerInvoice, Purchase
from repanier.tools import sboolean, sint, display_selected_value, \
display_selected_box_value, my_basket, my_order_confirmation, calc_basket_message, permanence_ok_or_404
display_selected_box_value, my_order_confirmation, calc_basket_message, permanence_ok_or_404
@never_cache
@require_GET
@login_required
def order_init_ajax(request):
if not request.is_ajax():
raise Http404
user = request.user
if not user.is_authenticated:
raise Http404
customer = Customer.objects.filter(
user_id=user.id, is_active=True
).only(
......@@ -59,8 +59,6 @@ def order_init_ajax(request):
if customer_invoice is None:
raise Http404
my_basket(customer_invoice.is_order_confirm_send, customer_invoice.get_total_price_with_tax(), to_json)
basket = sboolean(request.GET.get('ba', False))
from repanier.apps import REPANIER_SETTINGS_DISPLAY_PRODUCER_ON_ORDER_FORM, \
REPANIER_SETTINGS_MAX_WEEK_WO_PARTICIPATION
......@@ -121,47 +119,4 @@ def order_init_ajax(request):
option_dict = {'id': "#communication", 'html': html}
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)
# No need to check customer.may_order.
# Select one purchase
purchase = Purchase.objects.filter(
customer_id=customer.id,
offer_item_id=offer_item_id,
is_box_content=False
).select_related(
"offer_item"
).order_by('?').first()
if purchase is not None:
offer_item = purchase.offer_item
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
).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
).select_related(
"offer_item"
).order_by('?').first()
if box_purchase is not None:
offer_item = box_purchase.offer_item
if offer_item is not None:
option_dict = display_selected_box_value(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)
return HttpResponse(json.dumps(to_json, cls=DjangoJSONEncoder), content_type="application/json")
......@@ -197,7 +197,7 @@ def export_invoice(permanence=None, year=None, customer=None, producer=None, wb=
hide_column_deposit = True
for purchase in purchase_set:
print(slugify(purchase))
# print(slugify(purchase))
qty = purchase.quantity_invoiced
......
......@@ -646,7 +646,7 @@ def import_purchase_sheet(worksheet, permanence=None,
rule_of_3_target = row[_('rule of 3')]
if rule_of_3_target is not None:
rule_of_3_target = Decimal(rule_of_3_target).quantize(TWO_DECIMALS)
print(rule_of_3_target)
# print(rule_of_3_target)
if rule_of_3_target != rule_of_3_source:
max_purchase_counter = len(array_purchase)
if max_purchase_counter <= 1:
......
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