Example #1
0
	def post(self, brand_requested, action):
		global brand
		analyticsProps = configparsers.loadPropertyFile('analytics')
		gaAccount = analyticsProps.get('GA', 'ga_account')
		if brand_requested is not None and brand_requested != '':
			
			user = self.request.POST.get('session')

			if utils.validate_brand(brand_requested):
				viewState = 'start'
				brand = brand_requested
				section_content = utils.create_section_content(brand)
				session_key = None
				app_version = os.environ['CURRENT_VERSION_ID']

				brandProps = configparsers.loadPropertyFile('brand')
				brandDisplayName = brandProps.get(brand, 'display_name')
			
				self.context['sessionKey']=session_key
				self.context['sections']=section_content
				self.context['viewState']=viewState
				self.context['brand']=brand
				self.context['user']=user
				self.context['brandDisplayName']=brandDisplayName
				self.context['ga_account']=gaAccount
				template = 'index'
				self.render(template)
			else:
				self.error(500)
		else:
			self.error(500)
Example #2
0
def report_all_surveys(requestedBrand, emailRecipient):
	try:
		today = datetime.date.today()
		ioCsvOuput = utils.generate_survey_csv(requestedBrand)
		"""
		# [ST] Pre Py2.7 Upgrade
		mailProps = configparsers.loadPropertyFile('mail')
		message = mail.EmailMessage()
		message.sender = mailProps.get('NewSurvey', 'sender')
		message.to = emailRecipient
		message.subject = mailProps.get('NewSurvey', 'subject_report')
		message.body='New report'
		message.attachments=[(str(os.environ['HTTP_HOST'])+'-'+str(today.isoformat())+'.csv', ioCsvOuput.getvalue())]
		message.send()
		"""
		# [ST] Post Py2.7 Upgrade
		mailProps = configparsers.loadPropertyFile('mail')
		mail.send_mail_to_admins(
			sender=mailProps.get('NewSurvey', 'sender'),
			subject=mailProps.get('NewSurvey', 'subject_report'),
			body='New report',
			**dict(
				attachments=[(str(os.environ['HTTP_HOST'])+'-'+str(today.isoformat())+'.csv', ioCsvOuput.getvalue())]
			)
		)
	except (Exception, apiproxy_errors.ApplicationError, apiproxy_errors.OverQuotaError), e:
		logging.error(e)
		logging.error('report_all_surveys() : mail could not be sent as the 24-hour quota has been exceeded')
		raise e
Example #3
0
  def post(self):
	
	startAjaxRequestQuota = quota.get_request_cpu_usage()
	
	"""
	Get the config properties
	"""
	config_properties = configparsers.loadPropertyFile('config')
	
	destination = self.request.get("destination")
	startDateRaw = self.request.get("startDate")
	ratingRaw = self.request.get("rating")
	numberOfNightsRaw = self.request.get("nights")
	rating = None
	startDate = None
	
	logging.debug(startDateRaw)
	if ratingRaw is not None:
		rating = True

	if startDateRaw is not None and startDateRaw is not '':
		startDate = startDateRaw.split('-')
		try:
			dateTime = datetime.datetime(int(startDate[0]), int(startDate[1]), int(startDate[2]))
			startDate = dateTime
			endDateTimeDelta = datetime.timedelta(days=int(numberOfNightsRaw))
			endDate = startDate + endDateTimeDelta		
		except ValueError, e:
			logging.error(e)
			logging.error("AjaxAPIHandler_v3 : Invalid date values or date format")
Example #4
0
def ean_get_hotel_details(hotelId, requestObject):
    config_properties = configparsers.loadPropertyFile("config")
    requestXML = (
        "<HotelInformationRequest><hotelId>@TOKEN@</hotelId><options>DEFAULT</options></HotelInformationRequest>"
    )
    requestXML = requestXML.replace("@TOKEN@", hotelId)

    urlArgs = dict()
    apiKey = config_properties.get("EAN", "api_key")
    apiSecret = config_properties.get("EAN", "shared_secret")
    sig = generate_ean_signature(apiKey, apiSecret)
    logging.info("EAN sig")
    logging.info(sig)
    urlArgs["sig"] = sig
    urlArgs["cid"] = config_properties.get("EAN", "cid")
    urlArgs["apiKey"] = apiKey
    customerIpAddress = requestObject.remote_addr
    customerUserAgent = requestObject.headers.get("User-Agent")
    urlArgs["customerIpAddress"] = customerIpAddress
    urlArgs["customerUserAgent"] = customerUserAgent
    urlArgs["customerSessionId"] = str(uuid.uuid4())
    urlArgs["locale"] = config_properties.get("EAN", "locale")
    urlArgs["_type"] = config_properties.get("EAN", "type")
    urlArgs["xml"] = requestXML
    urlAgrsEncoded = urllib.urlencode(urlArgs)

    return urlAgrsEncoded
Example #5
0
	def get(self):
		brandProps = configparsers.loadPropertyFile('brand')
		brand = 'razorfish'
		brandDisplayName = brandProps.get(brand, 'display_name')
		adminUser = users.get_current_user()
		userData = None
		
		if adminUser is not None:
			userData = dict()
			userData['email'] = adminUser.email()
			userData['nickname'] = adminUser.nickname()
			userData['id'] = adminUser.user_id()
		
		# Retrieve all Clients, with the most latest created listed first
		clients = user_model.User.gql("ORDER BY created, account, name DESC")
		
		client_survey_url_brand = 'razorfish'
		app_id = utils.get_app_id()
		if app_id.find('mcdonalds') != -1:
			client_survey_url_brand = 'mcdonalds'

		self.context['clients']=clients
		self.context['adminUser']=userData
		self.context['brandDisplayName']=brandDisplayName
		self.context['brand']=brand
		self.context['client_survey_url_brand']=client_survey_url_brand
		template = 'clients'
		self.render(template)
