Example #1
0
	def get(self):
		logging.debug('Hit the Authorize Complete Handler')
		client_id = 'HD3ZXKL5LX4TFCARNIZO1EG2S5BV5UHGVDVEJ2AXB4UZHEOU'
		secret = 'LB3J4Q5VQWZPOZATSMOAEDOE5UYNL5P44YCR0FCPWFNXLR2K'
		redirect = 'https://levr-production.appspot.com/foursquare/authorize/complete'
		code = self.request.get('code')
		
		#make request for token
		url = "https://foursquare.com/oauth2/access_token?client_id="+client_id+"&client_secret="+secret+"&grant_type=authorization_code&redirect_uri="+redirect+"&code="+code
		result = urlfetch.fetch(url=url)
		token = json.loads(result.content)['access_token']
		
		#grab more user details
		url = 'https://api.foursquare.com/v2/users/self?v=20120920&oauth_token='+token
		result = urlfetch.fetch(url=url)
		user = json.loads(result.content)
		logging.debug(levr_utils.log_dict(user))
		self.response.out.write(levr_utils.log_dict(user))
Example #2
0
	def get(self):
		action = self.request.get('action')
		success	= self.request.get('success')
		
		logging.debug(action)
		logging.debug(action)
		template_values = {
						'action'	: action,
						'success'	: success
		}
		logging.debug(levr_utils.log_dict(template_values))
		template = jinja_environment.get_template('templates/login.html')
		self.response.out.write(template.render(template_values))
Example #3
0
	def get(self):
		try:
			#grab all the deals with current status == pending
			deal = levr.CustomerDeal.all().filter('been_reviewed =', False).get()
			
			#dictify deal
			if deal:
				#logging.info(deal['dateEnd'])
				#get the first matching entity and parse into template values
				logging.debug(levr_utils.log_model_props(deal))
				business = levr.Business.get(deal.businessID)
				if not business:
					raise Exception('business does not exist')
				ninjaID = deal.key().parent()
				logging.debug(ninjaID)
				ninja = levr.Customer.get(ninjaID)
				#sort tags for easy readin
				tags = deal.tags
				tags.sort()
				
				template_values = {
					"deal"		: deal,
					"img_big"	: levr_utils.URL+'/phone/img?dealID='+enc.encrypt_key(deal.key())+'&size=dealDetail',
					"img_small"	: levr_utils.URL+'/phone/img?dealID='+enc.encrypt_key(deal.key())+'&size=list',
					"tags"		: tags,
					"business"	: business,
					"dealID"	: enc.encrypt_key(deal.key()),
					"businessID": enc.encrypt_key(business.key()),
					"ninja"		: ninja
				}
				logging.debug(levr_utils.log_dict(template_values))
				
				template = jinja_environment.get_template('templates/admin_pending.html')
				self.response.out.write(template.render(template_values))
			else:
				self.response.out.write('No pending deals!')
		except Exception,e:
			levr.log_error(e)
			self.response.out.write('Error fetching deal. See logs.')
Example #4
0
def phoneFormat(deal,use,primary_cat=None):
	#dealID is used in a number of places
	dealID = enc.encrypt_key(str(deal.key()))
#	logging.info(deal.key())
#	logging.info(dealID)
	#dealText
	dealText = deal.deal_text
		
	#dealTextExtra
	if deal.deal_type == 'bundle':
		logging.debug('flag bundle')
		dealTextExtra = '(with purchase of ' + deal.secondary_name + ')'
	else:
		logging.debug('flag single')
		dealTextExtra = ''
		
	if use == 'list' or use == 'myDeals' or use == 'widget':
		#list is search results
		#mydeals is for the list of a users uploaded deals
		#widget is for the html iframe for merchants
		data = {"dealID"		: dealID,
				"imgURL"		: 'http://beta.levr-production.appspot.com/phone/img?dealID='+dealID+'&size=list',
				"imgURLlarge"	: 'http://beta.levr-production.appspot.com/phone/img?dealID='+dealID+'&size=dealDetail',
				"geoPoint"		: deal.geo_point,
				"vicinity"		: deal.vicinity,
				"dealText"  	: dealText,
				"dealTextExtra" : dealTextExtra,
				"description"	: deal.description,
				"businessName"	: deal.business_name,
				"primaryCat"	: primary_cat,
				"isExclusive"	: deal.is_exclusive}
		if use == 'myDeals':
			#shows list deal information AND statistics
			deal_parent = db.get(deal.key().parent())
			logging.debug(deal_parent)
			data.update({
				"gateRequirement"	: deal.gate_requirement,						#The number of redemptions needed to earn a dollar on this deal
				"gatePaymentPer"	: deal.gate_payment_per,						#The dollar amount we pay for each gate
				"earnedTotal"		: deal.earned_total, 							#The amount of money that this deal has earned so far
				"paymentMax"		: deal.gate_max*deal.gate_payment_per,			#The most money we will pay them for this deal
				"paidOut"			: deal.paid_out,								#The amount of money that this deal has earned to date
				"dealStatus"		: deal.deal_status,								#active,pending,rejected,expired
				"dateEnd"			: deal.date_end.__str__()[:10],					#The date this deal becomes inactive
				"moneyAvailable"	: deal_parent.money_available,					#The amount of money that the NINJA has available for redemption
				"ninjaMoneyEarned"	: deal_parent.money_paid,						#The amount of money that the ninja has earned to date
				"weightedRedeems"	: deal.count_redeemed % deal.gate_requirement,	#The number of redemptions they need to earn another dollar
				"dealCountRedeemed"	: deal.count_redeemed,							#The number of times that the deal has been redeemed
				"shareURL"			: levr_utils.create_share_url(deal)				#The URL for them to share
			})
		if use == 'widget':
			data.update({
				"description"	: deal.description,
			})
	elif use == 'deal':
		#view deal information screen
		#grab business 
