def handle_noargs(self, **options): # FIND ALL OF THE NOTIFICATIONS FIRST notifications = NotifyOutOfStock.objects.all() # NOW LET'S SEE IF ANY OF THOSE PRODUCTS ARE IN STOCK for n in notifications: product = n.product if product.in_stock(): try: # MAKE A DISCOUNT CODE discount = Discount.objects.create( discount_code=randint(10000, 99999), name=n.email, discount_value=0.05, expiry_date=(datetime.now() + timedelta(days=3)), is_active=True, ) # SEND AN EMAIL subject_line = "%s - back in stock at minrivertea.com" % product.name template = 'shop/emails/out_of_stock_notification.txt' _send_email( n.email, subject_line, template, extra_context={'product': product, 'discount_code': discount.discount_code}, ) # DELETE THE NOTIFICATION n.delete() except: pass # SKIP TO THE NEXT NOTIFICATION continue
def handle_noargs(self, **options): # FIND ALL OF THE NOTIFICATIONS FIRST notifications = NotifyOutOfStock.objects.all() # NOW LET'S SEE IF ANY OF THOSE PRODUCTS ARE IN STOCK for n in notifications: product = n.product if product.in_stock(): try: # MAKE A DISCOUNT CODE discount = Discount.objects.create( discount_code=randint(10000, 99999), name=n.email, discount_value=0.05, expiry_date=(datetime.now() + timedelta(days=3)), is_active=True, ) # SEND AN EMAIL subject_line = "%s - back in stock at minrivertea.com" % product.name template = 'shop/emails/out_of_stock_notification.txt' _send_email( n.email, subject_line, template, extra_context={ 'product': product, 'discount_code': discount.discount_code }, ) # DELETE THE NOTIFICATION n.delete() except: pass # SKIP TO THE NEXT NOTIFICATION continue
def notify_out_of_stock(request): if request.method == 'POST': form = NotifyOutOfStockForm(request.POST) if form.is_valid(): product = get_object_or_404(Product, id=form.cleaned_data['product']) notification = NotifyOutOfStock.objects.get_or_create( product=product, email=form.cleaned_data['email']) subject_line = "Thanks - you'll receive a notification when %s is back in stock" % product.name template = 'shop/emails/out_of_stock_notification_confirmation.txt' cancel_url = reverse('delete_notify_out_of_stock', args=[notification[0].id]) from emailer.views import _send_email _send_email( form.cleaned_data['email'], subject_line, template, extra_context={ 'product': product, 'email': form.cleaned_data['email'], 'url': cancel_url }, ) if request.is_ajax(): return HttpResponse('true') else: url = product.get_absolute_url() return HttpResponseRedirect(url) else: if request.is_ajax(): return HttpResponse('false') else: return HttpResponseRedirect('/') return HttpResponseRedirect('/')
def notify_out_of_stock(request): if request.method == "POST": form = NotifyOutOfStockForm(request.POST) if form.is_valid(): product = get_object_or_404(Product, id=form.cleaned_data["product"]) notification = NotifyOutOfStock.objects.get_or_create(product=product, email=form.cleaned_data["email"]) subject_line = "Thanks - you'll receive a notification when %s is back in stock" % product.name template = "shop/emails/out_of_stock_notification_confirmation.txt" cancel_url = reverse("delete_notify_out_of_stock", args=[notification[0].id]) from emailer.views import _send_email _send_email( form.cleaned_data["email"], subject_line, template, extra_context={"product": product, "email": form.cleaned_data["email"], "url": cancel_url}, ) if request.is_ajax(): return HttpResponse("true") else: url = product.get_absolute_url() return HttpResponseRedirect(url) else: if request.is_ajax(): return HttpResponse("false") else: return HttpResponseRedirect("/") return HttpResponseRedirect("/")