Example #6
0
def get_hotspots():
    hotspot_properties = configparsers.loadPropertyFile("hotspots")
    hotspotSections = hotspot_properties.sections()
    hotspotSection = choice(hotspotSections)
    hotspot = dict()
    for x, y in hotspot_properties.items(hotspotSection):
        hotspot[x] = hotspot_properties.getfloat(hotspotSection, x)
    return hotspot
Example #7
0
	def get(self, brand_requested, user_id_requested):
		global brand
		analyticsProps = configparsers.loadPropertyFile('analytics')
		gaAccount = analyticsProps.get('GA', 'ga_account')
		brandProps = configparsers.loadPropertyFile('brand')
		home_content = utils.get_home_content(brandProps)
		if brand_requested is not None and brand_requested != '' and user_id_requested is not None and user_id_requested != '' and brandProps.has_section(brand_requested):
			brand = brand_requested
			brandDisplayName = brandProps.get(brand, 'display_name')
			section_content = utils.create_section_content(brand)
			session_key = None
			
			if utils.validate_client_user(brand_requested, user_id_requested) and utils.validate_brand(brand_requested):
				viewState = 'new'
				self.context['home_content']=home_content
				self.context['sessionKey']=session_key
				self.context['sections']=section_content
				self.context['viewState']=viewState
				self.context['brand']=brand
				self.context['user']=user_id_requested
				self.context['brandDisplayName']=brandDisplayName
				self.context['ga_account']=gaAccount
				template = 'index'
				self.render(template)
			else:
				logging.warning('No valid Brand or User found')
				viewState = 'error-404'
				self.context['viewState']=viewState
				self.context['brand']=brand
				self.context['user']=user_id_requested
				self.context['brandDisplayName']=brandDisplayName
				self.context['ga_account']=gaAccount
				template = 'error'
				self.response.set_status(404)
				self.render(template)

		else:
			viewState = 'error-404'
			self.context['viewState']=viewState
			self.context['brand']='razorfish'
			self.context['brandDisplayName']='Razorfish'
			self.context['ga_account']=gaAccount
			template = 'error'
			self.response.set_status(404)
			self.render(template)
Example #8
0
def geonames_credentials():
	"""
		Return the Geonames username for any Geonames webservice requests
	"""
	config_properties = configparsers.loadPropertyFile('geonames')
	if config_properties.has_option('Geonames', 'username'):
		return config_properties.get('Geonames', 'username')
	else:
		return None
Example #9
0
def generate_survey_csv(brand):
	try:
		out=cStringIO.StringIO()
		surveyProps = configparsers.loadPropertyFile('survey_'+brand)
		
		"""
		Generate CSV file output VALUES
		"""
		surveys = datastore.get_surveys_by_brand(brand)

		# [ST] DEBUG: Returning just the first Survey, to test the Admin mail.send_to_admins() service 
		# due to quota size exception on the attachment
		# surveys = [surveys.get()]
		
		sortedSections = None
		counter = 0
		for survey in surveys:
			sections = dict()
			surveyResults = list()

			descendants = db.query_descendants(survey)
			for descendant in descendants:
				section = descendant
				_id = section.kind()
				# If the Section Model has properties, and it exists in the Survey Properties ConfigParser file (e.g. is not the User model)...
				if hasattr(section, 'properties') and surveyProps.has_section(_id):
					sections[_id] = dict()
					sections[_id]['index'] = surveyProps.getint(_id, 'index')
					sections[_id]['section'] = section
					surveyResults.append(sections[_id])

			sortedSections = sorted(surveyResults, key=itemgetter('index'), reverse=False)
			
			if counter == 0:
				# for each section within the survey
				out.write('Created,Client,Account,')
				for section in sortedSections:
					for question, data in section['section'].properties().items():
						out.write('\"'+str(surveyProps.get(section['section'].kind(), 'name')+' : '+data.verbose_name)+'\",')					
				out.write('\n')
			
			out.write(str(survey.created.date().isoformat())+',')
			out.write(str(survey.user_id.name)+',')
			out.write(str(survey.user_id.account)+',')
			for section in sortedSections:
				for question, data in section['section'].properties().items():
					answer = getattr(section['section'], question)
					# CGI Escape the output of the answer, in case there are '<', '>', '&' or '"' characters in there. The double quote will break the CSV formatting
					out.write('\"'+cgi.escape(str(answer), quote=True)+'\",')
			out.write('\n')
			counter += 1
			
		return out
	except (Exception), e:
		logging.error(e)
		raise e
Example #10
0
def get_brands():
	brandProps = configparsers.loadPropertyFile('brand')	
	brands = list()
	sections = brandProps.sections()
	for section in sections:
		brand = dict()
		brand['id'] = section
		brand['name'] = brandProps.get(section, 'display_name')
		brands.append(brand)
	return brands
Example #11
0
	def get(self):
		
		"""
		Get the config properties
		"""
		config_properties = configparsers.loadPropertyFile('config')
		
		hotspot = utils.get_hotspots()
		
		app_version = os.environ['CURRENT_VERSION_ID']
		
		widescreen = 'true'
		viewType = self.request.get("viewType")
		destination = self.request.get("destination")
		price = self.request.get("priceMax")
		startDate = self.request.get("startDate")
		priceSort = self.request.get("priceSort")
		ratingSort = self.request.get("ratingSort")
		nights = self.request.get("nights")
		hotelBrand = self.request.get("brand")
		servicePath = requestEANHotelList
		brand = "lastminute"
		urlPath = self.request.path
		
		if urlPath is not None and len(urlPath) > 1:
			brand = urlPath.replace('/','')
			
			if brand == "lastminute":
				#requestAjaxAPI
				servicePath = requestEANHotelList
			elif brand == "expedia":
				servicePath = requestEANHotelList
			elif brand == "razorfish":
				servicePath = requestEANHotelList
		if len(price) > 0:
			price = float(price)
		
		destinationDisplayName = destination
		tripAdvisorDestination = destination
		bookmarks = self.request.get("bookmarks").split(',')
		maptype = self.request.get("maptype")
		contenttype = self.request.get("contenttype")
		contenttype = 'minimized'
		if utils.destination_display_names.has_key(destination):
			destinationDisplayName = utils.destination_display_names[destination]
		
		if tripadvisor_image_paths.has_key(destination):
			tripAdvisorDestination = tripadvisor_image_paths[destination]
		try:
			facebookAppId = config_properties.get('Facebook', 'app_id')
			facebookAccessToken = config_properties.get('Facebook', 'access_token')
			analytics_key = config_properties.get('Google', 'analytics_key')
			twitterAppKey = config_properties.get('Twitter', 'anywhere_api_key')
		except ConfigParser.NoSectionError, e:
			logging.error(e)
