Enable resuming of logged work entry.
This commit is contained in:
30
app.py
30
app.py
@@ -256,5 +256,35 @@ def calculate_work_duration(arrival_time, departure_time, total_break_duration):
|
||||
|
||||
return work_duration, effective_break_duration
|
||||
|
||||
@app.route('/api/resume/<int:entry_id>', methods=['POST'])
|
||||
def resume_entry(entry_id):
|
||||
# Find the entry to resume
|
||||
entry_to_resume = TimeEntry.query.get_or_404(entry_id)
|
||||
|
||||
# Check if there's already an active entry
|
||||
active_entry = TimeEntry.query.filter_by(departure_time=None).first()
|
||||
if active_entry:
|
||||
return jsonify({
|
||||
'success': False,
|
||||
'message': 'Cannot resume this entry. Another session is already active.'
|
||||
}), 400
|
||||
|
||||
# Clear the departure time to make this entry active again
|
||||
entry_to_resume.departure_time = None
|
||||
|
||||
# Reset pause state if it was paused
|
||||
entry_to_resume.is_paused = False
|
||||
entry_to_resume.pause_start_time = None
|
||||
|
||||
db.session.commit()
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'message': 'Work resumed on existing entry',
|
||||
'id': entry_to_resume.id,
|
||||
'arrival_time': entry_to_resume.arrival_time.strftime('%Y-%m-%d %H:%M:%S'),
|
||||
'total_break_duration': entry_to_resume.total_break_duration
|
||||
})
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(debug=True)
|
||||
@@ -133,3 +133,52 @@ document.addEventListener('DOMContentLoaded', function() {
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// Add event listener for resume work buttons
|
||||
document.addEventListener('click', function(e) {
|
||||
if (e.target && e.target.classList.contains('resume-work-btn')) {
|
||||
const entryId = e.target.getAttribute('data-id');
|
||||
resumeWork(entryId);
|
||||
}
|
||||
});
|
||||
|
||||
// Function to resume work
|
||||
function resumeWork(entryId) {
|
||||
fetch(`/api/resume/${entryId}`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
}
|
||||
})
|
||||
.then(response => {
|
||||
if (!response.ok) {
|
||||
return response.json().then(data => {
|
||||
throw new Error(data.message || 'Failed to resume work');
|
||||
});
|
||||
}
|
||||
return response.json();
|
||||
})
|
||||
.then(data => {
|
||||
if (data.success) {
|
||||
// Show a notification
|
||||
const notification = document.createElement('div');
|
||||
notification.className = 'notification';
|
||||
notification.textContent = data.message;
|
||||
document.body.appendChild(notification);
|
||||
|
||||
// Remove notification after 3 seconds
|
||||
setTimeout(() => {
|
||||
notification.remove();
|
||||
}, 3000);
|
||||
|
||||
// Reload the page to show the active session
|
||||
window.location.reload();
|
||||
} else {
|
||||
alert(data.message);
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error:', error);
|
||||
alert(error.message || 'An error occurred while trying to resume work.');
|
||||
});
|
||||
}
|
||||
@@ -65,6 +65,9 @@
|
||||
<td>{{ '%d:%02d:%02d'|format(entry.duration//3600, (entry.duration%3600)//60, entry.duration%60) if entry.duration is not none else 'In progress' }}</td>
|
||||
<td>{{ '%d:%02d:%02d'|format(entry.total_break_duration//3600, (entry.total_break_duration%3600)//60, entry.total_break_duration%60) if entry.total_break_duration is not none else '00:00:00' }}</td>
|
||||
<td>
|
||||
{% if entry.departure_time and not active_entry %}
|
||||
<button class="resume-work-btn" data-id="{{ entry.id }}">Resume Work</button>
|
||||
{% endif %}
|
||||
<button class="edit-entry-btn" data-id="{{ entry.id }}">Edit</button>
|
||||
<button class="delete-entry-btn" data-id="{{ entry.id }}">Delete</button>
|
||||
</td>
|
||||
|
||||
Reference in New Issue
Block a user