missing api features
This commit is contained in:
112
app.py
112
app.py
@@ -257,7 +257,7 @@ def start_timer_bg(activity_id):
|
||||
'time_entry_id': new_entry_id,
|
||||
'status': 'open',
|
||||
'is_template': False,
|
||||
'source': 'auto', # Mark as auto-generated
|
||||
'source': 'auto',
|
||||
'created_at': datetime.now(),
|
||||
'comments': []
|
||||
})
|
||||
@@ -886,6 +886,116 @@ def api_status(user_id):
|
||||
} for a in activities]
|
||||
})
|
||||
|
||||
# --- API Timer Controls (Token Based) ---
|
||||
|
||||
@app.route('/api/timer/start/<activity_id>', methods=['POST'])
|
||||
@token_required
|
||||
def api_start_timer(user_id, activity_id):
|
||||
# Stop ANY running timer
|
||||
db.time_entries.update_many(
|
||||
{'user_id': user_id, 'end_time': None},
|
||||
{'$set': {'end_time': datetime.now()}}
|
||||
)
|
||||
|
||||
# Start new
|
||||
start_time = datetime.now()
|
||||
new_entry_id = db.time_entries.insert_one({
|
||||
'user_id': user_id,
|
||||
'activity_id': ObjectId(activity_id),
|
||||
'start_time': start_time,
|
||||
'end_time': None,
|
||||
'note': '',
|
||||
'subcategory': ''
|
||||
}).inserted_id
|
||||
|
||||
# Auto-add templates
|
||||
templates = db.tasks.find({
|
||||
'user_id': user_id,
|
||||
'activity_id': ObjectId(activity_id),
|
||||
'is_template': True
|
||||
})
|
||||
|
||||
for t in templates:
|
||||
db.tasks.insert_one({
|
||||
'user_id': user_id,
|
||||
'name': t['name'],
|
||||
'activity_id': ObjectId(activity_id),
|
||||
'time_entry_id': new_entry_id,
|
||||
'status': 'open',
|
||||
'is_template': False,
|
||||
'source': 'auto',
|
||||
'created_at': datetime.now(),
|
||||
'comments': []
|
||||
})
|
||||
|
||||
activity = db.activities.find_one({'_id': ObjectId(activity_id)})
|
||||
|
||||
return jsonify({
|
||||
'status': 'success',
|
||||
'entry_id': str(new_entry_id),
|
||||
'start_time': start_time.isoformat(),
|
||||
'activity_name': activity['name'],
|
||||
'activity_color': activity.get('color', '#3498db')
|
||||
})
|
||||
|
||||
@app.route('/api/timer/stop', methods=['POST'])
|
||||
@token_required
|
||||
def api_stop_timer(user_id):
|
||||
result = db.time_entries.update_many(
|
||||
{'user_id': user_id, 'end_time': None},
|
||||
{'$set': {'end_time': datetime.now()}}
|
||||
)
|
||||
return jsonify({'status': 'stopped', 'modified': result.modified_count})
|
||||
|
||||
@app.route('/api/timer/update', methods=['POST'])
|
||||
@token_required
|
||||
def api_update_active_entry(user_id):
|
||||
data = request.get_json()
|
||||
fields = {}
|
||||
if 'note' in data: fields['note'] = data['note']
|
||||
if 'subcategory' in data: fields['subcategory'] = data['subcategory']
|
||||
|
||||
if not fields:
|
||||
return jsonify({'message': 'No fields to update'}), 400
|
||||
|
||||
db.time_entries.update_one(
|
||||
{'user_id': user_id, 'end_time': None},
|
||||
{'$set': fields}
|
||||
)
|
||||
return jsonify({'status': 'updated'})
|
||||
|
||||
@app.route('/api/tasks/toggle', methods=['POST'])
|
||||
@token_required
|
||||
def api_toggle_task(user_id):
|
||||
data = request.get_json()
|
||||
task_id = data.get('task_id')
|
||||
is_checked = data.get('is_checked')
|
||||
|
||||
if not task_id: return jsonify({'message': 'task_id required'}), 400
|
||||
|
||||
status = 'completed' if is_checked else 'open'
|
||||
completed_at = datetime.now() if is_checked else None
|
||||
|
||||
update_doc = {
|
||||
'status': status,
|
||||
'completed_at': completed_at
|
||||
}
|
||||
|
||||
# Helper: Link to active timer if checking off
|
||||
if is_checked:
|
||||
current_entry = db.time_entries.find_one({
|
||||
'user_id': user_id,
|
||||
'end_time': None
|
||||
})
|
||||
if current_entry:
|
||||
update_doc['time_entry_id'] = current_entry['_id']
|
||||
|
||||
db.tasks.update_one(
|
||||
{'_id': ObjectId(task_id), 'user_id': user_id},
|
||||
{'$set': update_doc}
|
||||
)
|
||||
return jsonify({'status': 'success'})
|
||||
|
||||
# --- Activity Management API ---
|
||||
|
||||
@app.route('/api/activities/create', methods=['POST'])
|
||||
|
||||
Reference in New Issue
Block a user