Example #12
0
    def post(self):    
		hotelid = self.request.get("hotelid")
		hotelDetailsRequestArgs = utils.ean_get_hotel_details(hotelid, self.request)
		config_properties = configparsers.loadPropertyFile('config')
		try: 
			requestDetailsURL = config_properties.get('EAN', 'xml_url_hotelinfo')
			u = urllib.urlopen(""+requestDetailsURL+"%s" % hotelDetailsRequestArgs)
			r = u.read()
			logging.debug(r)
		except (apiproxy_errors.ApplicationError, DeadlineExceededError), e:
			logging.exception(e)
Example #13
0
def ean_get_hotel_list_url(arrivalDate, departureDate, city, hotelBrand, requestObject):
    config_properties = configparsers.loadPropertyFile("config")

    # input format is YYY-MM-DD
    arrivalDateList = arrivalDate.split("-")
    arrivalDateList.reverse()
    # Set US format date MM/DD/YYYYY
    arrivalDateFormatted = arrivalDateList[1] + "/" + arrivalDateList[0] + "/" + arrivalDateList[2]

    # input format is YYY-MM-DD
    departureDateList = departureDate.split("-")
    departureDateList.reverse()
    # Set US format date MM/DD/YYYYY
    departureDateFormatted = departureDateList[1] + "/" + departureDateList[0] + "/" + departureDateList[2]

    requestXML = (
        "<HotelListRequest><propertyName>"
        + hotelBrand
        + "</propertyName><arrivalDate>"
        + arrivalDateFormatted
        + "</arrivalDate><departureDate>"
        + departureDateFormatted
        + "</departureDate><RoomGroup><Room><numberOfAdults>2</numberOfAdults></Room></RoomGroup><city>"
        + city
        + "</city><numberOfResults>25</numberOfResults><options>"
        + config_properties.get("EAN", "option")
        + "</options></HotelListRequest>"
    )

    urlArgs = dict()
    urlArgs["cid"] = config_properties.get("EAN", "cid")
    apiKey = config_properties.get("EAN", "api_key")
    apiSecret = config_properties.get("EAN", "shared_secret")
    sig = generate_ean_signature(apiKey, apiSecret)
    logging.info("EAN sig")
    logging.info(sig)
    urlArgs["sig"] = sig
    urlArgs["apiKey"] = apiKey

    # Extract IP address and User Agent from the Request Headers
    customerIpAddress = requestObject.remote_addr
    customerUserAgent = requestObject.headers.get("User-Agent")
    urlArgs["customerIpAddress"] = customerIpAddress
    urlArgs["customerUserAgent"] = customerUserAgent
    urlArgs["customerSessionId"] = str(uuid.uuid4())

    urlArgs["locale"] = config_properties.get("EAN", "locale")
    urlArgs["minorRev"] = config_properties.get("EAN", "minor_rev")
    urlArgs["currencyCode"] = config_properties.get("EAN", "currency_code")
    urlArgs["_type"] = config_properties.get("EAN", "type")
    urlArgs["xml"] = requestXML
    urlAgrsEncoded = urllib.urlencode(urlArgs)

    return urlAgrsEncoded
Example #14
0
	def post(self):
		surveyKey = None
		arguments = dict()
		analyticsProps = configparsers.loadPropertyFile('analytics')
		gaAccount = analyticsProps.get('GA', 'ga_account')
		brand_requested = self.request.POST.get('brand')
		user_id = self.request.POST.get('session')
		try:
			brandProps = configparsers.loadPropertyFile('brand')
			
			
			
			if brand_requested is not None and brand_requested != '':
				brand_requested = brand_requested

				
				for arg in self.request.arguments():
					arguments[str(arg)] = self.request.get(arg)

				surveyKey = datastore.set_all_arguments(user_id, brand_requested, arguments)
				
				self.redirect('/'+brand_requested+'/success')

		except (CapabilityDisabledError,BadValueError,ValueError), e:
			logging.error("SurveySubmitHandler() : Error saving data")
			logging.error(e)
			sectionContent = utils.create_section_content(brand_requested)
			# If a Survey has failed to be stored in the datastore we won't receive the surveyKey,
			#  so we need to send the Form arguments to the Mail service
			mailservice.announce_survey_error(arguments)
			# [ST]TODO: We need to have stored the User's answer data, to return the form to the current state
			userAnswers = dict()
			viewState = 'error-500'
			brandDisplayName = brandProps.get(brand_requested, 'display_name')
			self.context['viewState']=viewState
			self.context['brand']=brand_requested
			self.context['brandDisplayName']=brandDisplayName
			self.context['ga_account']=gaAccount
			template = 'error'
			self.response.set_status(500)
			self.render(template)
