diff --git a/batiskaf/templates/jinja2/dtys/index.jinja b/batiskaf/templates/jinja2/dtys/index.jinja index e8c18b2..4d185aa 100644 --- a/batiskaf/templates/jinja2/dtys/index.jinja +++ b/batiskaf/templates/jinja2/dtys/index.jinja @@ -7,16 +7,11 @@ {% endblock %} {% block stylesheet %} - + {% endblock stylesheet %} @@ -29,7 +24,6 @@

D`TYS

-
{% for object in object_list %} diff --git a/batiskaf/templates/jinja2/includes/dtys_product_thumb.jinja b/batiskaf/templates/jinja2/includes/dtys_product_thumb.jinja index aef87d8..db46a39 100644 --- a/batiskaf/templates/jinja2/includes/dtys_product_thumb.jinja +++ b/batiskaf/templates/jinja2/includes/dtys_product_thumb.jinja @@ -3,23 +3,27 @@ {% set product = object.product.product %} {% set im = product.main_image()|thumbnail("420x420") %} - + Купить {{ product.title }} + +
-
+ +
{% if not object.is_stopped %} {{ object.current_price_format() }} {% else %} {{ object.price_stopped }} {% endif %}
+
Добавить в корзину + Добавить в корзину diff --git a/dtys/admin.py b/dtys/admin.py index 2cb272b..f45e872 100644 --- a/dtys/admin.py +++ b/dtys/admin.py @@ -5,11 +5,10 @@ from .models import DTYSModel @admin.register(DTYSModel) class DTYSModelAdmin(admin.ModelAdmin): + raw_id_fields = ('product',) list_display = ( 'id', 'product', 'is_stopped', 'is_public' ) - - prepopulated_fields = {"price_stopped": ("price_end",)} diff --git a/dtys/models.py b/dtys/models.py index a4f9eb1..e205e79 100644 --- a/dtys/models.py +++ b/dtys/models.py @@ -14,6 +14,8 @@ def get_change(current, previous): class DTYSModel(models.Model): + """Drop `Til You Shop Model""" + product = models.ForeignKey(ProductVariation) price_start = models.IntegerField() @@ -37,6 +39,9 @@ class DTYSModel(models.Model): def start_datetime(self): return datetime.combine(self.start_date, self.start_time) + def get_dec_by_sec(self): + return "%.1f" % (self.get_price_diff() / self.get_date_delta()) + def get_price_diff(self): return self.price_start - self.price_end diff --git a/dtys/views.py b/dtys/views.py index f41e300..0e54cbf 100644 --- a/dtys/views.py +++ b/dtys/views.py @@ -1,5 +1,10 @@ -from django.core.urlresolvers import reverse_lazy, reverse -from django.http import JsonResponse, HttpResponse, HttpResponseRedirect +# from django.core.urlresolvers import reverse_lazy, reverse +from django.http import ( + Http404, + HttpResponse, + HttpResponseRedirect, + JsonResponse, +) from django.utils import timezone from django.views.decorators.http import etag from django.views.generic import View, ListView, DetailView @@ -33,6 +38,9 @@ class DTYSListView(ListView): start_time__lte=timezone.now().time(), end_time__gte=timezone.now().time() ) + if qs.count() == 0: + raise Http404() + return qs @@ -46,19 +54,22 @@ class DTYSBuyView(DetailView): if obj.is_public and not obj.is_stopped: from store.cart import Cart, CartItem - from store.models import ProductVariation + # from store.models import ProductVariation # stop DTYS for this product obj.stop() + new_product_var = obj.product + + # Let's copy obj.product + new_product_var.pk = None + new_product_var.price = int(obj.price_stopped) + new_product_var.save() + # Add to cart cart = Cart(self.request) - try: - item_variant = ProductVariation.objects.get(pk=obj.product.pk) - except ProductVariation.DoesNotExist: - return HttpResponseRedirect('/') - cart.add_item(CartItem(item_variant, 1)) + cart.add_item(CartItem(new_product_var, 1)) return HttpResponseRedirect('/store/cart/') @@ -72,9 +83,9 @@ def latest_entry(request): ) if d.count() > 0: - pass + return "dtysETAG" + str(timezone.now().second) - return "myETAG" + str(timezone.now().second) + return "dtysETAG" @etag(latest_entry) @@ -108,6 +119,7 @@ class DTYSInfoView(View): if not i.is_stopped: dtys_item['current_price'] = i.current_price_format() + dtys_item['price_dec_in_sec'] = i.get_dec_by_sec() data['dtys_list'].append(dtys_item) diff --git a/static/js/dtys.js b/static/js/dtys.js index cbf348e..e572125 100644 --- a/static/js/dtys.js +++ b/static/js/dtys.js @@ -14,19 +14,39 @@ function getCookie(name) { return cookieValue; } +function countdownPrice(el, beginValue, decInSecond) { + var counter = el.find(".counter"); + + counter.text(beginValue); + + if (el.attr("data-tid") ) { + var tid = el.attr("data-tid") + clearInterval(tid); + } + + var delay = 1000; + var div = 3.0; + + var timerId = setInterval(function(){ + var val = parseFloat(counter.text()); + var new_val = (val - (decInSecond / div)).toFixed(); + counter.text(new_val); + }, delay / div); + + el.attr("data-tid", timerId); +} + +function countdownPriceStop(el) { + clearInterval(el.attr("data-tid")); +} + function get_price() { $.ajax({ url: "/dtys/info/", method: "GET", - // beforeSend: function(xhr) { - // // var csrftoken = getCookie("csrftoken"); - - // // if (csrftoken) { - // // xhr.setRequestHeader("X-CSRFToken", csrftoken); - // // } - // }, success: function(data, textStatus, jqXHR) { - // console.log("DTYS data", data); + console.log(data); + if (data.dtys_list.length > 0) { var active = data.dtys_list; @@ -39,11 +59,16 @@ function get_price() { product.addClass("stopped"); fake_add_to_cart_btn.show(); add_to_cart_btn.hide(); + + countdownPriceStop(product); + console.log("price countdown stopped", i.id); } else { product.removeClass("stopped"); product.find(".price").text(i.current_price); fake_add_to_cart_btn.hide(); add_to_cart_btn.show(); + + countdownPrice(product, i.current_price, i.price_dec_in_sec); } }); } @@ -56,7 +81,7 @@ function get_price() { var Updater = function(){ this.params = { - period: 3000, + period: 10000, url: '', onModified: function(data,x,modified){}, bgPause: false @@ -112,11 +137,13 @@ var Updater = function(){ $(function(){ var upd = new Updater(); + get_price(); + upd.init({ url: "/dtys/etg/", onModified: function(data, x, modified) { get_price(); - console.log(data, x, modified); + // console.log(data, x, modified); } });