.
[HamExam.git] / interface.py
blobe5b023f034d8a15ecd3dbdc2b4bfa5482bdb082b
1 #!/usr/bin/python
2 import string,cgi,time,posixpath, urllib, os
3 from os import curdir, sep
4 from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
5 import framework
7 class MyHandler(BaseHTTPRequestHandler):
8 def do_GET(self):
9 if self.path.endswith(".afu"):
10 self.AFU()
11 else:
12 f = self.send_head()
13 if f:
14 self.copyfile(f, self.wfile)
15 f.close()
17 def do_HEAD(self):
18 f = self.send_head()
19 if f:
20 f.close()
22 def send_head(self):
23 path = self.translate_path(self.path)
24 if os.path.isdir(path):
25 self.send_error(403, "Directory listing not supported")
26 return None
27 try:
28 f = open(path, 'rb')
29 except IOError:
30 self.send_error(404, "File not found")
31 return None
32 self.send_response(200)
33 self.send_header("Content-type", self.guess_type(path))
34 self.end_headers()
35 return f
37 def translate_path(self, path):
38 path = posixpath.normpath(urllib.unquote(path))
39 words = string.splitfields(path, '/')
40 words = filter(None, words)
41 path = os.getcwd()
42 for word in words:
43 drive, word = os.path.splitdrive(word)
44 head, word = os.path.split(word)
45 if word in (os.curdir, os.pardir): continue
46 path = os.path.join(path, word)
47 return path
49 def copyfile(self, source, outputfile):
50 BLOCKSIZE = 8192
51 while 1:
52 data = source.read(BLOCKSIZE)
53 if not data: break
54 outputfile.write(data)
56 def guess_type(self, path):
57 base, ext = posixpath.splitext(path)
58 if self.extensions_map.has_key(ext):
59 return self.extensions_map[ext]
60 ext = string.lower(ext)
61 if self.extensions_map.has_key(ext):
62 return self.extensions_map[ext]
63 else:
64 return self.extensions_map['']
66 extensions_map = {
67 '': 'text/plain', # Default, *must* be present
68 '.html': 'text/html',
69 '.htm': 'text/html',
70 '.gif': 'image/gif',
71 '.jpg': 'image/jpeg',
72 '.jpeg': 'image/jpeg',
75 def AFU(self):
76 if self.path.endswith("a.afu") or self.path.endswith("b.afu") or self.path.endswith("c.afu") or self.path.endswith("d.afu"):
77 answer = (self.path.split("/")[-1]).replace(".afu","")
78 if not f.EvalQuestion (answer):
79 self.WrongAnswer()
80 else:
81 self.AskQuestion()
82 elif self.path.endswith("hint.afu"):
83 self.DisplayHint()
84 elif self.path.endswith("menue.afu"):
85 self.DisplayMenu()
86 elif self.path.endswith("askquestion.afu"):
87 self.AskQuestion()
88 elif self.path.endswith("showquestion.afu"):
89 self.AskQuestion(update=False)
90 else:
91 self.StartDisplay()
93 def ShowHead(self):
94 self.wfile.write ("<html><head><base href="+base+"/Questions/>")
95 self.wfile.write ("<link href="+stylefile+" rel=stylesheet type=text/css>")
96 self.wfile.write ("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf8\">")
98 self.wfile.write ("<SCRIPT LANGUAGE=\"JavaScript\">")
99 self.wfile.write ("var inNav = navigator.appVersion.indexOf(\"MSIE\") < 0;")
100 self.wfile.write ("function eval_key (event) {")
101 self.wfile.write ("var key = (inNav==1) ? event.which : event.keyCode;")
102 self.wfile.write ("if (key == 65) { window.location = \""+base+"/a.afu\";")
103 self.wfile.write ("} else if (key == 66) { window.location = \""+base+"/b.afu\";")
104 self.wfile.write ("} else if (key == 67) { window.location = \""+base+"/c.afu\";")
105 self.wfile.write ("} else if (key == 68) { window.location = \""+base+"/d.afu\";")
106 self.wfile.write ("}")
107 self.wfile.write ("}")
108 self.wfile.write ("document.onkeydown = eval_key;")
109 self.wfile.write ("</script>")
111 self.wfile.write ("</head><body>")
113 def DisplayHint(self):
114 self.SendHeader()
115 print "Hint:",f.hint
116 self.wfile.write ("<html><head><meta http-equiv=refresh content=\"0; URL=/"+f.hint+"\"></head></html>")
118 def SendHeader(self):
119 self.send_response(200)
120 self.send_header('Content-type', 'text/html')
121 self.end_headers()
123 def DisplayQuestion(self):
124 self.wfile.write("<div class=id>"+f.id+"</div>")
125 self.wfile.write("<div class=question>")
126 self.wfile.write(f.question.encode("utf8"))
127 self.wfile.write("</div>")
128 self.wfile.write("<div class=statistics>Richtig: "+f.correct+" <br>Falsch: "+f.wrong+"</div>")
130 def WrongAnswer(self):
131 self.SendHeader()
132 self.ShowHead()
133 self.wfile.write("<div class=wronganswer>Falsche Antwort</div>")
134 self.DisplayQuestion()
135 self.wfile.write("<div class=correctanswer>"+f.answercorrect.encode("utf8")+"</div>")
138 def StartDisplay(self):
139 self.SendHeader()
141 self.wfile.write("<frameset border=0 frameborder=0 framespacing=0 marginwidth=0 rows=30px,*>")
142 self.wfile.write("<frame name=menue src=menue.afu scrolling=no noresize>")
143 self.wfile.write("<frame name=main src=askquestion.afu scrolling=auto noresize>")
144 self.wfile.write("</frameset>")
146 def AskQuestion(self,update=True):
147 self.ShowHead()
148 self.wfile.write("<body>")
150 if update:
151 f.AskQuestion()
153 self.DisplayQuestion()
155 self.wfile.write("<div class=answer>")
156 self.wfile.write("<a href="+base+"/a.afu class=button1>A</a>"+f.answera.encode("utf8")+"<br>")
157 self.wfile.write("<a href="+base+"/b.afu class=button1>B</a>"+f.answerb.encode("utf8")+"<br>")
158 self.wfile.write("<a href="+base+"/c.afu class=button1>C</a>"+f.answerc.encode("utf8")+"<br>")
159 self.wfile.write("<a href="+base+"/d.afu class=button1>D</a>"+f.answerd.encode("utf8")+"<br>")
160 self.wfile.write("</div>")
162 self.wfile.write("<div class=button>")
163 self.wfile.write("<a href="+base+"/a.afu class=button>A</a>")
164 self.wfile.write("<a href="+base+"/b.afu class=button>B</a>")
165 self.wfile.write("<a href="+base+"/c.afu class=button>C</a>")
166 self.wfile.write("<a href="+base+"/d.afu class=button>D</a>")
167 self.wfile.write("</div>")
169 self.wfile.write("<div class=hint>")
170 self.wfile.write("<a href="+base+"/hint.afu target=hint>Hinweis</a></div>")
172 self.wfile.write("</body></html>")
174 def DisplayMenu(self):
175 self.SendHeader()
177 self.wfile.write("<html><head><base target=main><link href="+stylefile+" rel=stylesheet type=text/css></head>")
178 self.wfile.write("<body class=menue><div class=menue>")
179 self.wfile.write("<a class=menue href="+base+"/askquestion.afu>Abfragen</a>")
180 self.wfile.write("<a class=menue href="+base+"/showquestion.afu>Zur&uuml;ck zur Frage</a>")
181 self.wfile.write("<a class=menue href="+base+"/method.afu>Abfragemethode</a>")
182 self.wfile.write("<a class=menue href="+base+"/statistic.afu>Statistik</a>")
183 self.wfile.write("</div></body></html>")
186 def do_POST(self):
187 global rootnode
188 ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
189 if ctype == 'multipart/form-data':
190 query=cgi.parse_multipart(self.rfile, pdict)
191 self.send_response(301)
193 self.end_headers()
194 upfilecontent = query.get('upfile')
195 print "filecontent", upfilecontent[0]
196 self.wfile.write("<HTML>POST OK.<BR><BR>");
197 self.wfile.write(upfilecontent[0]);
199 def main():
200 global f,port,base,stylefile
201 port = 8080
202 base = "http://127.0.0.1:"+str(port)+"/"
203 stylefile = base+"/style.css"
205 try:
206 f = framework.Framework()
207 server = HTTPServer(('', port), MyHandler)
208 print 'Started httpserver on port',port
209 print "Terminate with Ctrl+C"
210 server.serve_forever()
211 except KeyboardInterrupt:
212 print '^C received, shutting down http server'
213 f.Close()
214 server.socket.close()
216 if __name__ == '__main__':
217 main()