Example #15
0
	def get(self, brand_requested):
		analyticsProps = configparsers.loadPropertyFile('analytics')
		gaAccount = analyticsProps.get('GA', 'ga_account')
		global brand
		if brand_requested is not None and brand_requested != '':
			if '/' in brand_requested:
				requestSplit = brand_requested.split('/')
				brand_requested = requestSplit[0]
			else:
				brand_requested = brand_requested
			
			if utils.validate_brand(brand_requested):
				brand = brand_requested
				brandProps = configparsers.loadPropertyFile('brand')
				brandDisplayName = brandProps.get(brand, 'display_name')
		
				self.context['brandDisplayName']=brandDisplayName
				self.context['brand']=brand
				self.context['ga_account']=gaAccount
				template = 'success'
				self.render(template)
Example #16
0
	def post(self, wildCard):
		logging.debug('WildCardHandler')
		analyticsProps = configparsers.loadPropertyFile('analytics')
		gaAccount = analyticsProps.get('GA', 'ga_account')
		viewState = 'error-404'
		self.context['viewState']=viewState
		self.context['brand']='razorfish'
		self.context['brandDisplayName']='Razorfish'
		self.context['ga_account']=gaAccount
		template = 'error'
		self.response.set_status(404)
		self.render(template)
Example #17
0
def announce_survey_error(arguments):
	try:
		today = datetime.date.today()
		mailProps = configparsers.loadPropertyFile('mail')
		message = mail.EmailMessage()
		message.sender = mailProps.get('NewSurvey', 'sender')
		message.to = mailProps.get('NewSurvey', 'to')
		message.subject = mailProps.get('NewSurvey', 'subject_datastore_failure')
		message.body="""A Survey failed to save: \n\nSurvey Created: """+str(today)+"""\n\nSurvey answers: """+str(arguments)+"""\n\nend message"""
		message.send()
	except (Exception, apiproxy_errors.ApplicationError, apiproxy_errors.OverQuotaError), e:
		logging.error(e)
		logging.error('announce_new_survey() : mail could not be sent as the 24-hour quota has been exceeded')
Example #18
0
	def get(self):
		# If no brand has been selected, default to Razorfish
		#self.redirect('/razorfish')
		analyticsProps = configparsers.loadPropertyFile('analytics')
		gaAccount = analyticsProps.get('GA', 'ga_account')
		viewState = 'error-404'
		self.context['viewState']=viewState
		self.context['brand']='razorfish'
		self.context['brandDisplayName']='Razorfish'
		self.context['ga_account']=gaAccount
		template = 'error'
		self.response.set_status(404)
		self.render(template)
Example #19
0
	def get(self):
		try:
			brandProps = configparsers.loadPropertyFile('brand')
			brand = 'razorfish'
			brandDisplayName = brandProps.get(brand, 'display_name')
			adminUser = users.get_current_user()
			brands = utils.get_brands()
			userData = dict()
			userData['email'] = adminUser.email()
			userData['nickname'] = adminUser.nickname()
			userData['id'] = adminUser.user_id()
			sections = None
			since = None
			sectionContent = utils.create_section_content(brand)
			date = None
			section = self.request.get("section")
			since = self.request.get("since")
			requestedBrand = self.request.get('brand')
			dateQuery = None
			today = datetime.date.today()

			
			
			if since is not "":
				if since == "yesterday":
					delta = datetime.timedelta(days=-1)
					dateQuery = today + delta
				elif since == "lastweek":
					delta = datetime.timedelta(days=-7)
					dateQuery = today + delta
				elif since == "lastmonth":
					delta = datetime.timedelta(days=-30)
					dateQuery = today + delta	
				#sections = actions.action_get_survey_by_date_and_brand(dateQuery, requestedBrand)
				sections = actions.action_get_survey_sections_by_brand(requestedBrand)
				
			self.context['adminUser']=userData
			self.context['brandDisplayName']=brandDisplayName
			self.context['brand']=brand
			self.context['brands']=brands
			self.context['requestedBrand']=requestedBrand
			self.context['requestedSection']=section
			self.context['sectionContent']=sectionContent
			self.context['chartApiSubDomainList']=chartApiSubDomainList
			self.context['sections']=sections
			self.context['since']=since
			template = 'admin'
			self.render(template)

		except Exception, e:
			logging.exception(e)
Example #20
0
def action_get_results_by_section(requestedSection, date, requestedBrand):
	try:
		brand = requestedBrand
		surveyProps = configparsers.loadPropertyFile('survey_'+brand)
		sectionContent = None
		dateFormat = None
		if date is not None and date is not "":
			dateFormat = date.isoformat()
		
		sections = memcache.get(key='admin:'+requestedBrand+':'+requestedSection+':count', namespace='razorfish')
		if sections is not None:
			logging.debug("Returning admin:"+requestedBrand+':'+requestedSection+":count from MEMCACHE")
		else:
			survey_class = utils.determine_survey(brand)
			section_module = utils.determine_sections(brand)
			surveys = list()
			if hasattr(section_module, requestedSection) and inspect.isclass(getattr(section_module, requestedSection)):
				
				sectionClass = getattr(section_module, requestedSection)	
				section = dict()
				section['id'] = sectionClass.kind().lower()
				section['name'] = sectionClass.kind()
				section['index'] = surveyProps.get(sectionClass.kind(), 'index')
				section['questions'] = list()
				
				for key, value in sectionClass.properties().items():
					question = dict()
					question['id'] = key
					question['name'] = value.verbose_name
					logging.debug('getting results for '+str(key))
					results = datastore.get_results_by_count_section_question(sectionClass, key, dateFormat, requestedBrand)
					logging.debug(results)
					question['results'] = results[0]
					question['percentages'] = results[1]
					question['options_length'] = len(value.choices)
					section['questions'].append(question)
				
				surveys.append(section)

				sections = sorted(surveys, key=itemgetter('index'), reverse=False)

				# Save to memcache
				#memcache.set(key='admin:'+requestedBrand+':'+requestedSection+':count', value=sections, time=86400, namespace='razorfish')
				
		return sections
	except (BadValueError,AttributeError,TypeError,ValueError,CapabilityDisabledError), e:
		logging.error('action_get_results_by_section()')
		logging.error(e)
		raise e	
