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