Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
from flask_cors import CORS
|
| 3 |
+
import json
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
app = Flask(__name__)
|
| 8 |
+
CORS(app)
|
| 9 |
+
|
| 10 |
+
# File paths for data storage
|
| 11 |
+
DATA_DIR = "data"
|
| 12 |
+
SCHEDULE_FILE = os.path.join(DATA_DIR, "schedule.json")
|
| 13 |
+
ACTIVITY_LOG_FILE = os.path.join(DATA_DIR, "activity_logs.json")
|
| 14 |
+
COMPLETION_FILE = os.path.join(DATA_DIR, "task_completion.json")
|
| 15 |
+
|
| 16 |
+
# Create data directory if it doesn't exist
|
| 17 |
+
os.makedirs(DATA_DIR, exist_ok=True)
|
| 18 |
+
|
| 19 |
+
def load_json_file(filepath, default=None):
|
| 20 |
+
if os.path.exists(filepath):
|
| 21 |
+
with open(filepath, 'r') as f:
|
| 22 |
+
return json.load(f)
|
| 23 |
+
return default if default is not None else {}
|
| 24 |
+
|
| 25 |
+
def save_json_file(filepath, data):
|
| 26 |
+
with open(filepath, 'w') as f:
|
| 27 |
+
json.dump(data, f, indent=2)
|
| 28 |
+
|
| 29 |
+
# Initialize data files
|
| 30 |
+
if not os.path.exists(SCHEDULE_FILE):
|
| 31 |
+
save_json_file(SCHEDULE_FILE, {})
|
| 32 |
+
if not os.path.exists(ACTIVITY_LOG_FILE):
|
| 33 |
+
save_json_file(ACTIVITY_LOG_FILE, {})
|
| 34 |
+
if not os.path.exists(COMPLETION_FILE):
|
| 35 |
+
save_json_file(COMPLETION_FILE, {})
|
| 36 |
+
|
| 37 |
+
@app.route('/api/schedule', methods=['GET', 'POST'])
|
| 38 |
+
def handle_schedule():
|
| 39 |
+
if request.method == 'GET':
|
| 40 |
+
schedule = load_json_file(SCHEDULE_FILE)
|
| 41 |
+
return jsonify(schedule)
|
| 42 |
+
else:
|
| 43 |
+
schedule = request.json
|
| 44 |
+
save_json_file(SCHEDULE_FILE, schedule)
|
| 45 |
+
return jsonify({"message": "Schedule saved successfully"})
|
| 46 |
+
|
| 47 |
+
@app.route('/api/activity-log', methods=['GET', 'POST'])
|
| 48 |
+
def handle_activity_log():
|
| 49 |
+
if request.method == 'GET':
|
| 50 |
+
date = request.args.get('date')
|
| 51 |
+
logs = load_json_file(ACTIVITY_LOG_FILE)
|
| 52 |
+
if date:
|
| 53 |
+
return jsonify(logs.get(date, []))
|
| 54 |
+
return jsonify(logs)
|
| 55 |
+
else:
|
| 56 |
+
date = request.json.get('date')
|
| 57 |
+
entries = request.json.get('entries', [])
|
| 58 |
+
logs = load_json_file(ACTIVITY_LOG_FILE)
|
| 59 |
+
logs[date] = entries
|
| 60 |
+
save_json_file(ACTIVITY_LOG_FILE, logs)
|
| 61 |
+
return jsonify({"message": "Activity log saved successfully"})
|
| 62 |
+
|
| 63 |
+
@app.route('/api/task-completion', methods=['GET', 'POST'])
|
| 64 |
+
def handle_task_completion():
|
| 65 |
+
if request.method == 'GET':
|
| 66 |
+
date = request.args.get('date')
|
| 67 |
+
completion_data = load_json_file(COMPLETION_FILE)
|
| 68 |
+
if date:
|
| 69 |
+
return jsonify(completion_data.get(date, {}))
|
| 70 |
+
return jsonify(completion_data)
|
| 71 |
+
else:
|
| 72 |
+
date = request.json.get('date')
|
| 73 |
+
task_id = request.json.get('taskId')
|
| 74 |
+
is_completed = request.json.get('isCompleted')
|
| 75 |
+
completion_data = load_json_file(COMPLETION_FILE)
|
| 76 |
+
if date not in completion_data:
|
| 77 |
+
completion_data[date] = {}
|
| 78 |
+
completion_data[date][task_id] = is_completed
|
| 79 |
+
save_json_file(COMPLETION_FILE, completion_data)
|
| 80 |
+
return jsonify({"message": "Task completion status updated successfully"})
|
| 81 |
+
|
| 82 |
+
@app.route('/api/export', methods=['GET'])
|
| 83 |
+
def export_data():
|
| 84 |
+
schedule = load_json_file(SCHEDULE_FILE)
|
| 85 |
+
activity_logs = load_json_file(ACTIVITY_LOG_FILE)
|
| 86 |
+
completion_data = load_json_file(COMPLETION_FILE)
|
| 87 |
+
|
| 88 |
+
export_data = {
|
| 89 |
+
"schedule": schedule,
|
| 90 |
+
"activityLogArchive": activity_logs,
|
| 91 |
+
"taskCompletion": completion_data
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
return jsonify(export_data)
|
| 95 |
+
|
| 96 |
+
@app.route('/api/import', methods=['POST'])
|
| 97 |
+
def import_data():
|
| 98 |
+
data = request.json
|
| 99 |
+
|
| 100 |
+
if "schedule" in data:
|
| 101 |
+
save_json_file(SCHEDULE_FILE, data["schedule"])
|
| 102 |
+
if "activityLogArchive" in data:
|
| 103 |
+
save_json_file(ACTIVITY_LOG_FILE, data["activityLogArchive"])
|
| 104 |
+
if "taskCompletion" in data:
|
| 105 |
+
save_json_file(COMPLETION_FILE, data["taskCompletion"])
|
| 106 |
+
|
| 107 |
+
return jsonify({"message": "Data imported successfully"})
|
| 108 |
+
|
| 109 |
+
if __name__ == '__main__':
|
| 110 |
+
app.run(debug=True, host='0.0.0.0', port=7860)
|