def work_it(in_menu, in_category = 1): msg = '' bit = '' if (in_category == 1): head = "<h1>On Top of Your A-Game Groove?</h1>\n" elif (in_category == 2): head = "<h1>Feelin' More Like a B-Game Groove?</h1>\n" elif (in_category == 3): head = "<h1>Ready for some R-Ejuvenation?</h1>\n" else: head = "<h1>Please select the groove that matches your energy level</h1>\n" task = select_task(in_menu, in_category) if (task != None): desc = task.get_description() a_pri = task.get_priority() a_cat = task.get_category() key = Key.Key(in_cat = a_cat, in_pri = a_pri) if(in_menu.cur_task_bumped(key.get_str())): old_task = in_menu.get_task_from_key(in_menu.get_cur_task()) bit += """<script type="text/javascript"><!-- var bumped = '<h2>Your task has been bumped.</h2>' bumped += 'Thank you for your diligent work on your last task, <b><i>%s. </i></b>' bumped += 'Either a task of greater importance has become available, ' bumped += 'your last task is no longer available, or ' bumped += 'GrooveTask is open in multiple browser windows.' setTimeout("alert(bumped);", 3000) // --></script>""" % old_task.get_description().replace("'", "") in_menu.update_seconds() in_menu.set_comm_time(key.get_str()) pr(' just set_comm_time ') if(task.max_time_surpassed()): notice= "It's time to move on. <br>Please select the done button" bit +="<h2>" + notice + "</h2>" bit += "Thank you for working on task: " + desc bit += """<script type="text/javascript"><!-- setTimeout("document.title = 'Time is Up!';", 1000); var msg = 'Congratulations on a task well done. <br>' msg += 'It is time to move on. Please select the done button.' setTimeout("alert(msg);", 2000) // --></script>""" else: bit += head bit += "<p>Your mission, should you choose to accept it, is to </p>" bit += "<h2>" + desc + "</h2>\n" bit += display_time_spent(task) else: desc = 'Sorry, no more tasks in this groove' bit += head bit += "<h2>" + desc + "</h2>\n" if (task != None): bit += display_done_button(key.get_str()) bit += "The time is now " + now_string(in_menu.get_utcoffset()) bit += ".<br>Your timezone is set to " + str(in_menu.get_utcoffset()) + '.' bit += display_groove_select(in_category) return(bit)
def create_user_table(self, cursor): cursor.execute ("""CREATE TABLE Users( userid int, username varchar(59), pass_hash int, cur_task varchar(7), comm_time int, --Note comm_time is in seconds utcoffset int, -- Note this is -7 for Mountain Time PRIMARY KEY (userid) )""") pr("Remember to 'GRANT ALL PRIVILEGES ON TABLE Users TO jackdesert_groove'")
def create_table(self, cursor): cursor.execute ("""CREATE TABLE Tasks( category int, priority int, min_minutes int, max_minutes int, doors_open_time time, doors_close_time time, do_at_exactly_time time, description varchar(250), seconds int, userid int, PRIMARY KEY (category, priority, userid) )""") #~ pr("Table Created") pr("Remember to 'GRANT ALL PRIVILEGES ON TABLE Tasks TO jackdesert_groove'")
def cur_task_bumped(self, in_cat_pri_string): query = "SELECT cur_task FROM Users" query += " WHERE userid = " + str(self.get_userid()) self.cur.execute(query) result = self.cur.fetchone() cur_task = result[0] if(cur_task == None): return(False) # No task saved, so not bumped elif(cur_task[0:3] != in_cat_pri_string[0:3]): return(False) # Different category selected, so not bumped elif(cur_task == in_cat_pri_string): return(False) # Exact same task selected, so not bumped else: pr('cur_task as a string is ' + cur_task) pr('in_cat_pri_string is ' + in_cat_pri_string) return(True) # Same category, different priority, BUMPED!
def set_comm_time(self, in_cat_pri_string, seconds_ago = None): if (self.exists(in_cat_pri_string)): pass else: raise ValueError('Cannot set comm_time without pri_key match') seconds = int(unixtime()) if(seconds_ago): # Set to a time in the past for testing assert(type(seconds_ago) == type(100)) # Must be int seconds -= seconds_ago query = "UPDATE Users SET comm_time = " + str(seconds) query += ", cur_task = '" + in_cat_pri_string query += "' WHERE userid = " + str(self.get_userid()) self.cur.execute(query) self.db.commit() pr('query executed is:::::::: ' + query) pr('string is :::::::::::::::: ' + in_cat_pri_string) return(seconds)
def test_02_technical_form(self): import Task import re A = rand_string(None) aTask = Task.Task(['1','2','3','4','5','6','7', A, '0']) B = technical_form(aTask) pr(B) self.assert_('<form' in B) # Use a dictionary to store key-value pairs. The key is the # search string. The value is how many times it should show up expr_dict = {r'<form': 1, r'</form>': 1, r"<input type='hidden' NAME='pri_key' value=": 1, r"(<td>[^<>]*?</td>)": 7, r"(<td>.*?</td>)": 10} for a_str, a_val in expr_dict.items(): found = re.findall(a_str, B) print ("the string '" + a_str + "' was found " + str(len(found)) + " times") for res in found: print ("the string '" + a_str + "' was found as " + res) self.assertEquals(len(found), a_val)
def create_user(self, in_username, in_password): if (self.username_exists(in_username)): pr('Username already exists') return(None) #raise(ValueError, 'Username already exists') else: pass_hash = hash(in_password) username = cgi.escape(in_username) query = "SELECT MAX(userid) FROM Users" self.cur.execute(query) result = self.cur.fetchall() result_str = result[0][0] if (result_str == None): userid = 1 else: userid = (int(result_str) + 1) query = "INSERT INTO Users (userid, username, pass_hash, utcoffset)" + \ " VALUES (" + str(userid) + ", '" + username + "', " + str(pass_hash) + ', -5)' #~ pr("\n\n About to insert a user") #~ pr(query) self.cur.execute(query) self.db.commit() #~ pr("create_user query executed") return(userid)
def clear_cur_task(self): query = "UPDATE Users SET cur_task = NULL" query += " WHERE userid = " + str(self.get_userid()) self.cur.execute(query) self.db.commit() pr('Inside clear_cur_task query is::::::::' + query)
def application(environ, start_response): # the environment variable CONTENT_LENGTH may be empty or missing try: request_body_size = int(environ.get('CONTENT_LENGTH', 0)) except (ValueError): request_body_size = 0 # When the method is POST the query string will be sent # in the HTTP request body which is passed by the WSGI server # in the file like wsgi.input environment variable. request_body = environ['wsgi.input'].read(request_body_size) d = parse_qs(request_body) in_category = d.get('category', [''])[0] # Returns the first age value. if ((in_category == None) | (in_category == '')): in_category = 1 # hobbies = d.get('hobbies', []) # Returns a list of hobbies. # in_priority = d.get('priority', [''])[0] # Returns the first age value. in_priority = d.get('priority', [''])[0] # Returns the first age value. in_min_minutes = d.get('min_minutes', [''])[0] # Returns the first age value. in_max_minutes = d.get('max_minutes', ['NULL'])[0] # Returns the first age value. in_doors_open_time = d.get('doors_open_time', [''])[0] # Returns the first age value. in_doors_close_time = d.get('doors_close_time', [''])[0] # Returns the first age value. in_do_at_exactly_time = d.get('do_at_exactly_time', [''])[0] # Returns the first age value. in_description = d.get('description', [''])[0] # Returns the first age value. in_username = d.get('username', [''])[0] in_username = escape(in_username).replace(';', '') # No semicolons in_password = d.get('password', [''])[0] in_password = escape(in_password).replace(';', '') # No semicolons in_seconds = '0' in_add_to_cat = d.get('add_to_cat', [''])[0] # Add a task to this category new_Task = Task.Task([in_category, in_priority, in_min_minutes, in_max_minutes, in_doors_open_time, in_doors_close_time, in_do_at_exactly_time, in_description, in_seconds]) # If the Submit Button was clicked, insert the new task in_sbutton = d.get('sbutton', [''])[0] in_gbutton = d.get('gbutton', [''])[0] in_Lbutton = d.get('Lbutton', [''])[0] in_up_down = d.get('up_down', [''])[0] in_pri_key_string = d.get('pri_key', [''])[0] if (in_pri_key_string != ''): in_key = Key.Key(in_pri_key_string) #~ pr('in_pri_key_string is ' + in_pri_key_string) task_to_edit = '999_999' # Dummy value that won't match request = Request(environ) username_cookie = request.cookies.get('username_cookie') password_cookie = request.cookies.get('password_cookie') #~ pr("username_cookie is " + str(username_cookie)) #~ pr("password_cookie is " + str(password_cookie)) #~ pr("in_username is " + in_username) #~ pr("in_password is " + in_password) val_menu = Menu.Menu(999) cookies = [] if(in_Lbutton == 'register'): userid = val_menu.create_user(in_username, in_password) if(in_username == ''): message = "<p style='color: blue;'>Enter your desired username and password, then click <i>Register</i> again.</p>" elif(userid): message = """Thank you for creating an account with us. Please enjoy GrooveTask.com<br>""" cookies = [('Set-Cookie', 'username_cookie = ' + in_username), ('Set-Cookie', 'password_cookie = ' + in_password)] else: message = """Great Taste! But someone already chose that username. Please try your next-favorite name""" elif(in_Lbutton == 'logout'): if ((username_cookie) and (password_cookie)): in_username = username_cookie.value in_password = password_cookie.value logout_userid = val_menu.validate(in_username, in_password) #~ pr('logging out, so clearing cur_task') if(logout_userid): logout_Menu = Menu.Menu(logout_userid) logout_Menu.update_seconds() logout_Menu.clear_cur_task() # Now clear everything out userid = None cookies = [('Set-Cookie', 'username_cookie = none'), ('Set-Cookie', 'password_cookie = none')] message = """You are successfully logged out. Thank you for using GrooveTask. Please come again.""" elif(in_username != ''): userid = val_menu.validate(in_username, in_password) cookies = [('Set-Cookie', 'username_cookie = ' + in_username), ('Set-Cookie', 'password_cookie = ' + in_password)] #~ pr ('logging in through form data') if(userid): message = '' #"You are logged in! Enjoy Grooves!" else: message = """I'm sorry. The username/password you entered failed. Please try again""" elif ((username_cookie) and (password_cookie)): in_username = username_cookie.value in_password = password_cookie.value userid = val_menu.validate(in_username, in_password) pr ('cookie access by user: '******'Please Log In' if(userid): message = '' else: userid = None message = """First-Timers are always welcome here!""" #################################################################### ########### S T A R T S E C O N D B I G L O O P ######## if(userid != None): #~ pr('inside userid!=None sequence') #~ pr('in_sbutton is ' + in_sbutton) new_Menu = Menu.Menu(userid) if (in_sbutton == 'Clean Slate'): try: new_Menu.reset_table(in_add_to_cat) except: pr('ERROR: wrong in_add_to_cat passed to reset_table()') pr('in_add_to_cat is ' + in_add_to_cat) in_add_to_cat = '' # clear this so input form not displayed elif (in_sbutton == 'Submit'): new_Task.set_priority(new_Menu.get_next_priority(int(in_category))) new_Menu.insert_task(new_Task) # Insert once #~ pr('inside submit sequence') elif (in_sbutton == 'Update'): new_Menu.update_task(new_Task, in_pri_key_string) #~ pr('inside update_task sequence') elif(in_sbutton == 'Delete'): #~ pr('inside delete sequence') new_Menu.delete_task(in_pri_key_string) elif (in_up_down == 'up'): #~ pr('inside up sequence') new_Menu.bump_priority(in_key.get_cat(), in_key.get_pri(), in_key.get_pri() -1) elif (in_up_down == 'bump'): #~ pr('inside down sequence') new_Menu.bump_priority(in_key.get_cat(), in_key.get_pri(), 1) elif (in_up_down == 'edit'): #~ pr('inside edit sequence') task_to_edit = in_pri_key_string else: pass #~ pr('No options matched') title = "<h1>Plan Your Day!</h1>" greeting = display_greeting(in_username) go_to_work = display_groove_select() vert_work = display_groove_select(vertical=True) add_a_task = large_form(None) task_data = "<div id=task_list>" task_data += title task_data += display_tasks_from_database(new_Menu, task_to_edit, in_add_to_cat) task_data += "</div>" sidebar = "<div id='sidebar'>" sidebar += greeting sidebar += vert_work sidebar += "<br>" sidebar += "</div>" response_body = basic_html + sidebar + message + task_data else: response_body = basic_html + display_login(message) + introduction() title = "<h1>Go to Work!</h1>" greeting = basic_html + display_greeting(in_username) if(in_gbutton == 'A-Energy'): response_body = greeting + work_it(new_Menu, 1) pr('hello from A') elif(in_gbutton == 'B-Energy'): response_body = greeting + work_it(new_Menu, 2) pr('hello from B') elif(in_gbutton == 'R-Ejuvenate'): response_body = greeting + work_it(new_Menu, 3) pr('hello from R') elif(in_gbutton == 'task_done'): declare_task_done(new_Menu, in_pri_key_string) response_body = greeting + work_it(new_Menu, in_key.get_cat()) pr('hello from task_done') elif(userid != None): # make sure you account for time and clear cur_task when you 'Plan Your Day' new_Menu.update_seconds() pr(' line 210 For some reason this entire script is running twice. ') new_Menu.clear_cur_task() pr(' line 212 And the second time through it clears cur_task and then it cant do shit. Why?') # Note this runs any time you are still logged in status = '200 OK' response_headers = cookies + [('Content-Type', 'text/html'), ('Content-Length', str(len(response_body)))] start_response(status, response_headers) return [response_body]
if(in_gbutton == 'A-Energy'): response_body = greeting + work_it(new_Menu, 1) pr('hello from A') elif(in_gbutton == 'B-Energy'): response_body = greeting + work_it(new_Menu, 2) pr('hello from B') elif(in_gbutton == 'R-Ejuvenate'): response_body = greeting + work_it(new_Menu, 3) pr('hello from R') elif(in_gbutton == 'task_done'): declare_task_done(new_Menu, in_pri_key_string) response_body = greeting + work_it(new_Menu, in_key.get_cat()) pr('hello from task_done') elif(userid != None): # make sure you account for time and clear cur_task when you 'Plan Your Day' new_Menu.update_seconds() pr(' line 210 For some reason this entire script is running twice. ') new_Menu.clear_cur_task() pr(' line 212 And the second time through it clears cur_task and then it cant do shit. Why?') # Note this runs any time you are still logged in status = '200 OK' response_headers = cookies + [('Content-Type', 'text/html'), ('Content-Length', str(len(response_body)))] start_response(status, response_headers) return [response_body] if __name__ == '__main__': pr('a helluva lotta love coming from that abbers ;)')