5
5
from api .models import Timelog , TimelogSchema
6
6
from api .config import db
7
7
8
- def read_all ( ):
9
- timelogs = Timelog .query .order_by (Timelog .timelog_name ).all ()
8
+ def read_user_rows ( userid ):
9
+ timelogs = Timelog .query .filter ( Timelog . userid == userid ). order_by (Timelog .timelogid ).all ()
10
10
11
11
timelog_schema = TimelogSchema (many = True )
12
12
data = timelog_schema .dump (timelogs )
13
- return data
13
+ return data , 200
14
14
15
- def read_one (timelogid ):
16
- timelog = Timelog .query .filter (Timelog .timelogid == timelogid ).one_or_none ()
15
+ def read_user_current_row (userid ):
16
+ timelog = (
17
+ Timelog .query
18
+ .filter (Timelog .userid == userid )
19
+ .order_by (Timelog .timelogid .desc ())
20
+ .first ()
21
+ )
17
22
18
23
if timelog is not None :
19
24
timelog_schema = TimelogSchema ()
20
- data = timelog_schema .dump (timelog )
25
+ data = timelog_schema .dump (timelogs )
26
+ return data , 200
21
27
else :
22
28
abort (
23
29
404 ,
24
- "Timelog Record not found for {}" .format (timelogid )
30
+ "Current Timelog Record not found for {}" .format (userid )
25
31
)
26
32
27
- def create (timelog ):
28
- existing_timelog = Timelog .query .filter (Timelog .timelogid == timelogid .get ("timelogid" )).one_or_none ()
33
+ def read_user_rows_daterange (userid , daterange ):
34
+ range_start = daterange .get ("range_start" )
35
+ range_end = daterange .get ("range_end" )
36
+
37
+ timelogs = (
38
+ Timelog .query .filter (
39
+ Timelog .userid == userid
40
+ ).filter (
41
+ Timelog .start < range_end
42
+ ).filter (or_ (
43
+ Timelog .end > range_start ,
44
+ Timelog .end == None
45
+ )
46
+ )
47
+ )
48
+
49
+ if timelogs is not None :
50
+ timelog_schema = TimelogSchema (many = True )
51
+ data = timelog_schema .dump (timelogs )
52
+ return data , 200
53
+ else :
54
+ abort (
55
+ 404 ,
56
+ "Timelogs not found for {userid} between {start} and {end}" .format (
57
+ userid = userid , start = range_start , end = range_end
58
+ )
59
+ )
60
+
61
+ def create (userid , timelog_row ):
62
+ start = timelog_row .get ("start" )
63
+ stop = timelog_row .get ("stop" )
64
+
65
+ existing_timelog = (
66
+ Timelog .query
67
+ .filter (Timelog .userid == userid )
68
+ .filter (Timelog .start == start )
69
+ .filter (Timelog .stop == stop )
70
+ .one_or_none ()
71
+ )
29
72
30
73
if existing_timelog is None :
31
74
schema = TimelogSchema ()
@@ -41,10 +84,12 @@ def create(timelog):
41
84
else :
42
85
abort (
43
86
409 ,
44
- "Timelog {} already exists." .format (timelog .get ("timelogid" ))
87
+ ("Attempting to duplicate Timelog {tlid} start ands top for {userid}."
88
+ .format (tlid = existing_timelog .timelogid , userid = userid )
89
+ )
45
90
)
46
91
47
- def update (timelogid , timelog ):
92
+ def update_row (timelogid , timelog ):
48
93
update_timelog = Timelog .query .filter (Timelog .timelogid == timelogid ).one_or_none ()
49
94
50
95
if update_timelog is None :
@@ -63,3 +108,27 @@ def update(timelogid, timelog):
63
108
64
109
return data , 200
65
110
111
+ def update_user_current_row (userid , timelog ):
112
+ update_timelog = (
113
+ Timelog .query
114
+ .filter (Timelog .userid == userid )
115
+ .order_by (Timelog .timelogid .desc ())
116
+ .first ()
117
+ )
118
+
119
+ if update_timelog is None :
120
+ abort (
121
+ 404 ,
122
+ "Current Timelog Row not found for user {}" .format (userid )
123
+ )
124
+ else :
125
+ schema = TimelogSchema ()
126
+ update = schema .load (timelog , session = db .session )
127
+
128
+ db .session .merge (update )
129
+ db .session .commit ()
130
+
131
+ data = schema .dump (update_timelog )
132
+
133
+ return data , 200
134
+
0 commit comments