-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
63 lines (50 loc) · 1.84 KB
/
Copy pathapp.py
File metadata and controls
63 lines (50 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from flask import Flask, render_template, request, redirect, url_for
from io import StringIO
import datetime
import csv
app = Flask(__name__)
class TimesheetEntry:
def __init__(self, task, start_time):
self.task = task
self.start_time = start_time
self.end_time = None
def stop(self):
self.end_time = datetime.datetime.now()
def get_duration(self):
if self.end_time is None:
return None
return self.end_time - self.start_time
timesheet_entries = []
active_tasks = [] # List to store active tasks
@app.route('/')
def index():
return render_template('index.html', active_tasks=active_tasks, timesheet_entries=timesheet_entries)
@app.route('/start', methods=['POST'])
def start_task():
task = request.form['task']
entry = TimesheetEntry(task, datetime.datetime.now())
active_tasks.append(entry) # Add the new task to active tasks
return redirect(url_for('index'))
@app.route('/stop/<int:index>', methods=['POST'])
def stop_task(index):
if index < len(active_tasks):
entry = active_tasks[index]
entry.stop()
timesheet_entries.append(entry) # Move the completed task to timesheet_entries
del active_tasks[index] # Remove the task from active tasks
return redirect(url_for('index'))
@app.route('/exportcsv')
def export_csv():
output = StringIO()
csv_writer = csv.writer(output)
csv_writer.writerow(["Task", "Start Time", "End Time", "Duration"])
for entry in timesheet_entries:
duration = entry.get_duration()
csv_writer.writerow([entry.task, entry.start_time, entry.end_time, duration])
output.seek(0)
return output.getvalue(), 200, {
'Content-Disposition': 'attachment; filename=timesheet.csv',
'Content-Type': 'text/csv'
}
if __name__ == "__main__":
app.run(debug=False)