-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
64 lines (51 loc) · 1.93 KB
/
Copy pathapp.py
File metadata and controls
64 lines (51 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from flask import Flask, render_template, request, redirect, url_for
import gspread
from oauth2client.service_account import ServiceAccountCredentials
# Initialize the Flask application
app = Flask(__name__)
# --- Google Sheets Integration ---
# Define the scope of the APIs
SCOPE = [
"https://spreadsheets.google.com/feeds",
'https://www.googleapis.com/auth/spreadsheets',
"https://www.googleapis.com/auth/drive.file",
"https://www.googleapis.com/auth/drive"
]
import os
import json
# Get the JSON string from the environment variable
creds_json_str = os.environ.get('GOOGLE_APPLICATION_CREDS_JSON')
# Convert the JSON string to a dictionary
creds_dict = json.loads(creds_json_str)
# Authorize using the dictionary
CREDS = ServiceAccountCredentials.from_json_keyfile_dict(creds_dict, SCOPE)
# Authorize the client
client = gspread.authorize(CREDS)
# Find the spreadsheet by its name and open the first sheet
# Make sure the name matches your Google Sheet's name exactly
sheet = client.open("FOSSHACK v1.0 Registration Form").sheet1
# -------------------------------
@app.route('/')
def index():
return render_template('index.html')
@app.route('/submit', methods=['POST'])
def submit():
# Get form data as a list in the correct order
name = request.form.get('name')
year = request.form.get('year')
semester = request.form.get('semester')
department = request.form.get('department')
section = request.form.get('section')
phone = request.form.get('phone')
# Create a list of values to append to the sheet
new_row = [name, year, semester, department, section, phone]
# Append the new row to the Google Sheet
sheet.append_row(new_row)
# Redirect to a success page
return redirect(url_for('success'))
@app.route('/success')
def success():
return render_template('success.html')
# This part is for local testing. Render will use its own command.
if __name__ == '__main__':
app.run(debug=True)