User:Eleanorg/1.3/Dissolute Image/Code/verify input
untested, 15 june 6pm <source lang="python">
- !/usr/bin/python
- -*- coding:utf-8 -*-
import cgi, os, subprocess, re import cgitb; cgitb.enable() import pymongo from pymongo import Connection
- recieves url from inputForm.html scrapes it to check if pixel has been put there correctly
- if yes = updates db entry for that pixel
- if no = throws error
- -------------- error / thank you messages ----------------#
htmlHeader = """<!DOCTYPE html> <html>
<head> <title>A form talking to a python script</title> <link <link rel="stylesheet" href="../../pixels.css"> </head> <body>
htmlFooter = """
</body>
</html>"""
error = """Sorry, your pixel details couldn't be found at that URL!
"""
thankyou = """Thanks, URL submitted.
Your pixel has been added to the image."""
- ------------- get URL from input form -------------------#
form = cgi.FieldStorage() # Grabs whatever input comes from form
- string = form.getvalue("pixel")
- url = form.getvalue("url", "http://ox4.org/~nor/trials/hostedString.html") # assigns form's input to var 'url'. url on the right is a default value that will be printed for testing if nothing is recieved from the form
- for testing:
string = "x=123;y=345;col=(255,255,255);idNo=ObjectId('4fdb5b75ed9c8d4743000000')" url = "http://ox4.org/~nor/trials/hostedString.html"
- ------------- print html header--------------------------#
- print "Content-Type: text/html"
- print htmlHeader
- ------------ define splitting function-------------------#
- to split user's string into a hash which can be verified against the one in db
def splitString (stg) : pat = re.compile("(\w{1,4}=[^;]+);?") # captures each foo=blah pair in the string, separated by ;s cap = pat.findall(stg) # cap variable contains all four captures - id, x, y, color (if valid) # put captures into a hash for this pixel myhash = {} for s in cap : k,v = s.split('=') # splits each capture on the = into key and value myhash[k] = v # puts new k/v pair into hash with these values # return hash so it can be compared with that in the db try : myhash['x'] and myhash['y'] and myhash['col'] and myhash['idNo'] except: return # missing parameter! return nothing return myhash # returns a hash as above, accessed thus: paramGet(stg)['x'] etc
- ------------- scrape and check the url -----------------#
- if correct string not found, throw error
- if found, print thankyou and update db
- connect to db for verification, and possible updating
connection = Connection() myDB = connection['pixelsDbTest0'] collection = myDB.collection text = urllib2.urlopen(url).read() # reads page at the specified URL if not re.search(pat, text): # if pattern 'pat' (ie unique pixel string) isn't matched within 'text'
- print error # print error message to user
print "sorry, couldnt scrape ur string" else: pixelHash = myDB.collection.find({"id": idNo}) # check db to see if ID exists if pixelHash: # check if attributes from string recieved (xpos, ypos + colour) match attributes for that ID in DB? split = splitString(string) # calls split string function, passing it the string recieved from the input form, and assigns it to var 'split' if split: print "hurrah, string was split into a hash" # now do verification against db: # split['x'] should == pixelHash['x'] etc # split['idNo'] should == pixelHash['id'] << remember you're not allowed 'id' as a hash key in python # if all 4 attributes are the same, print hurrah and update db else print "boo, splitting didnt work" # if yes: # print thank you # trigger drawing script # else print error else:
- print error
print "sorry, no pixel with that id exists in the db" #print thankyou #collection.update( {"string":pat}, {"$set":{"url": url}}) # set pixel with string matching 'pat' to url given by user
- print htmlFooter
<source>