Package src :: Package gui :: Module gui
[hide private]
[frames] | no frames]

Source Code for Module src.gui.gui

  1  #!/usr/bin/python 
  2  # -*- coding: UTF-8 -*- 
  3  # 
  4  #The MIT License 
  5  # 
  6  #Copyright (c) 2011 
  7  # 
  8  #Permission is hereby granted, free of charge, to any person obtaining a copy 
  9  #of this software and associated documentation files (the "Software"), to deal 
 10  #in the Software without restriction, including without limitation the rights 
 11  #to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
 12  #copies of the Software, and to permit persons to whom the Software is 
 13  #furnished to do so, subject to the following conditions: 
 14  # 
 15  #The above copyright notice and this permission notice shall be included in 
 16  #all copies or substantial portions of the Software. 
 17  # 
 18  #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 19  #IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 20  #FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 21  #AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 22  #LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 23  #OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
 24  #THE SOFTWARE. 
 25  # 
 26  #Authors: 
 27  #   Vili Auvinen (vili.k.auvinen@jyu.fi) 
 28  #   Olli Kauppinen (olli.kauppinen@jyu.fi) 
 29  #   Juho Tammela (juho.i.tammela@jyu.fi) 
 30   
 31  '''The module provides the Mod_Python graphical user interface for the software. 
 32   
 33  @author: Juho Tammela 
 34  ''' 
 35  import controller 
 36  from cgi import escape 
 37  from urllib import urlopen 
 38  from cStringIO import StringIO 
 39   
40 -def firstPage(name, nameError, email, emailError, fileDiskError, fileUrl, fileUrlError):
41 '''Gets the form page of the interface as an HTML string.''' 42 43 return """<?xml version="1.0" encoding="UTF-8"?> 44 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 45 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 46 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fi" > 47 <head> 48 <meta http-equiv="content-type" content="text/html; charset=UTF-8"> 49 <link href="style.css" rel="StyleSheet" type="text/css" /> 50 <script src="jquery-1.5.2.min.js" type="text/javascript"></script> 51 <script src="file_input_changer.js" type="text/javascript"></script> 52 <title>Harkkatarkastin</title> 53 </head> 54 <body id="main"> 55 <h1>Tietokone ja tietoverkot työvälineenä - Harkkapoliisi</h1> 56 <p>Päivitetty 19.5.2011 klo 17:57</p> 57 <form action="" method="post" enctype="multipart/form-data"> 58 <fieldset> 59 <legend>Harjoitustyön tiedot</legend> 60 <p class="evenTextBox"> 61 <label for="name">Tekijän nimi:</label> 62 <input type="text" id="name" name="name" size="50" value="%s"/> 63 <span class="error">%s</span> 64 </p> 65 <p class="evenTextBox"> 66 <label for="email">Sähköposti:</label> 67 <input type="text" id="email" name="email" size="50" value ="%s"/> 68 <span class="error">%s</span> 69 </p> 70 <p>Tiedoston lataus:</p> 71 <p class="evenFileInput"> 72 <input id="radioDisk" type="radio" value="file" name="fileInputSelection" checked="checked"/> 73 <label for="radioDisk">Levyltä:</label> 74 <input class="" type="file" id="filerequest" name="filerequest" /> 75 <span class="error">%s</span> 76 </p> 77 <p class="evenFileInput" > 78 <input id="radioUrl" type="radio" value="url" name="fileInputSelection"/> 79 <label for="radioUrl">URL:</label> 80 <input class="hidden" type="text" id="fileUrl" name="fileUrl" size="50" value="%s" /> 81 <span class="error">%s</span> 82 </p> 83 <p class="evenInput"> 84 <input type="submit" id="submitButton" name="submitButton" value="Tarkasta!" /> 85 </p> 86 <p id="loading" class="hidden">Tarkastetaan... 87 <img src="http://sovellusprojektit.it.jyu.fi/parsi/sovellus/img/loading.gif" alt="Tarkastetaan dokumentteja." /> 88 </p> 89 </fieldset> 90 </form> 91 </body> 92 </html> 93 """ % (name, nameError, email, emailError, fileDiskError, fileUrl, fileUrlError)
94
95 -def feedBackPage(feedback, name, email):
96 '''Gets the feedback page of the interface.''' 97 98 page = """<?xml version="1.0" encoding="UTF-8"?> 99 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 100 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 101 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fi"> 102 <head> 103 <meta http-equiv="content-type" content="text/html; charset=UTF-8"> 104 <link href="style.css" rel="StyleSheet" type="text/css" /> 105 <script src="jquery-1.5.2.min.js" type="text/javascript"></script> 106 <script src="feedbackpage.js" type="text/javascript"></script> 107 <title>Harkkatarkastin</title> 108 </head> 109 <body> 110 <h1>Tietokone ja tietoverkot työvälineenä - Harkkapoliisi</h1> 111 <p> 112 Nimi: %s <input type="hidden" id="name" value="%s" /> 113 </p> 114 <p> 115 Sähköposti: %s <input type="button" id="sendToEmailButton" name="" value="Lähetä palaute myös sähköpostiin" /> <input type="hidden" id="emailAddress" value="%s" /> 116 </p> 117 <p>Tarkastettu: <span id="date"></span></p> 118 """ % (name, name, email, email) 119 120 for msg in feedback: 121 page += msg 122 123 page += """ 124 </body> 125 </html> 126 """ 127 return page
128
129 -def index(req):
130 req.content_type = "text/html" 131 form = req.form 132 feedback = [] 133 name = form.getfirst("name") 134 email = form.getfirst("email") 135 sent = form.getfirst("submitButton") 136 fileUrl = form.getfirst("fileUrl") 137 radioValue = form.getfirst("fileInputSelection") 138 #feedback.append(name) 139 140 if sent: 141 #feedback.append("Nimi: " + name) 142 #feedback.append("Sähköpostiosoite: " + email) 143 nameError = "" 144 emailError = "" 145 goForward = True 146 147 if name == "" or name == None: 148 nameError = "* Pakollinen kenttä!" 149 goForward = False 150 if email == "" or email == None: 151 emailError = "* Pakollinen kenttä!" 152 goForward = False 153 if goForward: 154 tmpfile = None 155 if radioValue.value == "file": 156 tmpfile = req.form['filerequest'] 157 158 if tmpfile.filename == "": 159 inputError = "* Tiedostoa ei löytynyt!" 160 return firstPage(escape(name), nameError, escape(email), emailError, inputError, escape(fileUrl), "") 161 controller.beginInspection(tmpfile.file, tmpfile.filename, feedback, email) 162 163 elif radioValue.value == "url": 164 try: 165 tmpfile = urlopen(fileUrl) 166 controller.beginInspection(StringIO(tmpfile.read()), fileUrl, feedback, email) 167 except IOError: 168 inputError = "* Url-osoite oli väärin!" 169 return firstPage(escape(name), nameError, escape(email), emailError, "", escape(fileUrl), inputError) 170 #else: then what? 171 172 return feedBackPage(feedback, escape(name), escape(email)) 173 174 else: 175 sent = None 176 return firstPage(escape(name), nameError, escape(email), emailError, "", escape(fileUrl), "") 177 return firstPage("", "", "", "", "", "http://", "")
178