You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
122 lines
3.0 KiB
122 lines
3.0 KiB
from django.core.cache import cache
|
|
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
|
|
|
|
from .models import DTYSModel
|
|
|
|
import logging
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
def latest_entry(request):
|
|
DTYSModel.objects.filter(
|
|
end_date__lte=timezone.now().date(),
|
|
end_time__lte=timezone.now().time()
|
|
).update(is_stopped=True)
|
|
|
|
d = DTYSModel.objects.filter(
|
|
is_public=True,
|
|
is_stopped=False
|
|
)
|
|
|
|
# if d.count() != cache.get("dtysETAGCount"):
|
|
# cache.set("dtysETAGCount", d.count())
|
|
# cache.set("dtysETAG", "ETAG" + str(timezone.now().second))
|
|
|
|
# return cache.get("dtysETAG")
|
|
return "ETAG" + str(timezone.now().second)
|
|
|
|
|
|
@etag(latest_entry)
|
|
def dtys_modified(request):
|
|
return HttpResponse('')
|
|
|
|
|
|
class DTYSListView(ListView):
|
|
model = DTYSModel
|
|
template_name = "dtys/index.jinja"
|
|
|
|
def get_queryset(self, *args, **kwargs):
|
|
qs = super(DTYSListView, self).get_queryset(*args, **kwargs)
|
|
qs = qs.filter(
|
|
is_public=True,
|
|
start_date__lte=timezone.now().date(),
|
|
end_date__gte=timezone.now().date(),
|
|
start_time__lte=timezone.now().time(),
|
|
end_time__gte=timezone.now().time()
|
|
)
|
|
if qs.count() == 0:
|
|
raise Http404()
|
|
|
|
return qs
|
|
|
|
|
|
class DTYSBuyView(DetailView):
|
|
model = DTYSModel
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
obj = self.get_object()
|
|
|
|
log.debug(obj.id)
|
|
|
|
if obj.is_public and not obj.is_stopped:
|
|
from store.cart import Cart, CartItem
|
|
# from store.models import ProductVariation
|
|
|
|
# stop DTYS for this product
|
|
obj.stop()
|
|
|
|
# Let's copy obj.product
|
|
obj.product.price = int(obj.price_stopped)
|
|
obj.product.save()
|
|
|
|
# Add to cart
|
|
cart = Cart(self.request)
|
|
|
|
cart.add_item(CartItem(obj.product, 1))
|
|
|
|
return HttpResponseRedirect('/store/cart/')
|
|
|
|
return HttpResponseRedirect('/dtys/')
|
|
|
|
|
|
class DTYSInfoView(View):
|
|
"""Ajax info"""
|
|
|
|
def get(self, request):
|
|
data = {
|
|
"dtys_list": []
|
|
}
|
|
|
|
dtys_list = DTYSModel.objects.filter(
|
|
is_public=True,
|
|
start_date__lte=timezone.now(),
|
|
end_date__gte=timezone.now(),
|
|
start_time__lte=timezone.now().time()
|
|
)
|
|
|
|
for i in dtys_list:
|
|
if i.end_time <= timezone.now().time():
|
|
i.stop()
|
|
|
|
dtys_item = {
|
|
'id': i.id,
|
|
'stopped': i.is_stopped,
|
|
'stopped_price': i.price_stopped
|
|
}
|
|
|
|
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)
|
|
|
|
return JsonResponse(data)
|
|
|