-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
271 lines (236 loc) · 8.26 KB
/
app.py
File metadata and controls
271 lines (236 loc) · 8.26 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import os
from flask import Flask, request, render_template_string
import pyodbc
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
app = Flask(__name__)
# Flask configuration from environment variables
app.config['SECRET_KEY'] = os.getenv('FLASK_SECRET_KEY', 'dev-key-change-in-production')
app.config['DEBUG'] = os.getenv('FLASK_DEBUG', 'False').lower() == 'true'
# Database connection string using environment variables
def get_connection_string():
"""Build database connection string from environment variables"""
driver = os.getenv('DB_DRIVER', 'ODBC Driver 17 for SQL Server')
server = os.getenv('DB_SERVER', 'localhost')
database = os.getenv('DB_NAME', 'Students')
trusted_connection = os.getenv('DB_TRUSTED_CONNECTION', 'yes')
conn_str = f"DRIVER={{{driver}}};SERVER={server};DATABASE={database};"
if trusted_connection.lower() == 'yes':
conn_str += "Trusted_Connection=yes;"
else:
username = os.getenv('DB_USERNAME')
password = os.getenv('DB_PASSWORD')
if username and password:
conn_str += f"UID={username};PWD={password};"
else:
raise ValueError("DB_USERNAME and DB_PASSWORD must be set when not using trusted connection")
return conn_str
# Get connection string
conn_str = get_connection_string()
# Test database connection on startup
def test_connection():
"""Test database connection and print status"""
try:
conn = pyodbc.connect(conn_str)
cursor = conn.cursor()
cursor.execute("SELECT DB_NAME();")
db_name = cursor.fetchone()[0]
print(f"✅ Connected to database: {db_name}")
cursor.close()
conn.close()
return True
except Exception as e:
print(f"❌ Database connection failed: {e}")
return False
# Test connection on startup
test_connection()
# HTML form template
form_html = '''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ app_name }}</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 600px;
margin: 50px auto;
padding: 20px;
background-color: #f5f5f5;
}
.container {
background-color: white;
padding: 30px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
h2 {
color: #333;
text-align: center;
margin-bottom: 30px;
}
.form-group {
margin-bottom: 20px;
}
label {
display: block;
margin-bottom: 5px;
font-weight: bold;
color: #555;
}
input[type="text"], input[type="number"] {
width: 100%;
padding: 10px;
border: 2px solid #ddd;
border-radius: 5px;
font-size: 16px;
box-sizing: border-box;
}
input[type="text"]:focus, input[type="number"]:focus {
border-color: #4CAF50;
outline: none;
}
input[type="submit"] {
background-color: #4CAF50;
color: white;
padding: 12px 30px;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
width: 100%;
}
input[type="submit"]:hover {
background-color: #45a049;
}
.message {
padding: 10px;
margin: 20px 0;
border-radius: 5px;
text-align: center;
}
.success {
background-color: #d4edda;
color: #155724;
border: 1px solid #c3e6cb;
}
.error {
background-color: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
}
</style>
</head>
<body>
<div class="container">
<h2>Enter Student Details</h2>
<form method="POST">
<div class="form-group">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
</div>
<div class="form-group">
<label for="class">Class:</label>
<input type="text" id="class" name="class" required>
</div>
<div class="form-group">
<label for="roll_no">Roll No:</label>
<input type="number" id="roll_no" name="roll_no" min="1" required>
</div>
<div class="form-group">
<label for="subject">Subject:</label>
<input type="text" id="subject" name="subject" required>
</div>
<input type="submit" value="Submit Student Data">
</form>
{% if message %}
<div class="message {{ message_type }}">
{{ message }}
</div>
{% endif %}
</div>
</body>
</html>
'''
def validate_form_data(name, class_, roll_no, subject):
"""Validate form input data"""
errors = []
if not name or not name.strip():
errors.append("Name is required")
elif len(name.strip()) < 2:
errors.append("Name must be at least 2 characters long")
if not class_ or not class_.strip():
errors.append("Class is required")
if not roll_no:
errors.append("Roll number is required")
else:
try:
roll_no_int = int(roll_no)
if roll_no_int <= 0:
errors.append("Roll number must be a positive number")
except ValueError:
errors.append("Roll number must be a valid number")
if not subject or not subject.strip():
errors.append("Subject is required")
return errors
@app.route('/', methods=['GET', 'POST'])
def form():
message = ''
message_type = ''
app_name = os.getenv('APP_NAME', 'Student Form Application')
if request.method == 'POST':
# Get form data
name = request.form.get('name', '').strip()
class_ = request.form.get('class', '').strip()
roll_no = request.form.get('roll_no', '')
subject = request.form.get('subject', '').strip()
# Validate form data
validation_errors = validate_form_data(name, class_, roll_no, subject)
if validation_errors:
message = '; '.join(validation_errors)
message_type = 'error'
else:
try:
# Connect to database and insert data
conn = pyodbc.connect(conn_str)
cursor = conn.cursor()
# Use parameterized query to prevent SQL injection
cursor.execute(
"INSERT INTO dbo.Students_data (name, class, roll_no, subject) VALUES (?, ?, ?, ?)",
name, class_, int(roll_no), subject
)
conn.commit()
cursor.close()
conn.close()
message = f'Student data for {name} submitted successfully!'
message_type = 'success'
except Exception as e:
message = f'Database error: {str(e)}'
message_type = 'error'
print(f"Database error: {e}") # Log error for debugging
return render_template_string(
form_html,
message=message,
message_type=message_type,
app_name=app_name
)
@app.route('/health')
def health_check():
"""Health check endpoint"""
try:
conn = pyodbc.connect(conn_str)
conn.close()
return {"status": "healthy", "database": "connected"}, 200
except Exception as e:
return {"status": "unhealthy", "database": "disconnected", "error": str(e)}, 500
if __name__ == '__main__':
host = os.getenv('FLASK_HOST', '127.0.0.1')
port = int(os.getenv('FLASK_PORT', 5000))
debug = os.getenv('FLASK_DEBUG', 'False').lower() == 'true'
print(f"Starting {os.getenv('APP_NAME', 'Student Form Application')}")
print(f"Server: http://{host}:{port}")
print(f"Debug mode: {debug}")
app.run(host=host, port=port, debug=debug)