def make_structure(): connection, cursor = sql_connect() sql = """CREATE TABLE IF NOT EXISTS stadiums ( id SERIAL PRIMARY KEY, name VARCHAR(100) NOT NULL, country VARCHAR(3), total INTEGER )""" execute_sql(sql, connection, cursor)
def stadiums(): connection, cursor = sql_connect() sql = 'SELECT id, name, country, total FROM stadiums' cursor = execute_sql(sql, connection, cursor) data = cursor.fetchall() stadiums = [] for stadium in data: stadiums.append({'id': stadium[0], 'name': stadium[1], 'country': stadium[2], 'total': stadium[3]}) return render_template("stadiums.html", stadiums=stadiums, isAuthenticated = user_is_authenticated())
def add_stadium(): if request.method == 'GET': return render_template('new_stadium.html', isAuthenticated = user_is_authenticated()) data = request.values.to_dict() if not 'name' in data.keys() or not 'country' in data.keys() or not 'total' in data.keys(): error = "Not all fields submitted" return render_template('new_stadium.html', error_msg=error, isAuthenticated = user_is_authenticated()) if not len(data['country']) == 2: return render_template('new_stadium.html', error_msg="Country code length should be 2", isAuthenticated = user_is_authenticated()) try: if int(data['total']) < 1: return render_template('new_stadium.html', error_msg="Capacity should be bigger than 0", isAuthenticated = user_is_authenticated()) except: return render_template('new_stadium.html', error_msg="Capacity should be integer number", isAuthenticated = user_is_authenticated()) connection, cursor = sql_connect() sql = "INSERT INTO stadiums (name, country, total) VALUES ('%s', '%s', %s)" % (data['name'], data['country'], data['total']) cursor = execute_sql(sql, connection, cursor) return redirect('/stadiums')