3
3
from pyca .config import config
4
4
from pyca .db import Service , ServiceStatus , UpcomingEvent , \
5
5
RecordedEvent , UpstreamState
6
- from pyca .db import get_session , Status , ServiceStates
6
+ from pyca .db import with_session , Status , ServiceStates
7
7
from pyca .ui import app
8
8
from pyca .ui .utils import requires_auth , jsonapi_mediatype
9
9
from pyca .utils import get_service_status , ensurelist
@@ -79,11 +79,11 @@ def internal_state():
79
79
@app .route ('/api/events/' )
80
80
@requires_auth
81
81
@jsonapi_mediatype
82
- def events ():
82
+ @with_session
83
+ def events (db ):
83
84
'''Serve a JSON representation of events splitted by upcoming and already
84
85
recorded events.
85
86
'''
86
- db = get_session ()
87
87
upcoming_events = db .query (UpcomingEvent )\
88
88
.order_by (UpcomingEvent .start )
89
89
recorded_events = db .query (RecordedEvent )\
@@ -97,10 +97,10 @@ def events():
97
97
@app .route ('/api/events/<uid>' )
98
98
@requires_auth
99
99
@jsonapi_mediatype
100
- def event (uid ):
100
+ @with_session
101
+ def event (db , uid ):
101
102
'''Return a specific events JSON
102
103
'''
103
- db = get_session ()
104
104
event = db .query (RecordedEvent ).filter (RecordedEvent .uid == uid ).first () \
105
105
or db .query (UpcomingEvent ).filter (UpcomingEvent .uid == uid ).first ()
106
106
@@ -112,7 +112,8 @@ def event(uid):
112
112
@app .route ('/api/events/<uid>' , methods = ['DELETE' ])
113
113
@requires_auth
114
114
@jsonapi_mediatype
115
- def delete_event (uid ):
115
+ @with_session
116
+ def delete_event (db , uid ):
116
117
'''Delete a specific event identified by its uid. Note that only recorded
117
118
events can be deleted. Events in the buffer for upcoming events are
118
119
regularly replaced anyway and a manual removal could have unpredictable
@@ -124,7 +125,6 @@ def delete_event(uid):
124
125
Returns 404 if event does not exist
125
126
'''
126
127
logger .info ('deleting event %s via api' , uid )
127
- db = get_session ()
128
128
events = db .query (RecordedEvent ).filter (RecordedEvent .uid == uid )
129
129
if not events .count ():
130
130
return make_error_response ('No event with specified uid' , 404 )
@@ -140,7 +140,8 @@ def delete_event(uid):
140
140
@app .route ('/api/events/<uid>' , methods = ['PATCH' ])
141
141
@requires_auth
142
142
@jsonapi_mediatype
143
- def modify_event (uid ):
143
+ @with_session
144
+ def modify_event (db , uid ):
144
145
'''Modify an event specified by its uid. The modifications for the event
145
146
are expected as JSON with the content type correctly set in the request.
146
147
@@ -163,7 +164,6 @@ def modify_event(uid):
163
164
except Exception :
164
165
return make_error_response ('Invalid data' , 400 )
165
166
166
- db = get_session ()
167
167
event = db .query (RecordedEvent ).filter (RecordedEvent .uid == uid ).first ()
168
168
if not event :
169
169
return make_error_response ('No event with specified uid' , 404 )
@@ -177,7 +177,8 @@ def modify_event(uid):
177
177
178
178
@app .route ('/api/metrics' , methods = ['GET' ])
179
179
@requires_auth
180
- def metrics ():
180
+ @with_session
181
+ def metrics (dbs ):
181
182
'''Serve several metrics about the pyCA services and the machine via
182
183
json.'''
183
184
# Get Disk Usage
@@ -189,7 +190,6 @@ def metrics():
189
190
# Get Memory
190
191
memory = psutil .virtual_memory ()
191
192
192
- dbs = get_session ()
193
193
# Get Services
194
194
srvs = dbs .query (ServiceStates )
195
195
services = []
@@ -202,7 +202,6 @@ def metrics():
202
202
state = dbs .query (UpstreamState ).filter (
203
203
UpstreamState .url == config ()['server' ]['url' ]).first ()
204
204
last_synchronized = state .last_synced .isoformat () if state else None
205
- dbs .close ()
206
205
return make_response (
207
206
{'meta' : {
208
207
'services' : services ,
0 commit comments