-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
111 lines (92 loc) · 3.37 KB
/
app.py
File metadata and controls
111 lines (92 loc) · 3.37 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
from flask import Flask, request, jsonify
from flask_cors import CORS
import pandas as pd
from datetime import datetime
import os
import re
app = Flask(__name__)
CORS(app)
EXCEL_FILE = 'registrations.xlsx'
def validate_email(email):
pattern = r'^[\w\.-]+@[\w\.-]+\.\w+$'
return re.match(pattern, email) is not None
def validate_phone(phone):
pattern = r'^\d{10}$'
return re.match(pattern, phone) is not None
def initialize_excel():
if not os.path.exists(EXCEL_FILE):
df = pd.DataFrame(columns=[
'timestamp',
'name',
'email',
'phone',
'age',
'gender',
'weight',
'goal',
'program'
])
df.to_excel(EXCEL_FILE, index=False)
@app.route('/api/register', methods=['POST'])
def register():
try:
data = request.json
# Validate required fields
required_fields = ['name', 'email', 'phone', 'age', 'gender', 'weight', 'goal', 'program']
for field in required_fields:
if field not in data:
return jsonify({'error': f'Missing required field: {field}'}), 400
# Validate email format
if not validate_email(data['email']):
return jsonify({'error': 'Invalid email format'}), 400
# Validate phone number
if not validate_phone(data['phone']):
return jsonify({'error': 'Phone number must be 10 digits'}), 400
# Validate age
try:
age = int(data['age'])
if age < 5 or age > 100:
return jsonify({'error': 'Age must be between 5 and 100'}), 400
except ValueError:
return jsonify({'error': 'Invalid age format'}), 400
# Validate weight
try:
weight = float(data['weight'])
if weight < 20 or weight > 300:
return jsonify({'error': 'Weight must be between 20 and 300 kg'}), 400
except ValueError:
return jsonify({'error': 'Invalid weight format'}), 400
# Create new registration entry
new_registration = {
'timestamp': datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
'name': data['name'],
'email': data['email'],
'phone': data['phone'],
'age': data['age'],
'gender': data['gender'],
'weight': data['weight'],
'goal': data['goal'],
'program': data['program']
}
# Read existing data
try:
df = pd.read_excel(EXCEL_FILE)
except FileNotFoundError:
initialize_excel()
df = pd.read_excel(EXCEL_FILE)
# Check for duplicate email
if data['email'] in df['email'].values:
return jsonify({'error': 'Email already registered'}), 400
# Append new registration
df = pd.concat([df, pd.DataFrame([new_registration])], ignore_index=True)
# Save to Excel
df.to_excel(EXCEL_FILE, index=False)
return jsonify({
'message': 'Registration successful',
'data': new_registration
}), 201
except Exception as e:
return jsonify({'error': str(e)}), 500
if __name__ == '__main__':
initialize_excel()
app.run(debug=True, port=5000)