示例#1
0
	def save(self, user_id):
		username = self.cleaned_data["username"].lower()	
		address = self.cleaned_data["address1"].lower()
		u = User.objects.get(pk = user_id)
		u.username = username
		u.save()
		zipcode_obj = ZipCode.objects.get(code=self.cleaned_data["zip_code"])

		phone = parse_phone_number(self.cleaned_data["phone"], zipcode_obj.city.region.country.code)
		customer = u.shoppleyuser.customer
		if (not customer.customerphone) or (not customer.customerphone.number==phone):
		#phone!= u.shoppleyuser.customer.phone:
			t = TxtTemplates()
			msg = t.render(TxtTemplates.templates["CUSTOMER"]["VERIFY_PHONE"], {})
			sms_notify(phone, msg)
			
		c = u.shoppleyuser.customer
		c.address_1 = address
		p,created = CustomerPhone.objects.get_or_create(customer=c, defaults={"number":phone,})
		if not created:
			p.number = phone
			p.save()

		c.daily_limit = self.cleaned_data["daily_limit"]
		c.zipcode = zipcode_obj
		c.save()
		c.set_location_from_address()
示例#2
0
def verify_phone(shoppleyUser, isVerify):
	t = TxtTemplates()
	
	if isVerify:
		shoppleyUser.verified_phone = shoppleyUser.VERIFIED_YES
		msg = t.render(TxtTemplates.templates["CUSTOMER"]["VERIFY_SUCCESS"], {})

	else:
		shoppleyUser.verified_phone = shoppleyUser.VERIFIED_NO
		msg = t.render(TxtTemplates.templates["CUSTOMER"]["VERIFY_NO_SUCCESS"], {})
		
	shoppleyUser.save()
	print "in verify_phone", shoppleyUser.verified_phone
	if shoppleyUser.is_merchant():
		sms_notify(shoppleyUser.phone, msg)
	else:
		sms_notify(shoppleyUser.customer.customerphone.number, msg)
示例#3
0
	def save(self):
		# assign email as user name
		username = self.cleaned_data["username"].lower()
		email = self.cleaned_data["email"].lower()
		password = self.cleaned_data["password1"]
		
		if self.cleaned_data["confirmation_key"]:
			from friends.models import JoinInvitation # @@@ temporary fix for issue 93
			try:
				join_invitation = JoinInvitation.objects.get(confirmation_key = self.cleaned_data["confirmation_key"])
				confirmed = True
			except JoinInvitation.DoesNotExist:
				confirmed = False
		else:
			confirmed = False
		
		# @@@ clean up some of the repetition below -- DRY!
		
		if confirmed:
			if email == join_invitation.contact.email:
				new_user = User.objects.create_user(username, email, password)
				join_invitation.accept(new_user) # should go before creation of EmailAddress below
				new_user.message_set.create(message=ugettext(u"Your email address has already been verified"))
				# already verified so can just create
				EmailAddress(user=new_user, email=email, verified=True, primary=True).save()
			else:
				new_user = User.objects.create_user(username, "", password)
				join_invitation.accept(new_user) # should go before creation of EmailAddress below
				if email:
					new_user.message_set.create(message=ugettext(u"Confirmation email sent to %(email)s") % {'email': email})
					EmailAddress.objects.add_email(new_user, email)
		else:
			new_user = User.objects.create_user(username, "", password)
			if email:
				new_user.message_set.create(message=ugettext(u"Confirmation email sent to %(email)s") % {'email': email})
				EmailAddress.objects.add_email(new_user, email)
		
		if settings.ACCOUNT_EMAIL_VERIFICATION:
			new_user.is_active = False
			new_user.save()

		zipcode_obj = ZipCode.objects.get(code=self.cleaned_data["zip_code"])
		phone = parse_phone_number(self.cleaned_data["phone"], zipcode_obj.city.region.country.code)
		t = TxtTemplates()
		msg = t.render(TxtTemplates.templates["CUSTOMER"]["VERIFY_PHONE"], {})
		sms_notify(phone, msg)

		new_customer = Customer.objects.create(user=new_user,
						address_1=self.cleaned_data["address_1"],
						# address_2=self.cleaned_data["address_2"],
				#		phone=phone,
						zipcode=zipcode_obj,
						verified=True,
					)
		new_customer.set_location_from_address()
		p = CustomerPhone.objects.create(number=phone ,customer = new_customer)
		return username, password # required for authenticate()
