.
[HamExam.git] / interface.py
blobe2d8d7277d6f13655def85845637dc1b2c65ecac
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 ff = self.send_head()
13 if f:
14 self.copyfile(ff, self.wfile)
15 ff.close()
17 def do_HEAD(self):
18 ff = self.send_head()
19 if ff:
20 ff.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 ff = 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 ff
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 self.SendHeader()
77 if self.path.endswith("/a.afu") or self.path.endswith("/b.afu") or self.path.endswith("/c.afu") or self.path.endswith("/d.afu"):
78 answer = (self.path.split("/")[-1]).replace(".afu","")
79 if not f.EvalQuestion (answer):
80 self.WrongAnswer()
81 else:
82 self.AskQuestion()
83 elif self.path.endswith("menue.afu"):
84 self.DisplayMenu()
85 elif self.path.endswith("method.afu"):
86 self.DisplayMethod()
87 elif self.path.endswith("methodNew.afu"):
88 f.Method ("NewQuestions")
89 self.AskQuestion()
90 elif self.path.endswith("methodBad.afu"):
91 f.Method ("BadQuestions")
92 self.AskQuestion()
93 elif self.path.endswith("methodAny.afu"):
94 f.Method ("AnyQuestions")
95 self.AskQuestion()
96 elif self.path.endswith("methodGood.afu"):
97 f.Method ("GoodQuestions")
98 self.AskQuestion()
99 elif self.path.endswith("statistic.afu"):
100 self.DisplayStatistics()
101 elif self.path.endswith("askquestion.afu"):
102 self.AskQuestion()
103 elif self.path.endswith("showquestion.afu"):
104 self.AskQuestion(update=False)
105 elif self.path.endswith("catalogTechnikA.afu"):
106 #f.close()
107 #f = framework.Framework(catalog="TechnikA")
108 self.StartDisplay()
109 elif self.path.endswith("catalogTechnikE.afu"):
110 #f.close()
111 #f = framework.Framework(catalog="TechnikE")
112 self.StartDisplay()
113 elif self.path.endswith("catalogBetriebAE.afu"):
114 #f.close()
115 #f = framework.Framework(catalog="BetriebAE")
116 self.StartDisplay()
117 else:
118 self.StartDisplay()
120 def DisplayStatistics(self):
121 self.ShowHead()
122 self.wfile.write ("<h2>Statistics...</h2>")
124 self.wfile.write ("<ul><li>Questions: "+str(len(f.q.questions)))
125 self.wfile.write ("<li>Good: "+str(len(f.s.goodquestion)))
126 self.wfile.write ("<li>Bad: "+str(len(f.s.badquestion)))
127 self.wfile.write ("<li>New: "+str(len(f.s.newquestion)))
128 self.wfile.write ("</ul>")
130 def DisplayMethod(self):
131 self.ShowHead()
132 self.wfile.write ("<h2>Abfragemethode</h2>")
133 self.wfile.write ("<ul>")
134 self.wfile.write ("<li><a href="+base+"/methodAny.afu>Irgendwelche Fragen</a>")
135 self.wfile.write ("<li><a href="+base+"/methodNew.afu>Neue Fragen</a>")
136 self.wfile.write ("<li><a href="+base+"/methodBad.afu>Schwierige Fragen</a>")
137 self.wfile.write ("<li><a href="+base+"/methodGood.afu>Einfache Fragen</a>")
138 self.wfile.write ("</ul>")
140 self.wfile.write ("<h2>Fragenkatalog</h2>")
141 self.wfile.write ("<ul>")
142 self.wfile.write ("<li><a href="+base+"/catalogTechnikA.afu>Technik A</a>")
143 self.wfile.write ("<li><a href="+base+"/catalogTechnikE.afu>Technik E</a> FIXME ") # FIXME
144 self.wfile.write ("<li><a href="+base+"/catalogBetriebAE.afu>Betrieb AE</a> FIXME ") # FIXME
145 self.wfile.write ("</ul>")
147 def ShowHead(self,question=False):
148 self.wfile.write ("<html><head><base href="+base+f.question_dir+">")
149 self.wfile.write ("<link href="+stylefile+" rel=stylesheet type=text/css>")
150 self.wfile.write ("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf8\">")
152 if question:
153 self.wfile.write ("<SCRIPT LANGUAGE=\"JavaScript\">")
154 self.wfile.write ("var inNav = navigator.appVersion.indexOf(\"MSIE\") < 0;")
155 self.wfile.write ("function eval_key (event) {")
156 self.wfile.write ("var key = (inNav==1) ? event.which : event.keyCode;")
157 self.wfile.write ("if (key == 65) { window.location = \""+base+"/a.afu\";")
158 self.wfile.write ("} else if (key == 66) { window.location = \""+base+"/b.afu\";")
159 self.wfile.write ("} else if (key == 67) { window.location = \""+base+"/c.afu\";")
160 self.wfile.write ("} else if (key == 68) { window.location = \""+base+"/d.afu\";")
161 self.wfile.write ("} else if (key == 78 || key == 83) { window.location = \""+base+"/askquestion.afu\";")
162 self.wfile.write ("}")
163 self.wfile.write ("}")
164 self.wfile.write ("document.onkeydown = eval_key;")
165 self.wfile.write ("</script>")
167 self.wfile.write ("</head><body>")
169 def SendHeader(self):
170 self.send_response(200)
171 self.send_header('Content-type', 'text/html')
172 self.end_headers()
174 def DisplayQuestion(self):
175 self.wfile.write("<div class=id>"+f.id+"</div>")
176 self.wfile.write("<div class=question>")
177 self.wfile.write(f.question.encode("utf8"))
178 self.wfile.write("</div>")
179 self.wfile.write("<div class=statistics>Richtig: "+f.correct_successive+"("+f.correct+")"+" <br>Falsch: "+f.wrong_successive+"("+f.wrong+")"+"</div>")
181 def WrongAnswer(self):
182 self.ShowHead(question=True)
183 self.wfile.write("<div class=wronganswer>Falsche Antwort</div>")
184 self.DisplayQuestion()
185 self.wfile.write("<div class=correctanswer>"+f.answercorrect.encode("utf8")+"</div>")
187 self.wfile.write("<div class=hint>")
188 self.wfile.write("<a href="+base+f.hint_dir+f.hint+" target=hint>Hinweis</a></div>")
191 def StartDisplay(self):
192 self.wfile.write("<frameset border=0 frameborder=0 framespacing=0 marginwidth=0 rows=30px,*>")
193 self.wfile.write("<frame name=menue src=menue.afu scrolling=no noresize>")
194 self.wfile.write("<frame name=main src=askquestion.afu scrolling=auto noresize>")
195 self.wfile.write("</frameset>")
197 def AskQuestion(self,update=True):
198 self.ShowHead(question=True)
199 self.wfile.write("<body>")
201 if update:
202 f.AskQuestion()
204 self.DisplayQuestion()
206 self.wfile.write("<div class=answer>")
207 self.wfile.write("<a href="+base+"/a.afu class=button1>A</a>"+f.answera.encode("utf8")+"<br>")
208 self.wfile.write("<a href="+base+"/b.afu class=button1>B</a>"+f.answerb.encode("utf8")+"<br>")
209 self.wfile.write("<a href="+base+"/c.afu class=button1>C</a>"+f.answerc.encode("utf8")+"<br>")
210 self.wfile.write("<a href="+base+"/d.afu class=button1>D</a>"+f.answerd.encode("utf8")+"<br>")
211 self.wfile.write("</div>")
213 self.wfile.write("<div class=button>")
214 self.wfile.write("<a href="+base+"/a.afu class=button>A</a>")
215 self.wfile.write("<a href="+base+"/b.afu class=button>B</a>")
216 self.wfile.write("<a href="+base+"/c.afu class=button>C</a>")
217 self.wfile.write("<a href="+base+"/d.afu class=button>D</a>")
218 self.wfile.write("</div>")
220 self.wfile.write("<div class=hint>")
221 self.wfile.write("<a href="+base+f.hint_dir+f.hint+" target=hint>Hinweis</a></div>")
223 self.wfile.write("</body></html>")
225 def DisplayMenu(self):
226 self.wfile.write("<html><head><base target=main><link href="+stylefile+" rel=stylesheet type=text/css></head>")
227 self.wfile.write("<body class=menue><div class=menue>")
228 self.wfile.write("<a class=menue href="+base+"/askquestion.afu>Abfragen</a>")
229 self.wfile.write("<a class=menue href="+base+"/method.afu>Abfragemethode</a>")
230 self.wfile.write("<a class=menue href="+base+"/statistic.afu>Statistik</a>")
231 self.wfile.write("</div></body></html>")
234 def do_POST(self):
235 global rootnode
236 ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
237 if ctype == 'multipart/form-data':
238 query=cgi.parse_multipart(self.rfile, pdict)
239 self.send_response(301)
241 self.end_headers()
242 upfilecontent = query.get('upfile')
243 print "filecontent", upfilecontent[0]
244 self.wfile.write("<HTML>POST OK.<BR><BR>");
245 self.wfile.write(upfilecontent[0]);
247 def main():
248 global f,port,base,stylefile
249 port = 8080
250 base = "http://127.0.0.1:"+str(port)+"/"
251 stylefile = base+"/style.css"
253 try:
254 f = framework.Framework()
255 server = HTTPServer(('', port), MyHandler)
256 print 'Started httpserver on port',port
257 print "Terminate with Ctrl+C"
258 server.serve_forever()
259 except KeyboardInterrupt:
260 print '^C received, shutting down http server'
261 f.Close()
262 server.socket.close()
264 if __name__ == '__main__':
265 main()