Created
November 11, 2012 14:05
-
-
Save sahib/4054981 to your computer and use it in GitHub Desktop.
Musterlösung für die Flask Aufgabe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# encoding: utf-8 | |
''' | |
Das ist die eigentliche Anwendung die ihr schreiben sollt. | |
Unten findet ihr bereits eine minimale Flask Anwendung. | |
Jetzt müsst ihr sie nur noch erweitern. | |
''' | |
import json | |
from flask import Flask, render_template, redirect | |
from loader.load import load, count, list_courses, NoSuchCourse | |
app = Flask('practice') | |
@app.route('/') | |
def root(): | |
return '<b>Well nothing here yet. Thats your job.</b>' | |
########################################################################### | |
# Aufgabe a) # | |
########################################################################### | |
@app.route('/api/<studiengang>/<int:semester>') | |
def api(studiengang, semester): | |
try: | |
return json.dumps(load(studiengang, semester)) | |
except NoSuchCourse: | |
return redirect('404.html'), 404 | |
########################################################################### | |
# Aufgabe b) # | |
########################################################################### | |
@app.route('/api/count/<studiengang>') | |
def api_count_filter(studiengang): | |
if studiengang == 'all': | |
return str(count()) | |
else: | |
return str(count(studiengang=studiengang)) | |
@app.route('/api/list_courses') | |
def api_list(): | |
courses = list_courses() | |
courses.sort() | |
return json.dumps(courses) | |
########################################################################### | |
# Aufgabe c) # | |
########################################################################### | |
@app.route('/view/<studiengang>/<int:semester>') | |
def view(studiengang, semester): | |
try: | |
data = load(studiengang, semester) | |
data = iter(sorted(data.items())) | |
return render_template('sched_table.html', schedule=data) | |
except NoSuchCourse: | |
return render_template('sched_table.html', schedule={}) | |
########################################################################### | |
# Let it run... # | |
########################################################################### | |
if __name__ == '__main__': | |
app.run(debug=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<title>Schedule</title> | |
<style> | |
/* From http://pythoughts.com/table-style-css/ */ | |
#table { | |
border: 1px solid #DFDFDF; | |
background-color: #F9F9F9; | |
width: 100%; | |
-moz-border-radius: 3px; | |
-webkit-border-radius: 3px; | |
border-radius: 3px; | |
font-family: Arial,"Bitstream Vera Sans",Helvetica,Verdana,sans-serif; | |
color: #333; | |
} | |
#table td, #table th { | |
border-top-color: white; | |
border-bottom: 1px solid #DFDFDF; | |
color: #555; | |
} | |
#table th { | |
text-shadow: rgba(255, 255, 255, 0.796875) 0px 1px 0px; | |
font-family: Georgia,"Times New Roman","Bitstream Charter",Times,serif; | |
font-weight: normal; | |
padding: 7px 7px 8px; | |
text-align: left; | |
line-height: 1.3em; | |
font-size: 14px; | |
} | |
#table td { | |
font-size: 12px; | |
padding: 4px 7px 2px; | |
vertical-align: top; | |
} | |
</style> | |
<!-- Table Stuff: --> | |
<table id="table"> | |
{% for day, data in schedule %} | |
<thead> | |
<th> | |
<b>{{ day }}</b> | |
</th> | |
</thead> | |
<thead> | |
{% for key in data[0].keys() %} | |
<th><i>{{ key }}<i></th> | |
{% endfor %} | |
</thead> | |
{% for sched in data %} | |
<tbody> | |
{% for val in sched.values() %} | |
<td>{{ val }}</th> | |
{% endfor %} | |
</tbody> | |
{% endfor %} | |
{% endfor %} | |
</table> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment