Skip to content

Adding 404 Easter Egg to your web app

darkryder edited this page Dec 8, 2014 · 3 revisions

###It's simply put, an idea, meant to add a quirky twist to your app, while ensuring that people are aware of what might be lost.

404 Easter Egg is a simple API of the lost-n-found that your web-app can plug into to render 404 Pages with the recently lost items reported on lost-n-found. This document will help you through the configurations you need to make, in order to implement the 404 Easter Egg.


Steps

  1. Login to the lost-n-found portal of your organisation and register your website there, adding the necessary details.
  2. A unique Site-ID and token will be generated. You could refresh the token, in case it gets leaked.
  3. Configure your app to let you handle the 404 Error.
  4. Make a GET or POST request with token and id parameters to the URL http://PortalURL/LnF404/. You could also send a quantity parameter to get the required number of latest lost items. In fact you should send a quantity parameter, otherwise you will end up receiving ~20 objects in the response.
  5. You could also simply issue a GET request to the URL http://PortalURL/LnF404/site_id/token/(optional number of items)
  6. Your web app will receive a JSON response
  7. Render your custom 404 Error page using this JSON data
  8. Add your site to the list at the end :)

NOTE: Add the correct IP, as it will be authenticated with the originating address from the request. If you are behind an NAT, give its global IP.

JSON response

The JSON response will have the following fields:

  1. success -> either a 'true' / 'false'. 'false' is sent if the portal fails to authenticate your token and site_id.
  2. quantity -> the number of lost items sent in the response.
  3. 0-indexed integers corresponding to lost items. such as 3
  4. These items will have further attributes:
  • item-name
  • location
  • info
  • email
  • image
  • image_url

Sample response

"{
  u'0':  {
     u'info':  u'Some info',
     u'location':  u'Hostel',
     u'item-name':  u'Laptop',
     u'email':  u'a@b.com',
     u'image': true,
     u'image_url': 'some_url_to_image',
  },
   u'success':  u'true',
   u'quantity':  1
}"

Configuring popular frameworks

Django

First of all, add these to your app's settings.py

LnF404_SiteID = 1
LnF404_token = ""
LnF404_url   = "http://127.0.0.1:8000"

Now tell your app to use a custom 404 handler. For this, add handler404 in the urls.py pointed to by the URLS_CONF in your settings file

handler404 = 'myapp.views.handle404_LnF'

Now add this handler in myapp/views.py


import json
from urllib import urlencode
from urllib2 import urlopen

from myrootapp import settings

def handle404_LnF(request):
	
	class Item(object):
		def __init__(self, name, location, info, email):
			self.name		= name
			self.location	= location
			self.info		= info
			self.email		= email

	url 	= settings.LnF404_url
	site_id	= settings.LnF404_SiteID
	token 	= settings.LnF404_token
	quantity= 10				# set it according to your wish

	data 	= urlencode({
		'id': site_id, 'token': token, 'quantity': quantity
		})
	request	= urlopen(url, data)
	resp	= json.loads(request.read())

	if resp['success'] == 'true' and resp['quantity'] != 0:
		items = []
		for i in range(int(resp['quantity'])):
			items.append(Item(
			  resp[str(i)]['item-name'],
			  resp[str(i)]['location'],
			  resp[str(i)]['info'],
			  resp[str(i)]['email'],
			))
		return render(request, 'LnF404.html',  
		{'items': items})  # Now show your creativity in LnF404.html

	else:
		raise Exception	#TO-DO

###Webapps which use this.

  1. Community Work Portal - IIITD
  2. [Lost & Found Portal - IIITD] (http://findmystuff.iiitd.edu.in/invalid/url)