Posted 01 April 2012 - 06:03 AM
Posted 01 April 2012 - 10:00 AM
import android
import json
import time
from urllib import urlencode
from urllib2 import urlopen
hello_msg = "Welcome to NUAC's Timing App"
list_title = 'Here is your list of athletes: '
quit_msg = "Quitting Coach Kelly's App"
web_server = 'http://192.168.178.58:8080'
get_names_cgi = '/cgi-bin/generate_names.py'
get_data_cgi = '/cgi-bin/generate_data.py'
def send_to_server(url, post_data=None):
if post_data:
page = urlopen(url, urlencode(post_data))
else:
page = urlopen(url)
return(page.read().decode("utf8"))
app = android.Android()
def status_update(msg, how_long=2):
app.makeToast(msg)
time.sleep(how_long)
status_update(hello_msg)
athletes = sorted(json.loads(send_to_server(web_server + get_names_cgi)))
athlete_names = [ath[0] for ath in athletes]
app.dialogCreateAlert(list_title)
app.dialogSetSingleChoiceItems(athlete_names)
app.dialogSetPositiveButtonText('Select')
app.dialogSetNegativeButtonText('Quit')
app.dialogShow()
resp = app.dialogGetResponse().result
if resp['which'] in ('positive'):
selected_athlete = app.dialogGetSelectedItems().result[0]
which_athlete = athletes[selected_athlete][1]
athlete = json.loads(send_to_server(web_server + get_data_cgi,{'which_athlete': which_athlete}))
athlete_title = athlete['Name'] + ' (' + athlete['DOB'] + '), top 3 times:'
app.dialogCreateAlert(athlete_title)
app.dialogSetItems(athlete['top3'])
app.dialogSetPositiveButtonText('OK')
app.dialogSetNegativeButtonText('Add Time') # new button
if resp['which'] in ('positive'):
pass
elif resp['which'] in ('negative'):
timing_title = 'Enter a new time:'
timing_msg = 'Provide a new timing value ' + athlete['Name'] + ': '
add_time_cgi = '/cgi-bin/add_timing_data.py'
resp = app.dialogGetInput(timing_title, timing_msg).result
if resp is not None:
new_time = resp
send_to_server(web_server + add_time_cgi,{'Time': new_time, 'athlete': which_athlete})
app.dialogShow()
resp = app.dialogGetResponse().result
status_update(quit_msg)
Posted 08 April 2012 - 06:58 AM
Posted 10 April 2012 - 09:29 AM
add_time_cgi = '/cgi-bin/add_timing_data.py'
192.168.178.65 - - [10/Apr/2012 19:16:12] "POST /cgi-bin/generate_data.py HTTP/1.1" 200 - 192.168.178.65 - - [10/Apr/2012 19:16:17] command: C:\Python32\python.exe -u C:\Users\Dirk\Documents\python_scripts\HeadFirst _Python\chapter9\webapp\cgi-bin\generate_data.py "" 192.168.178.65 - - [10/Apr/2012 19:16:21] CGI script exited OK
import cgi
import sqlite3
import yate
print(yate.start_response('text/plain'))
form_data = cgi.FieldStorage() # might be a cause of the problem as well?
the_id = form_data['Athlete'].value
the_time = form_data['Time'].value
connection = sqlite3.connect('coachdata.sqlite')
cursor = connection.cursor()
cursor.execute("INSERT INTO timing_data (athlete_id, value) VALUES (?, ?)",
(the_id, the_time))
connection.commit()
connection.close()
print('OK.')
Posted 17 April 2012 - 04:37 AM
Posted 17 April 2012 - 11:00 AM
Posted 17 April 2012 - 11:24 AM
Posted 18 April 2012 - 11:22 AM
...
app.dialogSetItems(athlete['top3'])
app.dialogSetPositiveButtonText('OK')
app.dialogSetNegativeButtonText('Add Time') # new button
app.dialogShow()
resp = app.dialogGetResponse().result
if resp['which'] in ('positive'):
pass
elif resp['which'] in ('negative'):
timing_title = 'Enter a new time:'
timing_msg = 'Provide a new timing value ' + athlete['Name'] + ': '
add_time_cgi = '/cgi-bin/add_timing_data.py'
resp = app.dialogGetInput(timing_title, timing_msg).result
if resp is not None:
new_time = resp
send_to_server(web_server + add_time_cgi,{'Time': new_time, 'athlete': which_athlete})
...
Posted 22 April 2012 - 07:52 AM
192.168.178.65 - - [22/Apr/2012 17:30:02] command: C:\Python32\python.exe -u C:\Users\Dirk\Documents\python_scripts\HeadFirst _Python\chapter9\webapp\cgi-bin\add_timing_data.py "" 192.168.178.65 - - [22/Apr/2012 17:30:07] b'Traceback (most recent call last):\n File "C:\\Users\\Dirk\\Documents\\python_sc ripts\\HeadFirst_Python\\chapter9\\webapp\\cgi-bin\\add_timing_data.py", line 9, in <module>\n the_id = form_data[\'Athlet e\'].value\n File "C:\\Python32\\lib\\cgi.py", line 575, in __getitem__\n raise KeyError(key)\nKeyError: \'Athlete\'\n' 192.168.178.65 - - [22/Apr/2012 17:30:11] CGI script exit status 0x1