def testDB(users, teams, request): if os.path.exists(DEFAULT_TEST_DB): print("Clean old test database.") os.remove(DEFAULT_TEST_DB) UserDB.connect(DEFAULT_TEST_DB) UserDB.initSchema() for team in teams.values(): UserDB.insertTeam(team["teamName"]) user_values = [] for user in users.values(): user_values.append([user["firstname"], user["lastname"], user["teamId"], user["first10k"]]) UserDB.insertUsers(user_values) def testDB_teardown(): # Teardown is called when a method using testDB ends. UserDB.close() request.addfinalizer(testDB_teardown)
def result_post(): team_name = request.form.get("teamName").strip() r1_firstname = request.form.get("r1FirstName").strip() r1_lastname = request.form.get("r1LastName").strip() r2_firstname = request.form.get("r2FirstName").strip() r2_lastname = request.form.get("r2LastName").strip() r3_firstname = request.form.get("r3FirstName").strip() r3_lastname = request.form.get("r3LastName").strip() r4_firstname = request.form.get("r4FirstName").strip() r4_lastname = request.form.get("r4LastName").strip() user1={"firstname" : r1_firstname, "lastname" : r1_lastname} user2={"firstname" : r2_firstname, "lastname" : r2_lastname} user3={"firstname" : r3_firstname, "lastname" : r3_lastname} user4={"firstname" : r4_firstname, "lastname" : r4_lastname} with app.app_context(): db = get_db() data = db.checkTeam(team_name, user1, user2, user3, user4) success = data["success"] if success: # Insert new team UserDB.insertTeam(team_name) team = UserDB.getTeamByTeamName(team_name) team_id = team[0] # Update teamId at UserDB.updateTeamByUser(team_id, r1_firstname, r1_lastname) UserDB.updateTeamByUser(team_id, r2_firstname, r2_lastname) UserDB.updateTeamByUser(team_id, r3_firstname, r3_lastname) UserDB.updateTeamByUser(team_id, r4_firstname, r4_lastname) # TODO: # - Read and prepare team_data users = UserDB.getUsersWithTeam() # - Upload team_data to spreadsheet team_dict = {} for user_team in users: firstname, lastname, team_id, first10k, team_name, timestamp = user_team if team_id not in team_dict: # time format: '2018-11-18 02:29:13' time_obj = datetime.strptime(timestamp, DEFAULT_TIME_FORMAT) # Convert to BKK timezone time_obj = time_obj + timedelta(hours=7) team_dict[team_id] = { "timestamp" : time_obj.strftime(DEFAULT_TIME_FORMAT), "team_name" : team_name, "members" : ["%s %s" % (firstname, lastname)] } else: team_dict[team_id]["members"].append("%s %s" % (firstname, lastname)) # - Convert to spreadsheet format team_rows = [] for idx, data in enumerate(team_dict.items()): # data is tuple of key and value. row = [idx + 1, data[1]["timestamp"], data[1]["team_name"], *data[1]["members"]] team_rows.append(row) team_spread.update_team(team_rows) return render_template("result.html", success=success, teamName=team_name, user1={ "firstname" : r1_firstname, "lastname" : r1_lastname, }, user2={ "firstname" : r2_firstname, "lastname" : r2_lastname, }, user3={ "firstname" : r3_firstname, "lastname" : r3_lastname, }, user4={ "firstname" : r4_firstname, "lastname" : r4_lastname, }, teamListLink=UI_TEMPLATE_TEAM_LIST_LINK, baseUrl=UI_TEMPLATE_BASE_URL) else: reason = "Something went wrong. Make sure you check the team before submitted." return render_template("result.html", success=success, reason=reason, teamListLink=UI_TEMPLATE_TEAM_LIST_LINK, baseUrl=UI_TEMPLATE_BASE_URL)