Spaces:
Build error
Build error
Create main.py
Browse files
main.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, render_template, request, jsonify, send_file
|
2 |
+
from flask_sqlalchemy import SQLAlchemy
|
3 |
+
import pandas as pd
|
4 |
+
from datetime import datetime
|
5 |
+
import os
|
6 |
+
|
7 |
+
app = Flask(__name__)
|
8 |
+
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data/tickets.db'
|
9 |
+
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
10 |
+
db = SQLAlchemy(app)
|
11 |
+
|
12 |
+
class Ticket(db.Model):
|
13 |
+
id = db.Column(db.Integer, primary_key=True)
|
14 |
+
email = db.Column(db.String(120), nullable=False)
|
15 |
+
phone = db.Column(db.String(20), nullable=False)
|
16 |
+
name = db.Column(db.String(100), nullable=False)
|
17 |
+
ticket_count = db.Column(db.Integer, nullable=False)
|
18 |
+
ticket_number = db.Column(db.String(50), nullable=False)
|
19 |
+
country = db.Column(db.String(50), nullable=False)
|
20 |
+
region = db.Column(db.String(50), nullable=False)
|
21 |
+
timestamp = db.Column(db.DateTime, default=datetime.utcnow)
|
22 |
+
|
23 |
+
@app.route('/add_data/<email>/<phone>/<name>/<int:ticket_count>/<ticket_number>/<country>/<region>')
|
24 |
+
def add_data(email, phone, name, ticket_count, ticket_number, country, region):
|
25 |
+
try:
|
26 |
+
new_ticket = Ticket(
|
27 |
+
email=email,
|
28 |
+
phone=phone,
|
29 |
+
name=name,
|
30 |
+
ticket_count=ticket_count,
|
31 |
+
ticket_number=ticket_number,
|
32 |
+
country=country,
|
33 |
+
region=region
|
34 |
+
)
|
35 |
+
db.session.add(new_ticket)
|
36 |
+
db.session.commit()
|
37 |
+
return jsonify({"status": "success", "message": "Ticket added successfully"})
|
38 |
+
except Exception as e:
|
39 |
+
return jsonify({"status": "error", "message": str(e)}), 400
|
40 |
+
|
41 |
+
@app.route('/')
|
42 |
+
def dashboard():
|
43 |
+
tickets = Ticket.query.order_by(Ticket.timestamp.desc()).all()
|
44 |
+
return render_template('index.html', tickets=tickets)
|
45 |
+
|
46 |
+
@app.route('/download_excel')
|
47 |
+
def download_excel():
|
48 |
+
try:
|
49 |
+
tickets = Ticket.query.all()
|
50 |
+
data = {
|
51 |
+
"Email": [t.email for t in tickets],
|
52 |
+
"Phone": [t.phone for t in tickets],
|
53 |
+
"Name": [t.name for t in tickets],
|
54 |
+
"Ticket Count": [t.ticket_count for t in tickets],
|
55 |
+
"Ticket Number": [t.ticket_number for t in tickets],
|
56 |
+
"Country": [t.country for t in tickets],
|
57 |
+
"Region": [t.region for t in tickets],
|
58 |
+
"Timestamp": [t.timestamp for t in tickets]
|
59 |
+
}
|
60 |
+
df = pd.DataFrame(data)
|
61 |
+
excel_path = os.path.join('data', 'tickets_export.xlsx')
|
62 |
+
df.to_excel(excel_path, index=False)
|
63 |
+
return send_file(excel_path, as_attachment=True)
|
64 |
+
except Exception as e:
|
65 |
+
return str(e), 500
|
66 |
+
|
67 |
+
if __name__ == '__main__':
|
68 |
+
with app.app_context():
|
69 |
+
db.create_all()
|
70 |
+
app.run(host='0.0.0.0', port=7860, debug=True)
|