Ejemplo n.º 1
0
	def post_firebase(self, reg_ids):
		def on_reply(response):
			try:
				self.logger.debug("!! FireBase response: " + str(response.body))
				response_obj = json.loads(response.body)
				delete = []
				for index, elem in enumerate(response_obj['results']):
					if elem.get('error') in ['NotRegistered', 'InvalidRegistration']:
						delete.append(reg_ids[index])
				if len(delete) > 0:
					self.logger.info("Deactivating subscriptions: %s", delete)
					Subscription.objects.filter(registration_id__in=delete).update(inactive=True)
			except Exception as e:
				self.logger.error("Unable to parse response" + str(e))
				pass

		headers = {"Content-Type": "application/json", "Authorization": "key=%s" % FIREBASE_API_KEY}
		body = json.dumps({"registration_ids": reg_ids})
		self.logger.debug("!! post_fire_message %s", body)

		# TODO
		# webpush(subscription_info,
		# 		  data,
		# 		  vapid_private_key="Private Key or File Path[1]",
		# 		  vapid_claims={"sub": "mailto:YourEmailAddress"})

		r = HTTPRequest(FIREBASE_URL, method="POST", headers=headers, body=body)
		http_client.fetch(r, callback=on_reply)
Ejemplo n.º 2
0
	def search_giphy(self, message, query, cb):
		self.logger.debug("!! Asking giphy for: %s", query)
		def on_giphy_reply(response):
			try:
				self.logger.debug("!! Got giphy response: " + str(response.body))
				res =  json.loads(response.body)
				giphy = res['data'][0]['images']['downsized_medium']['url']
			except:
				giphy = None
			cb(message, giphy)
		url = GIPHY_URL.format(GIPHY_API_KEY, quote(query, safe=''))
		http_client.fetch(url, callback=on_giphy_reply)
Ejemplo n.º 3
0
		def wrap(self, *args, **kwargs):
			if hasattr(settings, 'RECAPTCHA_PRIVATE_KEY'):
				if not kwargs.get(field):
					raise ValidationError("Captcha is missing")
				try:
					self.logger.debug('Validating captcha...')
					response_object = yield http_client.fetch(HTTPRequest(
						"https://www.google.com/recaptcha/api/siteverify",
						method='POST',
						body=urllib.parse.urlencode({
							'secret': settings.RECAPTCHA_PRIVATE_KEY,
							'response': kwargs[field],
							'remoteip': self.client_ip
						})
					))
					response = json.loads(response_object.body)
					if not response.get('success', False):
						self.logger.debug('Captcha is NOT valid, response: %s', response_object.body)
						raise ValidationError(
							response['error-codes'] if response.get('error-codes', None) else 'This captcha already used')
					self.logger.debug('Captcha is valid, response: %s', response_object.body)
				except Exception as e:
					raise ValidationError('Unable to check captcha because {}'.format(e))
			else:
				self.logger.debug('Skipping captcha validation')
			kwargs.pop(field, None)
			result =  f(self, *args, **kwargs)
			if isinstance(result, GeneratorType):
				result = yield from result
			return result