示例#4
0
	def save(self):
		user = self.request.user
		fbuser = self.request.facebook.graph.get_object("me")
		user.username = "******" + self.request.facebook.uid + "|" +fbuser['first_name'] + " " + fbuser['last_name']
		print user, user.username
		user.save()

		email = fbuser['email']
		EmailAddress.objects.get_or_create(user=user, email=fbuser['email'], verified=True, primary=True)

		phone = parse_phone_number(self.cleaned_data["phone"])
		t = TxtTemplates()
		msg = t.render(TxtTemplates.templates["CUSTOMER"]["VERIFY_PHONE"], {})
		#sms_notify(phone, msg)

		code = self.cleaned_data["zip_code"]
		zipcode = ZipCode.objects.get(code=code)
		c = Customer.objects.create(user=user, is_fb_connected=True, zipcode=zipcode)
		p = CustomerPhone.objects.create(customer=c, number=phone)
示例#5
0
			return actives[0]
	
	def handle_unverified_number(self, su, from_number, text):
		if su and su.is_customer() and su.verified_phone!=0:
			if text=="1":
				verify_phone(su, True)
			elif text=="0":
				verify_phone(su, False)
			else:
				msg = t.render(templates["CUSTOMER"]["VERIFY_PHONE"],{})
				self.notify(from_number, msg)
			raise CommandError ("Unverified phone")
		

	PARAMS = {
			REDEEM[0]: (3, t.render(templates["MERCHANT"]["REDEEM_PARAM_ERRORS"])),
                        REDEEM[1]: (3, t.render(templates["MERCHANT"]["REDEEM_PARAM_ERRORS"])),

			ZIPCODE[0]: (2, t.render(templates["MERCHANT"]["ZIPCODE_COMMAND_ERROR"])),
                        ZIPCODE[1]: (2, t.render(templates["MERCHANT"]["ZIPCODE_COMMAND_ERROR"])),
			ZIPCODE[2]: (2, t.render(templates["MERCHANT"]["ZIPCODE_COMMAND_ERROR"])),


			OFFER[0]: (2, t.render(templates["MERCHANT"]["OFFER_COMMAND_ERROR"])),
                        OFFER[1]: (2, t.render(templates["MERCHANT"]["OFFER_COMMAND_ERROR"])),

			IWANT[0]: (2, t.render(templates["CUSTOMER"]["IWANT_COMMAND_ERROR"])),
                        IWANT[1]: (2, t.render(templates["CUSTOMER"]["IWANT_COMMAND_ERROR"])),

			FORWARD[0]: (3, t.render(templates["CUSTOMER"]["FORWARD_COMMAND_ERROR"])),
                        FORWARD[1]: (3, t.render(templates["CUSTOMER"]["FORWARD_COMMAND_ERROR"])),
示例#6
0
def customer_register(email, username, zipcode, phone, password, address, method):
	data = {}
	
	# sanitize inputs
	if email is not None:
		email = check_email(email)
		if email is None:
			data["result"] = -1
			data["result_msg"] = "Email address is used by another user."
			return data
	
	if zipcode is not None:
		zipcode = check_zipcode(zipcode)
		if zipcode is None:
			data["result"] = -2
			data["result_msg"] = "Zip Code is invalid or not in the system."
			return data
	
	if phone is not None:
		phone = check_phone(phone)
		if phone is None:
			data["result"] = -3
			data["result_msg"] = "Phone number is used by another user."
			return data
	
	if username is None:
		if email is not None:
			username = email
		elif phone is not None:
			username = phone
		else:
			data["result"] = -4
			data["result_msg"] = "Either email address, phone number or username is required."
			return data
			
	is_random_password = False
	if password is None:
		s = string.lowercase + string.digits
		password = ''.join(random.sample(s,6))
		is_random_password = True
	
	try:
		user = User.objects.create_user(username, "", password)
		user.save()
	except IntegrityError:
		data["result"] = -5
		data["result_msg"] = "'" + username + "' is used by the other user."
		return data
	
	# create customer information
	c = Customer(user=user, zipcode=zipcode,  verified=True)
	c.save()

	CustomerPhone.objects.create(customer=c, number = phone)
	c.set_location_from_address()
	
	num_merchants = c.count_merchants_within_miles()
	
	t = TxtTemplates()
	args = {"email": email, "number": num_merchants}
	if is_random_password:
		args["password"] = password,
		welcome_msg = t.render(TxtTemplates.templates["CUSTOMER"]["SIGNUP_SUCCESS"], args)
	else:
		welcome_msg = t.render(TxtTemplates.templates["CUSTOMER"]["SIGNUP_SUCCESS_NO_PASSWORD"], args)
	
	if email is not None:
		e = EmailAddress(user=user, email=email, verified=True, primary=True)
		e.save()
		send_mail('Welcome to Shoppley', welcome_msg, '*****@*****.**', [email], fail_silently=True)		
		
	if phone is not None:
		if method == "SMS":
			sms_notify(phone, welcome_msg)
		else:
			# send verification sms
			verify_msg = t.render(TxtTemplates.templates["CUSTOMER"]["VERIFY_PHONE"], {})
			sms_notify(phone, verify_msg)
		
	data["result"] = 1
	data["result_msg"] = "User registered successfully."
	data["username"] = username
	data["password"] = password
	
	return data;
示例#7
0
    def handle_noargs(self, **options):
        """
			read all the offers that have not been distributed, find target users for
			each offer and control how many offers individual gets
		"""

        t = TxtTemplates()

        #####################################
        # process offer distribute
        #####################################
        process_areas = Offer.objects.filter(
            is_processing=True).values('merchant__zipcode').distinct()

        black_words = BlackListWord.objects.all().values_list('word',
                                                              flat=True)
        # for each area
        for z in process_areas:
            # for each offer in current area
            for o in Offer.objects.filter(
                    merchant__zipcode=z['merchant__zipcode'],
                    is_processing=True):
                """
				# check if merchant has enough credits
				"""
                print "processing: ", o
                from worldbank.models import Transaction
                allowed_number = int(o.merchant.balance /
                                     abs(Transaction.points_table["MOD"]))
                #print "balance=" ,self.merchant.balance
                #print "allowed_number", allowed_number
                if allowed_number == 0:
                    # if there isn't enough balance
                    receipt_msg = t.render(
                        TxtTemplates.templates["MERCHANT"]
                        ["OFFER_NOTENOUGH_BALANCE"],
                        {"points": o.merchant.balance})
                    o.is_processing = False
                    o.save()
                    o.delete()
                    continue
                """
				# check if offer has words in the black list
				"""
                blacked = set(
                    o.title.lower().split()).intersection(black_words)
                if len(blacked) == 0:
                    # if valid content
                    """
					# select target size
					"""
                    target_size = 20 if allowed_number > 20 else allowed_number

                    # TODO: need to select 80% of followers and 20% of non-followers

                    target_list = []

                    # divide up user base in this area and distribute
                    users = o.merchant.get_active_customers_miles(RADIUS)

                    num_users = len(users)
                    if num_users > target_size:
                        target_list = random.sample(users, target_size)
                    elif num_users > 0:
                        target_list = list(users)
                    else:
                        # no target users that have not received offer
                        # select users again among those previously received but haven't
                        # filled their quota
                        users = Customer.objects.filter(
                            verified=True,
                            active=True,
                            zipcode=z['merchant__zipcode']).values_list(
                                'pk', flat=True)

                        num_users = users.count()
                        if num_users > target_size:
                            target_list = random.sample(users, target_size)
                        elif num_users > 0:
                            target_list = list(users)
                    print "target", target_list

                    # distribute offer: generate offer codes
                    sentto = o.gen_offer_codes(
                        Customer.objects.filter(pk__in=target_list))
                    print "sentto:", sentto
                    #print "count=" , self.offercode_set.all().count()
                    for c in o.offercode_set.all():
                        offer_msg = t.render(
                            TxtTemplates.templates["CUSTOMER"]
                            ["OFFER_RECEIVED"], {
                                "merchant": o.merchant.business_name,
                                "title": o.title,
                                "code": c.code
                            })

                        #print c.customer.customerphone.number, offer_msg
                        success = self.notify(c.customer.customerphone.number,
                                              offer_msg)

                        if success:
                            transaction = Transaction.objects.create(
                                time_stamp=datetime.now(),
                                offer=o,
                                offercode=c,
                                dst=o.merchant,
                                ttype="MOD")
                            transaction.execute()

                    if sentto == 0:
                        # no customers
                        receipt_msg = t.render(
                            TxtTemplates.templates["MERCHANT"]
                            ["OFFER_NO_CUSTOMER"],
                            {"code": o.gen_tracking_code()})

                    else:
                        """
						# successfully sent offers
						"""
                        receipt_msg = t.render(
                            TxtTemplates.templates["MERCHANT"]
                            ["OFFER_SUCCESS"], {
                                "time": pretty_datetime(o.time_stamp),
                                "offer": o,
                                "number": sentto,
                                "code": o.gen_tracking_code(),
                            })

                else:
                    """
					# black list the offer
					"""
                    bo = BlackListOffer(offer=o)
                    bo.save()
                    for b_word in blacked:
                        bo.words.add(BlackListWord.objects.get(word=b_word))
                    bo.save()
                    receipt_msg = t.render(
                        TxtTemplates.templates["MERCHANT"]["OFFER_BLACKLIST"],
                        {"unacceptable": ','.join(blacked)})
                if o.starter_phone:

                    self.notify(o.starter_phone.number, receipt_msg)
                else:
                    self.notify(o.merchant.phone, receipt_msg)
                """
				# Update offer parameters
				"""
                o.num_init_sentto = sentto
                o.expired_time = o.starting_time + timedelta(
                    minutes=o.duration)
                o.is_processing = False
                o.save()

        #####################################
        # process offer redistribute
        #####################################
        process_areas = Offer.objects.filter(
            redistribute_processing=True).values(
                'merchant__zipcode').distinct()

        # for each area
        for z in process_areas:
            # for each offer in current area
            for o in Offer.objects.filter(
                    merchant__zipcode=z['merchant__zipcode'],
                    redistribute_processing=True):
                """
				# check if merchant has enough credits
				"""
                from worldbank.models import Transaction
                allowed_number = int(o.merchant.balance /
                                     abs(Transaction.points_table["MOD"]))
                #print "balance=" ,self.merchant.balance
                #print "allowed_number", allowed_number
                if allowed_number == 0:
                    # if there isn't enough balance
                    receipt_msg = t.render(
                        TxtTemplates.templates["MERCHANT"]
                        ["REOFFER_NOTENOUGH_BALANCE"],
                        {"points": o.merchant.balance})
                    o.redistribute_processing = False
                    o.save()
                    continue

                # customers who have received the offers
                old_offercodes = o.offercode_set.all()
                # extend old customers
                for oc in old_offercodes:
                    #print "before reset" , pretty_datetime(oc.expiration_time), " duration=", self.duration
                    oc.expiration_time = datetime.now() + timedelta(
                        minutes=o.duration)
                    #print "time added" , datetime.now() + timedelta(minutes=self.duration)
                    oc.save()
                    """
					# NOTE: not send confirmation to save txt messages
					offer_msg = t.render(TxtTemplates.templates["CUSTOMER"]["REOFFER_EXTENSION"],{
						"code": oc.code,
						"title": self.title,
						"merchant": self.merchant.business_name,
						"address": self.merchant.print_address(),
						"expiration": pretty_datetime(oc.expiration_time),})
					self.notify(oc.customer.phone, offer_msg)
					"""

                old_pks = old_offercodes.values_list('customer', flat=True)
                """
				# select target size
				"""
                target_size = 20 if allowed_number > 20 else allowed_number

                # TODO: need to select 80% of followers and 20% of non-followers

                target_list = []

                # divide up user base in this area and distribute
                users = o.merchant.get_active_customers_miles(RADIUS, old_pks)
                num_users = len(users)
                if num_users > target_size:
                    target_list = random.sample(users, target_size)
                elif num_users > 0:
                    target_list = list(users)
                else:
                    # no target users that have not received offer
                    # select users again among those previously received but haven't
                    # filled their quota
                    users = Customer.objects.exclude(pk__in=old_pks).filter(
                        verified=True,
                        active=True,
                        zipcode=z['merchant__zipcode']).values_list('pk',
                                                                    flat=True)

                    num_users = users.count()
                    if num_users > target_size:
                        target_list = random.sample(users, target_size)
                    elif num_users > 0:
                        target_list = list(users)

                # distribute offer
                resentto = o.gen_offer_codes(
                    Customer.objects.filter(pk__in=target_list))
                #print "count=" , self.offercode_set.all().count()

                for oc in o.offercode_set.filter(customer__pk__in=target_list):
                    oc.expiration_time = datetime.now() + timedelta(
                        minutes=o.duration)
                    oc.save()
                    offer_msg = t.render(
                        TxtTemplates.templates["CUSTOMER"]
                        ["REOFFER_NEWCUSTOMER_RECEIVED"], {
                            "merchant": o.merchant.business_name,
                            "title": o.title,
                            "code": oc.code
                        })

                    success = self.notify(oc.customer.customerphone.number,
                                          offer_msg)

                    if success:
                        transaction = Transaction.objects.create(
                            time_stamp=datetime.now(),
                            offer=o,
                            offercode=oc,
                            dst=o.merchant,
                            ttype="MOD")
                        transaction.execute()

                if resentto == 0:
                    # no customers
                    receipt_msg = t.render(
                        TxtTemplates.templates["MERCHANT"]
                        ["REOFFER_ZERO_CUSTOMER"],
                        {"code": o.trackingcode.code})
                else:
                    """
					# successfully sent offers
					"""
                    receipt_msg = t.render(
                        TxtTemplates.templates["MERCHANT"]["REOFFER_SUCCESS"],
                        {
                            "title": o.title,
                            "resentto": resentto,
                        })

                if o.starter_phone:
                    self.notify(o.starter_phone.number, receipt_msg)
                else:
                    self.notify(o.merchant.phone, receipt_msg)
                """
				# Update offer parameters
				"""

                #print "*************************** SENT RESEND OFFER *************************"
                o.num_resent_to = resentto
                o.redistribute_processing = False
                o.redistributable = False
                o.expired_time = datetime.now() + timedelta(minutes=o.duration)
                o.save()
示例#8
0
	def handle_noargs(self, **options):
		"""
			read all the offers that have not been distributed, find target users for
			each offer and control how many offers individual gets
		"""

		t = TxtTemplates()

		#####################################	
		# process offer distribute
		#####################################	
		process_areas = Offer.objects.filter(is_processing=True).values('merchant__zipcode').distinct()

		black_words = BlackListWord.objects.all().values_list('word', flat=True)
		# for each area
		for z in process_areas: 
			# for each offer in current area
			for o in Offer.objects.filter(merchant__zipcode=z['merchant__zipcode'], is_processing=True):
				"""
				# check if merchant has enough credits
				"""
				print "processing: ", o
				from worldbank.models import Transaction
				allowed_number =int( o.merchant.balance/abs(Transaction.points_table["MOD"]))
				#print "balance=" ,self.merchant.balance
				#print "allowed_number", allowed_number
				if allowed_number == 0:
					# if there isn't enough balance 
					receipt_msg = t.render(TxtTemplates.templates["MERCHANT"]["OFFER_NOTENOUGH_BALANCE"], {"points":o.merchant.balance})
					o.is_processing = False
					o.save()
					o.delete()
					continue

				"""
				# check if offer has words in the black list
				"""
				blacked = set(o.title.lower().split()).intersection(black_words) 
				if len(blacked) == 0:
					# if valid content

					"""
					# select target size
					"""
					target_size = 20 if allowed_number > 20 else allowed_number 

					# TODO: need to select 80% of followers and 20% of non-followers

					target_list = []

					# divide up user base in this area and distribute
					users=o.merchant.get_active_customers_miles(RADIUS)

					num_users = len(users)
					if num_users > target_size:
						target_list = random.sample(users, target_size)
					elif num_users > 0:
						target_list = list(users)
					else:
						# no target users that have not received offer
						# select users again among those previously received but haven't
						# filled their quota
						users=Customer.objects.filter(verified=True, active=True, zipcode=z['merchant__zipcode']).values_list('pk', flat=True)

						num_users = users.count()
						if num_users > target_size:
							target_list = random.sample(users, target_size)
						elif num_users > 0:
							target_list = list(users)
					print "target", target_list

					# distribute offer: generate offer codes
					sentto = o.gen_offer_codes(Customer.objects.filter(pk__in=target_list))	
					print "sentto:" , sentto
					#print "count=" , self.offercode_set.all().count()
					for c in o.offercode_set.all():
						offer_msg = t.render(TxtTemplates.templates["CUSTOMER"]["OFFER_RECEIVED"],{ "merchant":o.merchant.business_name, "title":o.title, "code":c.code })		
						
						#print c.customer.customerphone.number, offer_msg
						success = self.notify(c.customer.customerphone.number, offer_msg)
						
						if success :
							transaction = Transaction.objects.create(time_stamp=datetime.now(),
										offer = o,
										offercode = c,
										dst = o.merchant,
										ttype = "MOD")
							transaction.execute()

					if sentto==0 :
						# no customers
						receipt_msg = t.render(TxtTemplates.templates["MERCHANT"]["OFFER_NO_CUSTOMER"], {"code":o.gen_tracking_code()})

					else:
						"""
						# successfully sent offers
						"""
						receipt_msg = t.render(TxtTemplates.templates["MERCHANT"]["OFFER_SUCCESS"], {
							"time": pretty_datetime(o.time_stamp),
							"offer": o,
							"number": sentto,
							"code": o.gen_tracking_code(),
						})

				else:
					"""
					# black list the offer
					"""
					bo = BlackListOffer(offer=o)
					bo.save()
					for b_word in blacked:
						bo.words.add(BlackListWord.objects.get(word=b_word))
					bo.save()
					receipt_msg = t.render(TxtTemplates.templates["MERCHANT"]["OFFER_BLACKLIST"], {						"unacceptable": ','.join(blacked)
					})
				if o.starter_phone:
				
					self.notify(o.starter_phone.number, receipt_msg)
				else:
					self.notify(o.merchant.phone, receipt_msg)

				"""
				# Update offer parameters
				"""
				o.num_init_sentto = sentto
				o.expired_time = o.starting_time + timedelta(minutes=o.duration)
				o.is_processing = False
				o.save()


		#####################################	
		# process offer redistribute
		#####################################	
		process_areas = Offer.objects.filter(redistribute_processing=True).values('merchant__zipcode').distinct()

		# for each area
		for z in process_areas: 
			# for each offer in current area
			for o in Offer.objects.filter(merchant__zipcode=z['merchant__zipcode'], redistribute_processing=True):
				"""
				# check if merchant has enough credits
				"""
				from worldbank.models import Transaction
				allowed_number =int( o.merchant.balance/abs(Transaction.points_table["MOD"]))
				#print "balance=" ,self.merchant.balance
				#print "allowed_number", allowed_number
				if allowed_number == 0:
					# if there isn't enough balance 
					receipt_msg = t.render(TxtTemplates.templates["MERCHANT"]["REOFFER_NOTENOUGH_BALANCE"], {"points":o.merchant.balance})
					o.redistribute_processing = False
					o.save()
					continue


				# customers who have received the offers
				old_offercodes = o.offercode_set.all()
				# extend old customers
				for oc in old_offercodes:
					#print "before reset" , pretty_datetime(oc.expiration_time), " duration=", self.duration
					oc.expiration_time = datetime.now() + timedelta(minutes=o.duration)
					#print "time added" , datetime.now() + timedelta(minutes=self.duration)
					oc.save()
					"""
					# NOTE: not send confirmation to save txt messages
					offer_msg = t.render(TxtTemplates.templates["CUSTOMER"]["REOFFER_EXTENSION"],{
						"code": oc.code,
						"title": self.title,
						"merchant": self.merchant.business_name,
						"address": self.merchant.print_address(),
						"expiration": pretty_datetime(oc.expiration_time),})
					self.notify(oc.customer.phone, offer_msg)
					"""
	
				old_pks = old_offercodes.values_list('customer',flat=True)


				"""
				# select target size
				"""
				target_size = 20 if allowed_number > 20 else allowed_number 

				# TODO: need to select 80% of followers and 20% of non-followers

				target_list = []

				# divide up user base in this area and distribute
				users=o.merchant.get_active_customers_miles(RADIUS, old_pks)
				num_users = len(users)
				if num_users > target_size:
					target_list = random.sample(users, target_size)
				elif num_users > 0:
					target_list = list(users)
				else:
					# no target users that have not received offer
					# select users again among those previously received but haven't
					# filled their quota
					users=Customer.objects.exclude(pk__in=old_pks).filter(verified=True, active=True, zipcode=z['merchant__zipcode']).values_list('pk', flat=True)

					num_users = users.count()
					if num_users > target_size:
						target_list = random.sample(users, target_size)
					elif num_users > 0:
						target_list = list(users)

				# distribute offer
				resentto = o.gen_offer_codes(Customer.objects.filter(pk__in=target_list))	
				#print "count=" , self.offercode_set.all().count()

				for oc in o.offercode_set.filter(customer__pk__in=target_list):
					oc.expiration_time = datetime.now() + timedelta(minutes=o.duration)
					oc.save()
					offer_msg = t.render(TxtTemplates.templates["CUSTOMER"]["REOFFER_NEWCUSTOMER_RECEIVED"],{ "merchant":o.merchant.business_name, "title":o.title, "code":oc.code })
					
					success=	self.notify(oc.customer.customerphone.number, offer_msg)
						
					if success :
						transaction = Transaction.objects.create(time_stamp=datetime.now(),
									offer = o,
									offercode = oc,
									dst = o.merchant,
									ttype = "MOD")
						transaction.execute()

				if resentto==0 :
					# no customers
					receipt_msg = t.render(TxtTemplates.templates["MERCHANT"]["REOFFER_ZERO_CUSTOMER"], {"code": o.trackingcode.code})
				else:
					"""
					# successfully sent offers
					"""
					receipt_msg = t.render(TxtTemplates.templates["MERCHANT"]["REOFFER_SUCCESS"], {

						"title" : o.title,
						"resentto": resentto,
						})

				if o.starter_phone:
					self.notify(o.starter_phone.number, receipt_msg)
				else:
					self.notify(o.merchant.phone, receipt_msg)

				"""
				# Update offer parameters
				"""

				#print "*************************** SENT RESEND OFFER *************************"
				o.num_resent_to = resentto
				o.redistribute_processing = False
				o.redistributable = False 
				o.expired_time = datetime.now() + timedelta(minutes=o.duration)
				o.save()
示例#9
0
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))