예제 #1
0
파일: gdg-url.py 프로젝트: benjic/gdg-url
	def get(self):
		"""Response for GET requests."""

		# When rendering the template we pass a dictionary of values that the
		# template may use. The templates are rendered in a sandbox so local 
		# scope is not applicable. 
		#
		# In this instance the only variable is a conditional flag whether a
		# short url has been created.
		template_values = {
			'created': False,
			'hostname': os.environ.get('HTTP_HOST'),
			'urls': Url.all().order('-timestamp').fetch(limit=10)

		}

		template = JINJA_ENVIRONMENT.get_template("root.html")
		self.response.write(template.render(template_values))
예제 #2
0
파일: gdg-url.py 프로젝트: benjic/gdg-url
	def post(self):
		"""Response for POST requests."""

		# The ideas behind a good hashing algorithm are complex. For
		# this example we will only truncate the first 10 digits of
		# of the md5 hash. This will of course have collisions but it
		# is a problem for another day.
		m = hashlib.md5()
		m.update(self.request.get('link'))

		# The unique hash is truncated to keep our links small.
		url_hash = m.hexdigest()[0:10] 

		# A Url model is instinated with the supplied post data. Our url 
		# shortner uses a hash as a key to recover created urls. To enforce
		# link vaildation we try to create an entity with the supplied information
		# and anticipate failures.
		try:
			# By instinating an Url entity we call is validate method which ensures
			# the link is good.
			url = Url(key_name=url_hash,
				link=self.request.get('link'))
		except (HTTPError, BadUrlError), e:

			# If the underlying URL functions fail in any way we catch the error and
			# render the template to include the error message.

			template_values = {
				'created': False,
				'error': e,
				'hostname': os.environ.get('HTTP_HOST'),
				'urls': Url.all().order('-timestamp').fetch(limit=10)
			}

			template = JINJA_ENVIRONMENT.get_template("root.html")
			self.response.write(template.render(template_values))
예제 #3
0
파일: gdg-url.py 프로젝트: benjic/gdg-url
			}

			template = JINJA_ENVIRONMENT.get_template("root.html")
			self.response.write(template.render(template_values))
		else:

			# Model.put() commits the model to the datastore.
			url.put()

			# In this instance the only variable is a conditional flag whether a
			# short url has been created.
			template_values = {
				'created': True,
				'hash': url_hash,
				'hostname': os.environ.get('HTTP_HOST'),
				'urls': Url.all().order('-timestamp').fetch(limit=10)
			}

			template = JINJA_ENVIRONMENT.get_template("root.html")
			self.response.write(template.render(template_values))

class ShortUrlPage(webapp2.RequestHandler):
	"""Accepts route requests for anything but root."""

	# Requests to a short url should only accept GET http methods.
	def get(self, hash):
		# The request parses the hash from the path by slicing the string to omit
		# the leading "\". We ask the datastore for any entity that matches the hash.
		url = Url.get_by_key_name(hash)

		# If the hash is valid it will return Url Model object else it will return None