import os from django.conf import settings from django.http import ( Http404, HttpResponse, HttpResponseRedirect, JsonResponse, ) from django.core.cache import cache 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__) DTYS_UPDATE_FILE = os.path.join(settings.MEDIA_ROOT, "dtys_update") def update_dtys_etag(): fhandle = open(DTYS_UPDATE_FILE, 'a') try: os.utime(DTYS_UPDATE_FILE, None) except FileNotFoundError: open(DTYS_UPDATE_FILE, 'a').close() finally: fhandle.close() 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()) update_dtys_etag() if not os.path.exists(DTYS_UPDATE_FILE): update_dtys_etag() modified = str(os.path.getmtime(DTYS_UPDATE_FILE)) return modified @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_queryset(self, *args, **kwargs): qs = super(DTYSBuyView, 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 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 from .tasks import return_dtys update_dtys_etag() # stop DTYS for this product obj.stop() ProductVariation.objects.filter(id=obj.product.id)\ .update(price=obj.price_stopped) 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_dtys.apply_async( (obj.id, ), countdown=settings.RESUME_DTYS_DELAY_SECONDS) 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)