Example #21
0
	def get(self):
		try:

			brandProps = configparsers.loadPropertyFile('brand')
			brand = 'razorfish'
			brandDisplayName = brandProps.get(brand, 'display_name')
			adminUser = users.get_current_user()
			userData = dict()
			userData['email'] = adminUser.email()
			requestedBrand = str(self.request.get('brand'))
			mailservice.report_all_surveys(requestedBrand, userData['email'])
			
			self.redirect('/admin')
		except (Exception), e:
			self.error(500)
Example #22
0
def get_countries_by_region(region):
    if region is not None:
        region_properties = configparsers.loadPropertyFile("countries-by-region")
        countries = list()
        # If we have a region Section in the config file...
        if region_properties.has_section(region):
            # And that region Section has items...
            items = region_properties.items(region)
            if len(items) > 0:
                for item in items:
                    # Add the Country Code to the countries list
                    countries.append(item[0].upper())
        return countries
    else:
        return None
Example #23
0
def announce_new_survey(surveyKey):
	try:
		mailProps = configparsers.loadPropertyFile('mail')
		survey = survey_model.Survey.get(surveyKey)
		message = mail.EmailMessage()
		message.sender = mailProps.get('NewSurvey', 'sender')
		message.to = mailProps.get('NewSurvey', 'to')
		message.subject = mailProps.get('NewSurvey', 'subject_success')
		message.body="""
		A new Survey has been submitted:

		Survey Created: """+str(survey.created.isoformat())+"""
		Survey ID: """+str(survey.key())
		message.send()
	except (Exception, apiproxy_errors.ApplicationError, apiproxy_errors.OverQuotaError), e:
		logging.error(e)
		logging.error('announce_new_survey() : mail could not be sent as the 24-hour quota has been exceeded')
Example #24
0
def get_hotels_request_url(destination, startDate, endDate):
	"""
	Get the config properties
	"""
	config_properties = configparsers.loadPropertyFile('config')
	
	hotelsURL = config_properties.get('Hotels', 'hotels_service_v4_url')
	hotelsArgs = dict()
	hotelsArgs['r.object'] = config_properties.get('Hotels', 'hotels_service_v4_r_object')
	hotelsArgs[config_properties.get('Hotels', 'hotels_service_v4_data')] = destination
	hotelsArgs['r.url'] = config_properties.get('Kapow', 'kapow_r_url')
	hotelsArgs['r.username'] = config_properties.get('Kapow', 'kapow_r_username')
	hotelsArgs['r.password'] = config_properties.get('Kapow', 'kapow_r_password')
	if startDate is not None:
		hotelsArgs[config_properties.get('Hotels', 'hotels_service_v4_startDate')] = startDate.date().isoformat()
		hotelsArgs[config_properties.get('Hotels', 'hotels_service_v4_endDate')] = endDate.date().isoformat()
	hotelsArgsEncoded = urllib.urlencode(hotelsArgs)
	hotelsURL += hotelsArgsEncoded
	return hotelsURL
Example #25
0
def geonames_find_nearby_wikipedia(lang, lat, lng, radius, maxRows, country):
	"""
		@params:
			lang 	: 2 character shortcode for language, e.g. 'en' [String]
			lat  	: Latitude [Float]
			lng		: Longitude [Float]
			radius	: Radius in Kilometres (max = 20 Km) [Long]
			maxRows	: Maximum number of entries to return (default = 5) [Long]
			country	: Country Code to search within (default = all countries), e.g. "GB" = United Kingdom(?), "CH" = Switzerland, "DE" = Germany [String]
			
		@description:
			Use the Geonames webservice to find Wikipedia entries by LatLng and Language.
			Don't forget to url encode string parameters containing special characters or spaces.
		
		@example:
			Webservice URI: http://api.geonames.org/findNearbyWikipedia?lang=en&&radius=10&lat=47&lng=9&country=England&username=USERNAME
	"""
	try:
		config_properties = configparsers.loadPropertyFile('geonames')
		
		urlArgs = dict()
		urlArgs['lang'] = str(lang)
		urlArgs['lat'] = float(str(lat))
		urlArgs['lng'] = float(str(lng))
		urlArgs['radius'] = long(str(radius))
		urlArgs['maxRows'] = long(str(maxRows))
		urlArgs['country'] = str(country).upper() # Force uppercase for the Country Code
		urlArgs['username'] = geonames_credentials()
		
		# URL Encode the request arguments
		urlAgrsEncoded = urllib.urlencode(urlArgs)
	
		# Check we have the webservice endpoint available
		if config_properties.has_option('Geonames', 'webservice_url'):
			geonamesServiceURL = config_properties.get('Geonames', 'webservice_url')
			response = urllib.urlopen(""+geonamesServiceURL+'findNearbyWikipediaJSON?'+"%s" % urlAgrsEncoded)
			geonamesJSON = response.read()
			logging.debug(geonamesJSON)
			return geonamesJSON
		else:
			raise e
	except (Exception), e:
		raise e
