dtys app improve js counter

remotes/origin/stepan-k
Stepan Krapivin 8 years ago
parent 47019cf459
commit 0b37076dfd
  1. 16
      batiskaf/templates/jinja2/dtys/index.jinja
  2. 15
      batiskaf/templates/jinja2/includes/dtys_product_thumb.jinja
  3. 3
      dtys/admin.py
  4. 5
      dtys/models.py
  5. 32
      dtys/views.py
  6. 47
      static/js/dtys.js

@ -7,16 +7,11 @@
{% endblock %}
{% block stylesheet %}
<style>
.dtys__product {
margin-bottom: 20pt;
border: 1px dashed lightgray;
padding: 4pt;
}
.dtys .stopped {
border: 2px solid red;
}
</style>
<style>
.dtys .stopped {
border: 2px solid red;
}
</style>
{% endblock stylesheet %}
@ -29,7 +24,6 @@
<div class="index-goods">
<div class="row">
<h1>D`TYS</h1>
<input type="hidden" value="{{ csrf_token }}">
<div class="dtys">
{% for object in object_list %}

@ -3,23 +3,27 @@
{% set product = object.product.product %}
{% set im = product.main_image()|thumbnail("420x420") %}
<a href="{{ product.get_absolute_url() }}">
<a>
<img src="/static/{{ im.url|watermark('medium-photo') }}"
class="img-responsive" alt="Купить {{ product.title }}"
title="Купить {{ product.title }}" width="210" height="210"></a>
</a>
<div class="caption">
<div class="title">
<a href="{{ product.get_absolute_url() }}">
{{ product.title }} ({{ object.product.product.id }})
</a>
<span>
{{ product.title }}
</span>
</div>
<div class="price">
<div class="price counter">
{% if not object.is_stopped %}
{{ object.current_price_format() }}
{% else %}
{{ object.price_stopped }}
{% endif %}
</div>
<div class="in-cart">
<a class="btn btn-warning btn-block add-to-cart"
href="/dtys/buy/{{ object.id }}/"
@ -27,6 +31,7 @@
<span><i class="glyphicon glyphicon-shopping-cart"
aria-hidden="true"></i> Добавить в корзину</span>
</a>
<span class="btn btn-warning btn-block fake-add-to-cart" {% if not object.is_stopped %}style="display: none"{% endif %}>
<span><i class="glyphicon glyphicon-shopping-cart"
aria-hidden="true"></i> Добавить в корзину</span>

@ -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",)}

@ -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

@ -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)

@ -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);
}
});

Loading…
Cancel
Save