User:Eleanorg/1.3/Dissolute Image/Code/verify input

From XPUB & Lens-Based wiki
< User:Eleanorg
Revision as of 18:06, 15 June 2012 by Eleanorg (talk | contribs) (Created page with "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 im...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

untested, 15 june 6pm <source lang="python">

  1. !/usr/bin/python
  2. -*- coding:utf-8 -*-

import cgi, os, subprocess, re import cgitb; cgitb.enable() import pymongo from pymongo import Connection

  1. recieves url from inputForm.html scrapes it to check if pixel has been put there correctly
  2. if yes = updates db entry for that pixel
  3. if no = throws error


  1. -------------- 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!

  • Did you paste it exactly as it appears here?
  • Is the page where you pasted it publicly accessible?
  • """ thankyou = """Thanks, URL submitted.
    Your pixel has been added to the image."""

    1. ------------- get URL from input form -------------------#

    form = cgi.FieldStorage() # Grabs whatever input comes from form

    1. string = form.getvalue("pixel")
    2. 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
    1. for testing:

    string = "x=123;y=345;col=(255,255,255);idNo=ObjectId('4fdb5b75ed9c8d4743000000')" url = "http://ox4.org/~nor/trials/hostedString.html"

    1. ------------- print html header--------------------------#
    1. print "Content-Type: text/html"
    2. print
    3. print htmlHeader
    1. ------------ define splitting function-------------------#
    2. 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

    1. ------------- scrape and check the url -----------------#
    1. if correct string not found, throw error
    2. if found, print thankyou and update db
    1. 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'

    1. 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:

    1. 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

    1. print htmlFooter

    <source>