-
Notifications
You must be signed in to change notification settings - Fork 84
/
app.py
136 lines (109 loc) · 4.43 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
from flask import Flask, render_template, request, url_for, redirect, jsonify
import mysql.connector
from mysql.connector import Error
from flask_cors import CORS
import os
import uuid # Import for unique filenames
from web3 import Web3
import logging # Import logging for error handling
# Initialize the app and CORS
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
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
# Database and Blockchain configuration from environment variables
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') # Recommend using a secure secrets manager
}
blockchain_url = os.environ.get('BLOCKCHAIN_URL', 'http://127.0.0.1:8545')
web3 = Web3(Web3.HTTPProvider(blockchain_url))
contract_address = os.environ.get('CONTRACT_ADDRESS')
contract_abi = [...] # Contract ABI array
contract = web3.eth.contract(address=contract_address, abi=contract_abi)
# Logging configuration
logging.basicConfig(level=logging.INFO)
# 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:
logging.error(f"Database connection error: {e}")
return None
# 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
# Handle file upload securely
profile_pic = request.files.get('profile')
filename = None
if profile_pic:
filename = f"{uuid.uuid4()}_{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:
return jsonify({"error": "Database connection failed"}), 500
with connection.cursor() as cursor:
add_registration_proc = "CALL add_registration(%s, %s, %s, %s, %s, %s, %s)"
cursor.execute(add_registration_proc, (roll, fullname, email, phno, stream, event, filename))
connection.commit()
return redirect(url_for('success'))
except Error as e:
logging.error(f"Database error: {e}")
return jsonify({"error": "Database error occurred."}), 500
except Exception as e:
logging.error(f"General error: {e}")
return jsonify({"error": str(e)}), 500
@app.route('/see_details.html')
def see_details():
connection = create_connection()
rows = []
event_filter = request.args.get('event')
search_query = request.args.get('search')
if connection:
try:
with connection.cursor() as cursor:
# Secure parameterized query
query = "SELECT * FROM registrations WHERE event = %s" if event_filter else "SELECT * FROM registrations"
params = [event_filter] if event_filter else []
if search_query:
query += " AND fullname LIKE %s"
params.append(f"%{search_query}%")
cursor.execute(query, params)
rows = cursor.fetchall()
except Error as e:
logging.error(f"Error fetching details: {e}")
return jsonify({"error": "An error occurred while fetching details."}), 500
finally:
connection.close()
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)