Example #26
0
	def post(self):
		"""
		Get the config properties
		"""
		types = self.request.get('types')
		radius = self.request.get('radius')
		locationid = self.request.get('locationid')
                                     
		memcacheKey = str(locationid)+":"+str(types)+":"+str(radius)
		memcachePlaces = memcache.get(key=memcacheKey)
		if memcachePlaces is not None:
			logging.info("Retrieving PLACES from MEMCACHE")
			self.response.out.write(memcachePlaces)
		else:
			placesData = datastore.get_places_by_hotellocationid_types_radius(locationid, types, radius).get()
			if placesData is not None:
				logging.info("Retrieving PLACES from DATASTORE")
				self.response.out.write(placesData)
			else:
				logging.info("Requesting PLACES from GOOGLE")   	
				try:
					config_properties = configparsers.loadPropertyFile('config')
					placesURL = "https://maps.googleapis.com/maps/api/place/search/json?%s"
					urlArgs = dict()
					urlArgs['location'] = self.request.get('location')
					urlArgs['radius'] = radius
					urlArgs['types'] = types
					urlArgs['name'] = self.request.get('name')
					urlArgs['key'] = config_properties.get('Google', 'places_api_key')
					urlArgs['sensor'] = config_properties.get('Google', 'places_sensor')

					urlAgrsEncoded = urllib.urlencode(urlArgs)
					result = urllib.urlopen(placesURL % urlAgrsEncoded)
					jsonResponse = result.read()
					self.response.out.write(jsonResponse)
					memcache.set(key=memcacheKey, value=jsonResponse, time=6000)
					datastore.put_places_by_hotellocationid_and_types(locationid, types, jsonResponse, radius)
				except urllib2.URLError, e:
					logging.exception("GooglePlacesHandler : urllib2 error") 
					logging.exception(e)   
Example #27
0
def create_section_content(brand):
	try:
		surveyProps = configparsers.loadPropertyFile('survey_'+brand)  
		sections_module = determine_sections(brand)
		memcache_key = brand+'section:content'
		memcachedSectionContent = memcache.get(key=memcache_key, namespace='razorfish')
		
		if memcachedSectionContent is not None:
			logging.debug("Returning section:content from MEMCACHE")
			return memcachedSectionContent
		else:
			sections = list()
			for name, obj in inspect.getmembers(sections_module):
				if inspect.isclass(obj) and surveyProps.has_section(name):
					section = dict()
					section['name'] = surveyProps.get(name, 'name')
					section['instructions'] = surveyProps.get(name, 'instructions')
					section['id'] = name
					section['questions'] = obj.properties()
					section['index'] = surveyProps.get(name, 'index')
					section['error_message'] = surveyProps.get(name, 'error_message')
					sections.append(section)

			# Sort the Sections
			sections = sorted(sections, key=itemgetter('index'), reverse=False)
			
			# Save to memcache
			memcache.set(key=memcache_key, value=sections, namespace='razorfish')
			
			# return the content
			logging.debug("Returning section:content from method")

			return sections
		
	except ValueError, e:
		logging.error('create_section_content()')
		logging.error(e)
Example #28
0
	def post(self):
		try:
			result = None               
			logging.info("EANHotelRequest")
			config_properties = configparsers.loadPropertyFile('config')
			
			memcacheExpires = config_properties.getfloat('Env', 'memcache_expires')
			
			arrivalDateRaw = self.request.get('arrivalDate')
			numberOfNights = self.request.get('nights')
			priceSort = self.request.get("priceSort")
			ratingSort = self.request.get("ratingSort")
			city = self.request.get('city')
			hotelBrand = self.request.get('brand')
			
			arrivalDateList = arrivalDateRaw.split('-')
			arrivalDate = None
			departureDate = None
			price = None
			priceRaw = self.request.get("priceMax")
			if priceRaw is not None and priceRaw != '':
				price = float(priceRaw)
				
			global_mashup = {}
			
			destination = re.sub(r'(<|>|\s)', '', city)
			destination = destination.lower()
			global_mashup['destination'] = destination
			
			logging.debug(self.request.arguments())
			
			try:
				arrivalDate = datetime.datetime(int(arrivalDateList[2]), int(arrivalDateList[1]), int(arrivalDateList[0]))
				departureDateTimeDelta = datetime.timedelta(days=int(numberOfNights))
				departureDate = arrivalDate + departureDateTimeDelta
			except ValueError, e:
				logging.exception(e)
				logging.exception("EANHotelRequest : Invalid date values or date format")
			
			if arrivalDate is not None and departureDate is not None:
				# Memcache Key convention:
				# CITY:MAX_PRICE:ARRIVAL_DATE:DEPARTURE_DATE:BRAND
				#memcacheKey = str(city)+":"+str(price)+":"+str(arrivalDate.date().isoformat())+":"+str(departureDate.date().isoformat())+":"+str(priceSort)+":"+str(ratingSort)+":"+str(hotelBrand)
				memcacheKey = str(city)+":"+str(arrivalDate.date().isoformat())+":"+str(departureDate.date().isoformat())+":"+str(hotelBrand)
				logging.debug(memcacheKey)
				memcachedHotels = memcache.get(key=memcacheKey, namespace='ean')
				logging.info("Looking up MEMCACHE for : "+memcacheKey)
				
				if memcachedHotels is not None:
					result = memcachedHotels
					logging.debug("Got hotels from memcache")
					
				else:	
					logging.debug("Memcache empty, requesting hotels")
					destination = city.lower()
					if destination == 'europe':
						hotelsData = datastore.get_hotels_by_region(destination, None)
						logging.info(hotelsData)
					else:

						requestArgs = utils.ean_get_hotel_list_url(arrivalDate.date().isoformat(), departureDate.date().isoformat(), city, hotelBrand, self.request)
						logging.info('EAN Hotel request args')
						logging.info(requestArgs)

						try: 
							requestServiceURL = config_properties.get('EAN', 'xml_url_hotellist')
							f = urllib.urlopen(""+requestServiceURL+"%s" % requestArgs)
							response = f.read()
							logging.info('EAN service reponse body')
							logging.info(response)
							response = response.replace('&gt;','>')
							response = response.replace('&lt;','<')
							
						except (apiproxy_errors.ApplicationError, DeadlineExceededError), e:
							logging.exception("EANHotelRequest : DeadlineExceededError error")
							logging.exception(e) 
							global_mashup['name'] = city
							path = os.path.join(os.path.dirname(__file__),'templates/version3/includes/service-error.html')
							self.response.out.write(template.render(path, global_mashup))
							return
				
						try:
							jsonLoadResponse = json.loads(response)	
						except Exception, e:
							logging.exception(e)
							raise e
						
						logging.info('jsonLoadResponse')
						logging.info(jsonLoadResponse)
						
						global_mashup['name'] = city
						if utils.destination_display_names.has_key(city):
							global_mashup['name'] = utils.destination_display_names[city]

						if jsonLoadResponse['HotelListResponse'] is not None:
							if jsonLoadResponse['HotelListResponse'].has_key('HotelList'):
								if jsonLoadResponse['HotelListResponse']['HotelList']['HotelSummary'] is not None:
									result = jsonLoadResponse['HotelListResponse']['HotelList']['HotelSummary']
									if isinstance(result, list):
										for hotel in result:
											if hotel.has_key('thumbNailUrl'):
												hotel['mainImageUrl'] = hotel['thumbNailUrl'].replace('_t', '_b')
									elif isinstance(result, dict):
										tempResult = list()
										tempResult.append(result)
										result = tempResult
										for hotel in result:
											if hotel.has_key('thumbNailUrl'):
												hotel['mainImageUrl'] = hotel['thumbNailUrl'].replace('_t', '_b')
					
						if result is not None:
							# Add the datastore write to the taskqueue
							for hotel in result:
								existingHotel = datamodel.EANHotel.get_by_key_name(str(hotel['hotelId']))
								if existingHotel is None:
									logging.info("EANHotelRequest() : Hotel with hotelid "+str(hotel['hotelId'])+" DOES NOT exist. Assigning task to queue")
									taskqueue.add(queue_name='eanhotelsqueue', url='/eanhotelsworker', params={'hotel':json.dumps(hotel)})
								else: 
									logging.info("EANHotelRequest() : Hotel with location id "+str(hotel['hotelId'])+" DOES exist. No task queue necessary")
								# Add the new price data for this hotel
								taskqueue.add(queue_name='eanhotelspricequeue', url='/eanhotelspriceworker', params={'hotel':json.dumps(hotel), 'arrivalDate':str(arrivalDate.date().isoformat()), 'departureDate':str(departureDate.date().isoformat())})
			
								# Add Hotel details
								#taskqueue.add(queue_name='eanhoteldetailsqueue', url='/eanhoteldetailsworker', params={'hotelid':str(hotel['hotelId'])})
							
								# Get Geonames Wikipedia Data
								
								#wikipediaData = geonames.geonames_find_nearby_wikipedia('en', hotel['latitude'], hotel['longitude'], 10, 5, 'us')
								#hotel['geonames'] = wikipediaData
							# Add the hotel results to Memcache
							memcache.set(key=memcacheKey, value=result, time=memcacheExpires, namespace='ean')
