forked from Ramsey99/fest-registration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
158 lines (125 loc) · 4.93 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
from flask import Flask, render_template, request, url_for, redirect, jsonify, send_file
import mysql.connector
from mysql.connector import Error
from flask_cors import CORS
import os
from web3 import Web3 # Import Web3 for Ethereum integration
app = Flask(__name__)
CORS(app)
# Configuration for file uploads
app.config['UPLOAD_FOLDER'] = 'static/uploads/'
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # Max upload size: 16MB
# Ensure the upload folder exists
if not os.path.exists(app.config['UPLOAD_FOLDER']):
os.makedirs(app.config['UPLOAD_FOLDER'])
# MySQL database configuration
db_config = {
'host': os.environ.get('DB_HOST', 'localhost'),
'database': os.environ.get('DB_NAME', 'event_database'),
'user': os.environ.get('DB_USER', 'root'),
'password': os.environ.get('DB_PASSWORD', 'anuradha') # write your own password
}
# Blockchain configuration
blockchain_url = os.environ.get('BLOCKCHAIN_URL', 'http://127.0.0.1:8545') # Change this to your Ethereum node URL
web3 = Web3(Web3.HTTPProvider(blockchain_url))
# Smart contract ABI and address
contract_address = os.environ.get('CONTRACT_ADDRESS', '0xYourContractAddress') # Replace with actual contract address
contract_abi = [
# Add your contract ABI here
]
contract = web3.eth.contract(address=contract_address, abi=contract_abi)
# Function to establish a MySQL connection
def create_connection():
try:
connection = mysql.connector.connect(**db_config)
if connection.is_connected():
return connection
except Error as e:
print(f"Error: {e}")
return None
def close_connection(connection, cursor=None):
if cursor:
cursor.close()
if connection:
connection.close()
# Route to serve the index.html page
@app.route('/')
def index():
return render_template('index.html')
@app.route('/submit', methods=['POST'])
def submit():
try:
# Fetch form data
roll = request.form['roll']
fullname = request.form['fullname']
email = request.form['email']
phno = request.form['phno']
stream = request.form['stream']
event = request.form['event']
# Validate inputs
if not all([roll, fullname, email, phno, stream, event]):
return jsonify({"error": "All fields are required"}), 400
# Fetch file input correctly
profile_pic = request.files.get('profile') # Use get to avoid KeyError
# Check if file is uploaded
if profile_pic:
# Save the file
filename = f"{roll}_{profile_pic.filename}"
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
profile_pic.save(filepath)
# Create a database connection
connection = create_connection()
if connection is None:
raise Exception("Failed to connect to the database") # Handle connection failure
cursor = connection.cursor()
# Call stored procedure to add registration, including the image file name
add_registration_proc = "CALL add_registration(%s, %s, %s, %s, %s, %s, %s)"
data = (roll, fullname, email, phno, stream, event, filename)
cursor.execute(add_registration_proc, data)
connection.commit()
return redirect(url_for('success'))
except Error as e:
print(f"Database error: {e}")
return jsonify({"error": "Database error occurred."}), 500
except Exception as e:
print(f"General error: {e}")
return jsonify({"error": str(e)}), 500
finally:
if cursor:
cursor.close() # Safely close the cursor if it was created
if connection:
connection.close() # Safely close the connection if it was created
@app.route('/see_details.html')
def see_details():
connection = create_connection()
cursor = None
rows = []
event_filter = request.args.get('event')
search_query = request.args.get('search')
if connection:
try:
cursor = connection.cursor()
# Basic query
select_query = "SELECT * FROM registrations WHERE 1=1"
# Event filter
if event_filter:
select_query += f" AND event='{event_filter}'"
# Search filter
if search_query:
select_query += f" AND fullname LIKE '%{search_query}%'"
cursor.execute(select_query)
rows = cursor.fetchall()
except Error as e:
print(f"Error occurred while fetching details: {e}")
return jsonify({"error": "An error occurred while fetching details."}), 500
finally:
close_connection(connection, cursor)
return render_template('see_details.html', rows=rows)
@app.route('/success')
def success():
return render_template('success.html')
@app.route('/index.html', methods=['GET'])
def home():
return redirect(url_for('index'))
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)