Mehrere an einem Tag
This commit is contained in:
84
app.py
84
app.py
@@ -29,16 +29,34 @@ def get_habits():
|
||||
last_30_days = get_last_30_days()
|
||||
|
||||
for habit_doc in raw_habits:
|
||||
# Handle migration from old format (list) to new format (dict)
|
||||
completed_dates = habit_doc.get('completed_dates', {})
|
||||
if isinstance(completed_dates, list):
|
||||
# Convert old format to new format
|
||||
new_completed_dates = {}
|
||||
for date in completed_dates:
|
||||
new_completed_dates[date] = 1
|
||||
completed_dates = new_completed_dates
|
||||
# Update in database
|
||||
db.update({'completed_dates': completed_dates}, doc_ids=[habit_doc.doc_id])
|
||||
|
||||
current_habit_data = {
|
||||
'id': habit_doc.doc_id,
|
||||
'name': habit_doc.get('name'),
|
||||
'completed_dates': habit_doc.get('completed_dates', []),
|
||||
'color': habit_doc.get('color', '#4CAF50')
|
||||
'completed_dates': completed_dates,
|
||||
'color': habit_doc.get('color', '#4CAF50'),
|
||||
'daily_target': habit_doc.get('daily_target', 1)
|
||||
}
|
||||
|
||||
completion_history = {}
|
||||
for date_str in last_30_days:
|
||||
completion_history[date_str] = date_str in current_habit_data['completed_dates']
|
||||
completed_count = completed_dates.get(date_str, 0)
|
||||
target_count = current_habit_data['daily_target']
|
||||
completion_history[date_str] = {
|
||||
'completed': completed_count,
|
||||
'target': target_count,
|
||||
'is_complete': completed_count >= target_count
|
||||
}
|
||||
|
||||
current_habit_data['completion_history'] = completion_history
|
||||
formatted_habits.append(current_habit_data)
|
||||
@@ -50,11 +68,12 @@ def add_habit():
|
||||
data = request.json
|
||||
name = data.get('name')
|
||||
color = data.get('color', '#4CAF50') # Default green color
|
||||
daily_target = data.get('daily_target', 1) # Default 1x per day
|
||||
if not name:
|
||||
return jsonify({'error': 'Habit name is required'}), 400
|
||||
|
||||
habit_id = db.insert({'name': name, 'completed_dates': [], 'color': color})
|
||||
return jsonify({'id': habit_id, 'name': name, 'completed_dates': [], 'color': color}), 201
|
||||
habit_id = db.insert({'name': name, 'completed_dates': {}, 'color': color, 'daily_target': daily_target})
|
||||
return jsonify({'id': habit_id, 'name': name, 'completed_dates': {}, 'color': color, 'daily_target': daily_target}), 201
|
||||
|
||||
@app.route('/habits/<int:habit_id>/complete', methods=['POST'])
|
||||
def complete_habit(habit_id):
|
||||
@@ -64,11 +83,25 @@ def complete_habit(habit_id):
|
||||
if not habit:
|
||||
return jsonify({'error': 'Habit not found'}), 404
|
||||
|
||||
if date_to_complete not in habit['completed_dates']:
|
||||
habit['completed_dates'].append(date_to_complete)
|
||||
db.update({'completed_dates': habit['completed_dates']}, doc_ids=[habit_id])
|
||||
# Handle migration from old format (list) to new format (dict)
|
||||
completed_dates = habit.get('completed_dates', {})
|
||||
if isinstance(completed_dates, list):
|
||||
new_completed_dates = {}
|
||||
for date in completed_dates:
|
||||
new_completed_dates[date] = 1
|
||||
completed_dates = new_completed_dates
|
||||
|
||||
return jsonify({'message': f'Habit {habit_id} marked as completed for {date_to_complete}', 'completed_dates': habit['completed_dates']})
|
||||
# Increment completion count for the date
|
||||
current_count = completed_dates.get(date_to_complete, 0)
|
||||
completed_dates[date_to_complete] = current_count + 1
|
||||
|
||||
db.update({'completed_dates': completed_dates}, doc_ids=[habit_id])
|
||||
|
||||
return jsonify({
|
||||
'message': f'Habit {habit_id} marked as completed for {date_to_complete}',
|
||||
'completed_dates': completed_dates,
|
||||
'current_count': completed_dates[date_to_complete]
|
||||
})
|
||||
|
||||
@app.route('/habits/<int:habit_id>/uncomplete', methods=['POST'])
|
||||
def uncomplete_habit(habit_id):
|
||||
@@ -78,20 +111,39 @@ def uncomplete_habit(habit_id):
|
||||
if not habit:
|
||||
return jsonify({'error': 'Habit not found'}), 404
|
||||
|
||||
if date_to_uncomplete in habit['completed_dates']:
|
||||
habit['completed_dates'].remove(date_to_uncomplete)
|
||||
db.update({'completed_dates': habit['completed_dates']}, doc_ids=[habit_id])
|
||||
# Handle migration from old format (list) to new format (dict)
|
||||
completed_dates = habit.get('completed_dates', {})
|
||||
if isinstance(completed_dates, list):
|
||||
new_completed_dates = {}
|
||||
for date in completed_dates:
|
||||
new_completed_dates[date] = 1
|
||||
completed_dates = new_completed_dates
|
||||
|
||||
return jsonify({'message': f'Habit {habit_id} marked as uncompleted for {date_to_uncomplete}', 'completed_dates': habit['completed_dates']})
|
||||
# Decrement completion count for the date
|
||||
if date_to_uncomplete in completed_dates:
|
||||
current_count = completed_dates[date_to_uncomplete]
|
||||
if current_count > 1:
|
||||
completed_dates[date_to_uncomplete] = current_count - 1
|
||||
else:
|
||||
del completed_dates[date_to_uncomplete]
|
||||
|
||||
db.update({'completed_dates': completed_dates}, doc_ids=[habit_id])
|
||||
|
||||
return jsonify({
|
||||
'message': f'Habit {habit_id} marked as uncompleted for {date_to_uncomplete}',
|
||||
'completed_dates': completed_dates,
|
||||
'current_count': completed_dates.get(date_to_uncomplete, 0)
|
||||
})
|
||||
|
||||
@app.route('/habits/<int:habit_id>', methods=['PUT'])
|
||||
def update_habit(habit_id):
|
||||
data = request.json
|
||||
name = data.get('name')
|
||||
color = data.get('color')
|
||||
daily_target = data.get('daily_target')
|
||||
|
||||
if not name and not color:
|
||||
return jsonify({'error': 'Habit name or color is required'}), 400
|
||||
if not name and not color and not daily_target:
|
||||
return jsonify({'error': 'Habit name, color, or daily target is required'}), 400
|
||||
|
||||
habit = db.get(doc_id=habit_id)
|
||||
if not habit:
|
||||
@@ -102,6 +154,8 @@ def update_habit(habit_id):
|
||||
update_data['name'] = name
|
||||
if color:
|
||||
update_data['color'] = color
|
||||
if daily_target:
|
||||
update_data['daily_target'] = int(daily_target)
|
||||
|
||||
db.update(update_data, doc_ids=[habit_id])
|
||||
return jsonify({'message': f'Habit {habit_id} updated', **update_data})
|
||||
|
||||
Reference in New Issue
Block a user