#		logging.info(deal.businessID)
#		b = db.get(deal.businessID)
		#uploaded by a user
		data = {"dealID"		: dealID,
				"imgURL"	  	: 'http://beta.levr-production.appspot.com/phone/img?dealID='+dealID+'&size=dealDetail',
				"dealText"  	: dealText,
				"dealTextExtra" : dealTextExtra,
				"businessName"	: deal.business_name,
				"vicinity"		: deal.vicinity,
				"description"	: deal.description,
				"isExclusive"	: deal.is_exclusive}
				
	elif use == 'dealsScreen':
		deal_parent = db.get(deal.key().parent())
		logging.debug(deal_parent.kind())
		if deal_parent.kind() == 'Customer':
			#deal has a ninja parent.
			ninja = deal_parent
			alias = ninja.alias
			logging.debug(ninja)
		else:
#			business = deal_parent
			alias = ''
			
		data = {"barcodeURL"	: 'http://beta.levr-production.appspot.com/phone/img?dealID='+dealID+'&size=dealDetail',
				"ninjaName"		: alias,
				"isExclusive"	: deal.is_exclusive}
	elif use == 'manage':
		data = {
			"dealID"		:dealID,
			"dealText"		:dealText,
			"dealTextExtra"	:dealTextExtra,
			"secondaryName"	:deal.secondary_name,
			"businessName"	:deal.business_name,
			"vicinity"		:deal.vicinity,
			"description"	:deal.description,
			"isExclusive"	:deal.is_exclusive,
			"imgURLLarge"	:'http://beta.levr-production.appspot.com/phone/img?dealID='+dealID+'&size=dealDetail',
			"imgURLSmall"	:'http://beta.levr-production.appspot.com/phone/img?dealID='+dealID+'&size=list',

			}
	data.update({'geoPoint':str(deal.geo_point)})
	logging.info(levr_utils.log_dict(data))
	return data
Example #5
0
	def post(self):
		try:
			logging.info('uploadDeal')
			uid	= enc.decrypt_key(self.request.get('uid'))
			user = levr.Customer.get(uid)
			logging.debug(levr_utils.log_model_props(user,['alias','email']))
			logging.debug(self.request.params)
			#make sure than an image is uploaded
			logging.debug(self.get_uploads())
			if self.get_uploads(): #will this work?
				upload	= self.get_uploads()[0]
				blob_key= upload.key()
				img_key = blob_key
			else:
				raise Exception('Image was not uploaded')
			
			
			#screen for businessID to determine which mode of upload we are receiving
			if self.request.get('businessID'):
				logging.debug('targeted business')
				#we are on iphone
				origin = 'phone_existing_business'
				params = {
					'uid'				: self.request.get('uid'),
					'business'			: self.request.get('businessID'),
					'deal_description'	: self.request.get('deal_description'),
					'deal_line1'		: self.request.get('deal_line1'),
					'distance'			: self.request.get('distance'), #is -1 if unknown = double
					'img_key'			: img_key
					}
				
#				(share_url,deal_entity) = levr_utils.dealCreate(params,'phone_existing_business')
			else:
				logging.debug('untargeted business')
				#we are on android
				origin = 'phone_new_business'
				params = {
					'uid'				: self.request.get('uid'),
					'business_name'		: self.request.get('businessName'),
					'geo_point'			: self.request.get('geoPoint'),
					'vicinity'			: self.request.get('vicinity'),
					'types'				: self.request.get('types'),
					'deal_description'	: self.request.get('deal_description'),
					'deal_line1'		: self.request.get('deal_line1'),
					'distance'			: self.request.get('distance'), #is -1 if unknown = double
					'img_key'			: img_key
					}
			
			
			
			#create the deal using the origin specified
			(share_url,deal_entity) = levr_utils.dealCreate(params,origin)
			
			#grab deal information for sending back to phone
			deal = levr.phoneFormat(deal_entity,'list')
			
			
			#Return Info..
			toEcho = {"success":True,"data":{"shareURL":share_url,"deal":deal}}
			logging.debug(levr_utils.log_dict(toEcho))
			self.response.out.write(json.dumps(toEcho))
		except:
			levr.log_error(self.request.body)
			toEcho = {"success":False}#,"data":{"shareURL":share_url}}
			self.response.out.write(json.dumps(toEcho))