Example #29
0
def action_get_survey_by_date_and_brand(date, brand):
	
	memcacheKey = 'survey:results:'+str(date.isoformat())	
	memcacheResult = memcache.get(memcacheKey, namespace=brand)
	if memcacheResult is not None:
		logging.debug('Returning processed Survey results from MEMCACHE')
		return memcacheResult
	else:
		logging.debug('Returning processed Survey results from DATASTORE')
		surveys = datastore.get_surveys_by_date_and_brand(date, brand)	
		
		surveyProps = configparsers.loadPropertyFile('survey_'+brand)
	
		sections = dict()
		surveyResults = list()
		# Prepare an object with the Datastore data
		# for each survey

		"""
			[ST]TODO:
		 		Refactor all of this to use the db.query_descendants() method against the Survey model instance received
		 		This will catch pre-PolyModel Survey instances in the Datastore, and saves iterating over all Survey model instance
		 		properties looking for Survey Sections that are classes, and then getting their properties, etc, etc.

		 		Otherwise we end up collecting Survey model instances that have no connected Section model instances :(
		"""
		for survey in surveys:
			# for each section within the survey
			for surveyAttr in survey.properties():
				#logging.debug('actions.py : action_get_survey_by_date_and_brand() : surveyAttr')
				#logging.debug(surveyAttr)
				section = getattr(survey, surveyAttr)

				#logging.debug('actions.py : action_get_survey_by_date_and_brand() : section')
				#logging.debug(type(section))

				# [ST]TODO: Ugly patch for pre-PolyModel Surveys
				if type(section) is list and surveyAttr == '_class' and len(section) <= 1:
					logging.debug('We have a broken Razorfish Survey connection')
					child_sections = db.query_descendants(survey)
					for child in child_sections:
						logging.debug(child)
				else:
					logging.debug('We have found a Razorfish Survey')

				# If the Section Model has properties, and it exists in the Survey Properties ConfigParser file (e.g. is not the User model)...
				if section is not None and hasattr(section, 'properties') and surveyProps.has_section(section.kind()):
					# If we have not yet created a dict entry for this section, then do so
					
					if surveyAttr not in sections:
						sections[surveyAttr] = dict()
						sections[surveyAttr]['id'] = surveyAttr
						sections[surveyAttr]['created'] = survey.created
						sections[surveyAttr]['index'] = surveyProps.get(section.kind(), 'index')
						sections[surveyAttr]['name'] = surveyProps.get(section.kind(), 'name')
						sections[surveyAttr]['results'] = dict()
						surveyResults.append(sections[surveyAttr])

					# for question name and value in the section
					for question, prop in section.properties().items():
						if type(prop) == db.IntegerProperty:
							answer = getattr(section, question)
							# If we have not yet created a dict entry for this question in this section, then do so
							if question not in sections[surveyAttr]['results']:
								sections[surveyAttr]['results'][question] = dict()
								sections[surveyAttr]['results'][question]['id'] = question
								sections[surveyAttr]['results'][question]['name'] = prop.verbose_name
								sections[surveyAttr]['results'][question]['raw'] = list()
								sections[surveyAttr]['results'][question]['count'] = dict()
								sections[surveyAttr]['results'][question]['percentage'] = dict()
								sections[surveyAttr]['results'][question]['options_length'] = len(prop.choices)
								sections[surveyAttr]['results'][question]['type'] = 'multi'
							# Add the answer to the question list
							sections[surveyAttr]['results'][question]['raw'].append(answer)
						if type(prop) == db.TextProperty:
							answer = getattr(section, question)
							
							# If we have not yet created a dict entry for this question in this section, then do so
							if question not in sections[surveyAttr]['results']:
								sections[surveyAttr]['results'][question] = dict()
								sections[surveyAttr]['results'][question]['id'] = question
								sections[surveyAttr]['results'][question]['name'] = prop.verbose_name
								sections[surveyAttr]['results'][question]['answers'] = list()
								sections[surveyAttr]['results'][question]['type'] = 'single'
							
							# IF an answer exists, include this as a tuple with the User model object as the first argument and the answer Text value as the second argument
							if answer != '':	
								sections[surveyAttr]['results'][question]['answers'].append({'user':getattr(survey, 'user_id'), 'answer':answer, 'date':survey.created})
						
		for section, questionSet in sections.items():
			for question, data in questionSet['results'].items():
				# If we have a multiple choice question we need to process the results
				
				if data.has_key('raw') and type(data['raw']) is list:
					base = {}
					base[1]=0
					base[2]=0
					base[3]=0
					base[4]=0
					base[5]=0
					counted = utils.count_unsorted_list_items(data['raw'])				
					data['count'] = dict(base.items() + counted.items())
					sorted(data['count'], reverse=False)
					data['percentage'] = utils.get_percentage(data['count'])
		
		finalResult = sorted(surveyResults, key=itemgetter('index'), reverse=False)
		if len(finalResult) > 0:
			memcache.set(key=memcacheKey, value=finalResult, time=300, namespace=brand)
		return finalResult
