-
Notifications
You must be signed in to change notification settings - Fork 3
Adding 404 Easter Egg to your web app
###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.
- Login to the lost-n-found portal of your organisation and register your website there, adding the necessary details.
- A unique Site-ID and token will be generated. You could refresh the token, in case it gets leaked.
- Configure your app to let you handle the 404 Error.
- 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.
- You could also simply issue a GET request to the URL
http://PortalURL/LnF404/site_id/token/(optional number of items)
- Your web app will receive a JSON response
- Render your custom 404 Error page using this JSON data
- 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.
The JSON response will have the following fields:
-
success -> either a 'true' / 'false'. 'false' is sent if the portal fails to authenticate your token and site_id.
-
quantity -> the number of lost items sent in the response.
- 0-indexed integers corresponding to lost items. such as 3
- These items will have further attributes:
-
item-name
-
location
-
info
-
email
-
image
-
image_url
"{
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
}"
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.
- Community Work Portal - IIITD
- [Lost & Found Portal - IIITD] (http://findmystuff.iiitd.edu.in/invalid/url)
404 Easter Egg API opened and maintained by darkryder