Example #6
0
				deal.put()
				
				logging.debug(levr_utils.log_model_props(deal,['gate_max','has_been_shared']))

				
				notifications = user.get_notifications()
				toEcho = {"success":True,"notifications":notifications}
			else:
				raise Exception('Unrecognized action')
			############ END OF ACTION FILE PART!!! JSONIFY!!!
		except:
			levr.log_error(self.request.body)
			toEcho = {"success":False}
		finally:
			try:
				logging.debug(levr_utils.log_dict(toEcho))
				logging.debug(json.dumps(toEcho))
				self.response.out.write(json.dumps(toEcho))
			except:
				#catches the case where toEcho cannot be parsed as json
				self.response.out.write(json.dumps({"success":False}))
				levr.log_error('json is not parseable')


class uploadDeal(blobstore_handlers.BlobstoreUploadHandler):
	def post(self):
		try:
			logging.info('uploadDeal')
			uid	= enc.decrypt_key(self.request.get('uid'))
			user = levr.Customer.get(uid)
			logging.debug(levr_utils.log_model_props(user,['alias','email']))
Example #7
0
	def post(self):
		logging.debug('Foursquare push request received!')
		logging.debug(self.request.body)
		checkin = json.loads(self.request.get('checkin'))
		secret = self.request.get('secret')
		logging.debug(checkin)
		
		#verify that the secret passed matches ours
		hc_secret = 'LB3J4Q5VQWZPOZATSMOAEDOE5UYNL5P44YCR0FCPWFNXLR2K'
		if hc_secret != secret:
			#raise an exception
			logging.debug('SECRETS DO NOT MATCH')
		
		#go look in our database for a matching foursquare venue id
		business = levr.Business.gql('WHERE foursquare_id = :1',checkin["venue"]["id"]).get()
		#business = levr.Business.get('ahFzfmxldnItcHJvZHVjdGlvbnIQCxIIQnVzaW5lc3MY-dIBDA')
		
		#initialize the response object
		reply = {
			'CHECKIN_ID'		: checkin['id'],
			'text'				: 'Hi there! We seem to be having some issues. Back soon!',
			'url'				: 'http://www.levr.com',
			'contentID'			: 'BWANHHPAHAHA'
		}
		
		if business:	#business found
			#for deal in levr.Deal().all().filter('businessID =', str(business.key())).run():
			q = levr.Deal.gql("WHERE businessID = :1 AND deal_status = :2 ORDER BY count_redeemed DESC",str(business.key()),'active')
			numdeals = q.count()
			if numdeals > 1:	#many deals found
				topdeal = q.get()
				reply['text'] = "There are "+str(numdeals)+" deals here! Click to browse."
				reply['url'] = '' #deeplink into dealResults screen
			elif numdeals == 1:	#only one deal found
				topdeal = q.get()
				reply['text'] = topdeal.deal_text+". Click to redeem."
				reply['url'] = '' #deeplink into dealDetail screen
			else:	#no deals found
				reply['text'] = "See any deals? Pay it forward: click to upload."
				reply['url'] = '' #deeplink into deal upload screen
		else:			#no business found
			#ask pat for all the deals within walking distance
			url = 'http://www.levr.com/phone'
			ll = str(checkin['venue']['location']['lat'])+','+str(checkin['venue']['location']['lng'])
			request_point = levr.geo_converter(ll)
			precision = 6
			results = levr_utils.get_deals_in_area(['all'],request_point,precision)

			if len(results) > 0:
				reply['text'] = "There are "+str(len(results))+" deals near you - click to view."
				reply['url'] = '' #deeplink into deal upload screen
			else:
				reply['text'] = "See any deals? Pay it forward: click to upload."
				reply['url'] = '' #deeplink into deal upload screen
			
		url = 'https://api.foursquare.com/v2/checkins/'+reply['CHECKIN_ID']+'/reply?v=20120920&oauth_token='+'PZVIKS4EH5IFBJX1GH5TUFYAA3Z5EX55QBJOE3YDXKNVYESZ'
		logging.debug(url)
		result = urlfetch.fetch(url=url,
								payload=urllib.urlencode(reply),
								method=urlfetch.POST)
		logging.debug(levr_utils.log_dict(result.__dict__))