Example #30
0
def action_get_survey_sections_by_brand(brand):
	try:
		memcache_key = 'survey:sections:'+str(brand)	
		memcache_result = memcache.get(memcache_key, namespace=brand)
		if memcache_result is not None:
			logging.debug('action_get_survey_sections_by_brand() : Returning processed Survey Section results from MEMCACHE')
			return memcache_result
		else:
			logging.debug('action_get_survey_sections_by_brand() : Returning processed Survey Section results from DATASTORE')
			# Get the Surveys from the Datastore by Brand
			surveys = datastore.get_surveys_by_brand(brand)	
			# Load the Survey Properties, for Content iteration in the HTML Template
			surveyProps = configparsers.loadPropertyFile('survey_'+brand)

			# Create an empty dict for Survey Sections
			sections = dict()
			# Create an empty list for all section results
			section_results = list()
			# For each Survey in the Datastore Query result
			for survey in surveys:
				# Get the Survey descendats
				survey_descendants = db.query_descendants(survey)
				
				for descendant in survey_descendants:
					# Set the Sections dict key to be the Kind (lowercase)
					_id = descendant.kind().lower()
					# Create a Section dict
					if _id not in sections:
						sections[_id] = dict(
							id=_id,
							created=survey.created,
							index=surveyProps.get(descendant.kind(), 'index'),
							name=surveyProps.get(descendant.kind(), 'name'),
							results = dict()
						)
						section_results.append(sections[_id])

					# for question name and value in the descendant section
					for question, prop in descendant.properties().items():
						# If the Property is of type Integer (Long returned from Datastore, actually)
						if type(prop) == db.IntegerProperty:
							answer = getattr(descendant, question)
							
							# If we have not yet created a dict entry for this question in this section, then do so
							if question not in sections[_id]['results']:
								sections[_id]['results'][question] = dict(
									id=question,
									name=prop.verbose_name,
									raw=list(),
									count=dict(),
									percentage=dict(),
									options_length=len(prop.choices),
									type='multi'
								)

							# Add the answer to the question list
							sections[_id]['results'][question]['raw'].append(answer)
							
						# If the Property is of type Text:
						if type(prop) == db.TextProperty:
							answer = getattr(descendant, question)
							
							# If we have not yet created a dict entry for this question in this section, then do so
							if question not in sections[_id]['results']:
								sections[_id]['results'][question] = dict(
									id=question,
									name=prop.verbose_name,
									answers=list(),
									type='single'
								)
							
							# If an answer exists, include this as a tuple with the User model object as the first argument and the answer Text value as the second argument
							if answer != '':	
								sections[_id]['results'][question]['answers'].append({
								'user':getattr(survey, 'user_id'),
								'answer':answer,
								'date':survey.created
							})

				
			# Once we have iterated through all resuts, we need further processing.
			# This calculates the percentages of answers for multiple choice questions
			for section, questionSet in sections.items():
				for question, data in questionSet['results'].items():
					# If we have a multiple choice question we need to process the results
					#logging.debug(question)
					#logging.debug(data)
					if data.has_key('raw') and type(data['raw']) is list:
						base = {}
						if brand == 'razorfish':
							base[0]=0
							base[1]=0
							base[2]=0
							base[3]=0
							base[4]=0
							base[5]=0
						elif brand == 'mcdonalds':
							base[0]=0
							base[1]=0
							base[2]=0
							base[3]=0
							base[4]=0
							
						counted = utils.count_unsorted_list_items(data['raw'])
						#logging.debug('counted')
						#logging.debug(counted)
						data['count'] = dict(base.items() + counted.items())
						sorted(data['count'], reverse=False)
						data['percentage'] = utils.get_percentage(data['count'])
			
			# Finally, we sort our Section Results list, using the index of the section as the sort key
			section_results = sorted(section_results, key=itemgetter('index'), reverse=False)
			
			# Add the result to Memcache, to expire in 5 minutes
			if len(section_results) > 0:
				memcache_stored = memcache.set(memcache_key, value=section_results, time=360, namespace=brand)
			return section_results
	except Exception, e:
		logging.error(type(e))
		logging.error(e)
		raise e