def test_offer(request): data = {} #u = request.user u = User.objects.get(email="*****@*****.**") if u.shoppleyuser.is_customer(): return HttpResponseRedirect( reverse("offer.views.offer_home") ) if request.method == "POST": form = StartOfferForm(request.POST) if form.is_valid(): offer = form.save(commit=False) offer.merchant = u.shoppleyuser.merchant offer.time_stamp = datetime.now() if form.cleaned_data["now"]: offer.starting_time = datetime.now()+timedelta(minutes=5) offer.save() # send out the offer num_sent = offer.distribute() data["result"] = "1" data["offer_id"] = offer.id data["num_sent"] = num_sent return JSONHttpResponse(data) else: data["result"] = "-1" else: data["result"] = "-2" form = StartOfferForm() data["form"] = str(form) return JSONHttpResponse(data)
def start_offer(request): data = {} u = request.user if u.shoppleyuser.is_customer(): return HttpResponseRedirect( reverse("offer.views.offer_home") ) if request.method == "POST": form = StartOfferForm(request.POST) if form.is_valid(): offer = form.save(commit=False) offer.merchant = u.shoppleyuser.merchant offer.time_stamp = datetime.now() if form.cleaned_data["now"]: offer.starting_time = datetime.now()+timedelta(minutes=5) offer.title = self.cleaned_data.get("title") offer.description = self.cleaned_data.get("description") if len(offer.title) == 0: offer.title = description[:64] if form.cleaned_data.get("offer_radio") == 0: offer.percentage = self.cleaned_data.get("percentage") offer.dollar_off = None elif form.cleaned_data.get("offer_radio") == 1: offer.dollar_off = self.cleaned_data.get("dollar_off") offer.percentage = None offer.save() # send out the offer num_sent = offer.distribute() data["result"] = "1" data["offer"] = offer data["num_sent"] = num_sent # past and current offers # TODO: would be more efficient if there was a way to filter active and past offers separately instead of doing it on template data["offers"] = Offer.objects.filter(merchant=u.shoppleyuser.merchant).order_by("-time_stamp") data["form"] = StartOfferForm() return render_to_response("offer/start_offer.html", data, context_instance=RequestContext(request)) else: data["result"] = "-1" else: data["offers"] = Offer.objects.filter(merchant=u.shoppleyuser.merchant).order_by("-time_stamp") form = StartOfferForm(initial={'offer_radio':0}) data["form"] = form return render_to_response("offer/start_offer.html", data, context_instance=RequestContext(request))
def merchant_start_offer(request,template = "offer/merchant_offer_start.html"): user = request.user try: su = user.shoppleyuser if su.is_customer(): return HttpResponseRedirect(reverse("offer.views.offer_home")) except ShoppleyUser.DoesNotExist: return HttpResponseRedirect(reverse("home")) if request.method == 'POST': form = StartOfferForm(request.POST, request.FILES) if form.is_valid(): user = request.user merchant = user.shoppleyuser.merchant #offer_type = form.cleaned_data["offer_radio"] value = float(form.cleaned_data["value"]) description = form.cleaned_data["description"] title = form.cleaned_data["title"] if form.cleaned_data["now"]: d = datetime.now() d = d + timedelta(minutes=5) d = d.replace(second=0, microsecond=0) time_stamp = d else: d = form.cleaned_data["date"] t = form.cleaned_data["time"] time_stamp = datetime.combine(d,t) max_offers = form.cleaned_data["max_offers"] duration = form.cleaned_data["duration"] discount_obj = form.cleaned_data["discount"] discount_obj = discount_obj.split(':::') discount = float(discount_obj[0]) discount_type = discount_obj[1] dollar_off = 0 percentage = 0 discount_str = "None" if discount_type == '%': dollar_off = discount * value percentage = int(discount) elif discount_type == '$': dollar_off = discount if value ==0: percentage = 0 else: percentage = int(100.0*discount / value) if discount_type != 'custom': discount_str = ''.join(discount_obj) expiration = time_stamp + timedelta(minutes=duration) Offer(merchant = merchant, title = title, description = description, time_stamp = time_stamp, starting_time = time_stamp, duration = duration , max_offers = max_offers, expired_time =expiration , offer_value= value, dollar_off = dollar_off, percentage=percentage).save() #return HttpResponseRedirect(reverse("offer.views.offer_home")) t = TxtTemplates() templates = TxtTemplates.templates txt_preview =t.render(templates["CUSTOMER"]["INFO"], { "offercode": "xxxx", "description":title, "merchant": merchant, "expiration": expiration, }) return render_to_response("offer/offer_confirmation.html", {"offer": title, "business_name": merchant.business_name, "expiration": expiration, "address": merchant.print_address(), "value": value, "discount": discount_str, "starting_time": time_stamp, "max_offers": max_offers, "description" :description, "txt_preview": txt_preview, },context_instance=RequestContext(request)) else: ten_min_later = datetime.now() +timedelta( minutes=5) ten_min_later = ten_min_later.time().replace(second=0,microsecond=0) form = StartOfferForm(initial={"value": '0',"time": ten_min_later, "date": datetime.today()}) return render_to_response(template,{"form": form,}, context_instance=RequestContext(request))