def customer_offercode_side_tag(offercode): offer = offercode.offer return { "title": offer.title, "description": offer.description, "location": offer.merchant.print_address(), "biz_name": offer.merchant.business_name, "time_left": pretty_date(offer.expired_time, True), "STATIC_URL": settings.STATIC_URL, "redeem_time": pretty_datetime(offercode.redeem_time), }
def customer_offercode_side_tag(offercode): offer = offercode.offer return { "title": offer.title, "description": offer.description, "location": offer.merchant.print_address(), "biz_name" : offer.merchant.business_name, "time_left": pretty_date(offer.expired_time, True), "STATIC_URL": settings.STATIC_URL, "redeem_time": pretty_datetime(offercode.redeem_time), }
def handle_redeem(self, su, from_number, command, text, parsed): self.handle_lack_params(from_number, command, text, parsed) code = parsed[1] client_number = parsed[2] offercode = self.check_offercode(code, from_number) phone = self.validate_number( client_number, from_number) # offercode expired if offercode == -1: sender_msg = t.render(templates["MERCHANT"]["REDEEM_EXPIRED"], {"offer": code,}) self.notify(from_number, sender_msg) else: if offercode.offer.merchant != su.merchant: msg = t.render(templates["MERCHANT"]["REDEEM_WRONG_MERCHANT"], {"code": code,}) self.notify(from_number, msg) raise CommandError("Merchant attempts to redeem an offer he does not own") try: current_time = datetime.now() offercode_obj = OfferCode.objects.filter(expiration_time__gt=current_time, time_stamp__lt=current_time).get(code__iexact=offercode.code) try: p = CustomerPhone.objects.get(number__icontains=phone) customer = p.customer if offercode_obj.customer==customer: if not offercode_obj.redeem_time: offercode_obj.redeem_time=current_time offercode_obj.save() # informing merchant + trans sender_msg = t.render(templates["MERCHANT"]["REDEEM_SUCCESS"], { "offer_code": offercode_obj.code, "customer": offercode_obj.customer }) mtransaction = Transaction.objects.create(time_stamp = current_time, dst = su.merchant, offercode = offercode_obj, offer = offercode_obj.offer, ttype = "MOR") mtransaction.execute() self.notify(from_number,sender_msg) # informing customer + trans customer_msg = t.render(templates["CUSTOMER"]["REDEEM_SUCCESS"], { "merchant": su.merchant.business_name }) ctransaction = Transaction.objects.create(time_stamp = current_time, dst = offercode_obj.customer, offercode = offercode_obj, offer = offercode_obj.offer, ttype = "COR") ctransaction.execute() self.notify(phone, customer_msg) # informing forwarders if offercode_obj.forwarder: ftransaction = Transaction.objects.create(time_stamp = current_time, dst = offercode_obj.forwarder, offercode = offercode_obj, offer = offercode_obj.offer, ttype = "CFR") ftransaction.execute() else: sender_msg = t.render(templates["MERCHANT"]["REDEEM_CODE_REUSE"], { "offer_code": offercode_obj.code, "customer": offercode_obj.customer, "time": pretty_datetime(offercode_obj.redeem_time), }) self.notify(from_number, sender_msg) OfferCodeAbnormal(time_stamp=current_time, ab_type="DR", offercode=offercode_obj).save() raise CommandError("Customer attempts to reuse a code") else: sender_msg = t.render(templates["MERCHANT"]["REDEEM_WRONG_CUSTOMER"],{ "offer_code":offercode_obj.code, "customer": offercode_obj.customer,}) self.notify(from_number, sender_msg) raise CommandError("Customer attempts to redeem a code he does not own") except ObjectDoesNotExist: sender_msg = t.render(templates["MERCHANT"]["REDEEM_INVALID_CUSTOMER_NUM"], { "offer_code": offercode.code, "phone" : phone,}) self.notify(from_number, sender_msg) except MultipleObjectsReturned, e: sms_logger.exception("\"%s\" causes an error: " % text) except ObjectDoesNotExist: sender_msg = t.render(templates["MERCHANT"]["REDEEM_INVALID_CODE"], { "offer_code": offercode.code,}) self.notify(from_number, sender_msg) OfferCodeAbnormal(time_stamp=datetime.now(), ab_type="IV", invalid_code=offercode.code).save() except MultipleObjectsReturned, e: sms_logger.exception("\"%s\" causes an error: " % text)