abdullahalioo commited on
Commit
b680c7a
·
verified ·
1 Parent(s): aec8e3b

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +78 -55
main.py CHANGED
@@ -1,68 +1,91 @@
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():
 
1
  from flask import Flask, render_template, request, jsonify, send_file
2
+ import json
 
 
3
  import os
4
+ from datetime import datetime
5
+ import csv
6
+ from io import StringIO
7
 
8
  app = Flask(__name__)
 
 
 
9
 
10
+ # Ensure data directory exists
11
+ os.makedirs('data', exist_ok=True)
 
 
 
 
 
 
 
 
12
 
13
+ # JSON file path
14
+ DATA_FILE = 'data/tickets.json'
15
+
16
+ def load_data():
17
  try:
18
+ with open(DATA_FILE, 'r') as f:
19
+ return json.load(f)
20
+ except (FileNotFoundError, json.JSONDecodeError):
21
+ return {"tickets": []}
22
+
23
+ def save_data(data):
24
+ with open(DATA_FILE, 'w') as f:
25
+ json.dump(data, f, indent=2)
 
 
 
 
 
 
26
 
27
  @app.route('/')
28
+ def index():
29
+ return render_template('index.html')
 
30
 
31
+ @app.route('/add_data/<email>/<phone>/<name>/<tickets_count>/<ticket_number>/<country>/<region>')
32
+ def add_data(email, phone, name, tickets_count, ticket_number, country, region):
33
+ data = load_data()
34
+
35
+ # Create new ticket entry
36
+ new_ticket = {
37
+ "email": email,
38
+ "phone": phone,
39
+ "name": name,
40
+ "tickets_count": int(tickets_count),
41
+ "ticket_number": ticket_number,
42
+ "country": country,
43
+ "region": region,
44
+ "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
45
+ }
46
+
47
+ data["tickets"].append(new_ticket)
48
+ save_data(data)
49
+
50
+ return jsonify({"status": "success", "message": "Ticket data added successfully"})
51
+
52
+ @app.route('/get_tickets')
53
+ def get_tickets():
54
+ data = load_data()
55
+ return jsonify(data)
56
+
57
+ @app.route('/download_csv')
58
+ def download_csv():
59
+ data = load_data()
60
+
61
+ # Create CSV in memory
62
+ output = StringIO()
63
+ writer = csv.writer(output)
64
+
65
+ # Write header
66
+ writer.writerow(["Email", "Phone", "Name", "Tickets Count", "Ticket Number", "Country", "Region", "Timestamp"])
67
+
68
+ # Write data
69
+ for ticket in data["tickets"]:
70
+ writer.writerow([
71
+ ticket["email"],
72
+ ticket["phone"],
73
+ ticket["name"],
74
+ ticket["tickets_count"],
75
+ ticket["ticket_number"],
76
+ ticket["country"],
77
+ ticket["region"],
78
+ ticket["timestamp"]
79
+ ])
80
+
81
+ output.seek(0)
82
+
83
+ return send_file(
84
+ StringIO(output.getvalue()),
85
+ mimetype="text/csv",
86
+ as_attachment=True,
87
+ download_name="tickets_data.csv"
88
+ )
89
 
90
  if __name__ == '__main__':
91
  with app.app_context():