-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
101 lines (81 loc) · 3.1 KB
/
app.py
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import csv
import os
import gspread
from dotenv import load_dotenv
from flask import Flask, render_template, request
app = Flask(__name__)
# Google Sheets API
def add_values_to_gsheet(
spreadsheet_id: str,
row: list,
index: int = 2,
):
gc = gspread.service_account(filename="./credentials.json")
spreadsheet = gc.open_by_key(spreadsheet_id)
sheet_in_spreadsheet = spreadsheet.get_worksheet(0)
sheet_in_spreadsheet.insert_row(values=row, index=index)
def write_to_gsheet(data: dict):
row = [data["Name"], data["Regno"], data["Email"], data["Phone"], data["Message"]]
add_values_to_gsheet(
spreadsheet_id="1M2d92D9k2dCXptgV-BPbAQWH2sMIkmhgNCqZDIO7ZB4", row=row
)
# CSV
def check_if_exists_in_directory(file_or_folder_name: str, directory: str = "") -> bool:
current_working_dir = os.getcwd()
try:
if directory:
os.chdir(directory)
return file_or_folder_name in os.listdir()
except FileNotFoundError:
return False
finally:
os.chdir(current_working_dir)
def write_to_csv(data: dict):
header = ["Name", "Regno", "Email", "Phone", "Message"]
row = [data["Name"], data["Regno"], data["Email"], data["Phone"], data["Message"]]
file_exists = check_if_exists_in_directory("CTFregistrations.csv")
with open("CTFregistrations.csv", "a") as csv_file_obj:
csv_write = csv.writer(csv_file_obj, delimiter=",", lineterminator="\n")
if file_exists:
csv_write.writerow(row)
else:
csv_write.writerow(header)
csv_write.writerow(row)
def check_user_exists_in_csv(data: dict):
if not check_if_exists_in_directory("CTFregistrations.csv"):
return False
else:
with open("CTFregistrations.csv", "r") as csv_file_obj:
csv_reader = csv.DictReader(csv_file_obj)
for row in csv_reader:
if data["Regno"] == row["Regno"]:
return True
return False
@app.route("/", methods=["POST", "GET"])
def data():
data = {}
if request.method == "POST":
data["Name"] = request.form["Name"]
data["Regno"] = request.form["Regno"].upper()
data["Email"] = request.form["Email"]
data["Phone"] = request.form["Phone"]
if "Message" in request.form:
data["Message"] = request.form["Message"]
else:
data["Message"] = ""
if check_user_exists_in_csv(data):
return render_template(
"index.html", filled=True, show_message="You have already registered!"
)
else:
write_to_csv(data)
write_to_gsheet(data)
return render_template(
"index.html",
show_message="You have successfully registered",
filled=True,
)
return render_template("index.html", yet_to_register=True, filled=False)
if __name__ == "__main__":
load_dotenv(".env")
app.run(debug=bool(os.getenv("DEBUG")), host="0.0.0.0", port=80)