1
1
from flask_restful import Resource , abort
2
2
from flask import request , make_response , jsonify
3
- from pbench .server .database .models .metadata import Metadata
3
+ from pbench .server .database .models .metadata import Metadata , MetadataKeys
4
4
from pbench .server .api .auth import Auth
5
5
6
6
@@ -14,58 +14,70 @@ def __init__(self, config, logger, auth):
14
14
self .logger = logger
15
15
self .auth = auth
16
16
17
- @Auth .token_auth .login_required ()
18
- def post (self ):
17
+ @Auth .token_auth .login_required (optional = True )
18
+ def post (self , key ):
19
19
"""
20
20
Post request for creating metadata instance for a user.
21
- This requires a Pbench auth token in the header field
21
+
22
+ The url requires a metadata session key such as /metadata/<key>
23
+ the only keys acceptable to create the metadata sessions are favourite/saved
22
24
23
25
This requires a JSON data with required user metadata fields
24
26
{
25
- "config": "config",
26
- "description": "description",
27
+ "value": "blog text" <Client defined text, can be a string of json>,
27
28
}
29
+ Example: {"value": '{"config": "config", "description": "description"}'}
28
30
29
- Required headers include
31
+ Authorization header can be included as
30
32
Authorization: Bearer <Pbench authentication token (user received upon login)>
33
+ If the authorization header is not present, the created metadata session becomes public by default
31
34
32
35
:return: JSON Payload
33
36
response_object = {
34
37
"message": "success"
35
38
"data" {
36
39
"id": "metadata_id",
37
- "config": "Config string"
38
- "description": "Description string"
40
+ "value": "client text blob",
41
+ "created": "datetime string",
42
+ "updated": "datetime string",
43
+ "key": favorite/saved
39
44
}
40
45
}
41
46
"""
47
+ metadata_key = key .upper ()
48
+ if metadata_key not in [key .name for key in MetadataKeys ]:
49
+ self .logger .warning (
50
+ "Invalid Metadata key provided during metadata creation. Key: {}" ,
51
+ metadata_key ,
52
+ )
53
+ abort (
54
+ 400 ,
55
+ message = "Invalid metadata key. Key need to be either Favorite/Saved" ,
56
+ )
57
+
42
58
post_data = request .get_json ()
43
59
if not post_data :
44
60
self .logger .warning ("Invalid json object: {}" , request .url )
45
61
abort (400 , message = "Invalid json object in request" )
46
62
47
- current_user_id = self .auth .token_auth .current_user ().id
48
-
49
- config = post_data .get ("config" )
50
- if not config :
51
- self .logger .warning (
52
- "Config not provided during metadata creation. user_id: {}" ,
53
- current_user_id ,
54
- )
55
- abort (400 , message = "Config field missing" )
63
+ current_user = self .auth .token_auth .current_user ()
64
+ if current_user :
65
+ current_user_id = current_user .id
66
+ else :
67
+ current_user_id = None
56
68
57
- description = post_data .get ("description " )
58
- if not description :
69
+ value = post_data .get ("value " )
70
+ if not value :
59
71
self .logger .warning (
60
- "Description not provided during metadata creation by user : {}" ,
72
+ "value not provided during metadata creation. user_id : {}" ,
61
73
current_user_id ,
62
74
)
63
- abort (400 , message = "Description field missing" )
75
+ abort (400 , message = "Value field missing" )
64
76
65
77
try :
66
78
# Create a new metadata session
67
79
metadata_session = Metadata (
68
- config = config , description = description , user_id = current_user_id
80
+ value = value , key = metadata_key , user_id = current_user_id
69
81
)
70
82
# insert the metadata session for a user
71
83
metadata_session .add ()
@@ -78,16 +90,12 @@ def post(self):
78
90
else :
79
91
response_object = {
80
92
"message" : "success" ,
81
- "data" : {
82
- "id" : metadata_session .id ,
83
- "config" : metadata_session .config ,
84
- "description" : metadata_session .description ,
85
- },
93
+ "data" : metadata_session .get_json (),
86
94
}
87
95
return make_response (jsonify (response_object ), 201 )
88
96
89
- @Auth .token_auth .login_required ()
90
- def get (self ):
97
+ @Auth .token_auth .login_required (optional = True )
98
+ def get (self , key ):
91
99
"""
92
100
Get request for querying all the metadata sessions for a user.
93
101
returns the list of all the metadata sessions associated with a logged in user.
@@ -98,20 +106,38 @@ def get(self):
98
106
99
107
:return: JSON Payload
100
108
response_object = {
101
- "status ": "success"
109
+ "message ": "success"
102
110
"data": {
103
111
"sessions": [
104
112
{"id": "metadata_id",
105
- "config": "Config string"
106
- "description": "Description string"},
113
+ "value": "client text blob",
114
+ "key": "key string",
115
+ "created": "datetime string",
116
+ "updated": "datetime string", },
107
117
{}]
108
118
}
109
119
}
110
120
"""
111
- current_user_id = self .auth .token_auth .current_user ().id
121
+ if not key :
122
+ self .logger .warning ("Metadata key not provided during metadata query" )
123
+ abort (400 , message = "Missing metadata key in the query url" )
124
+
125
+ current_user = self .auth .token_auth .current_user ()
126
+ if current_user :
127
+ current_user_id = current_user .id
128
+ else :
129
+ current_user_id = None
130
+ print (current_user_id )
112
131
try :
113
- # Fetch the metadata session
114
- metadata_sessions = Metadata .query (user_id = current_user_id )
132
+ # Fetch the metadata sessions
133
+ # If the key is favorite, we return only favorite sessions,
134
+ # else we return all the saved and favorite sessions
135
+ if key .upper () == "FAVORITE" :
136
+ metadata_sessions = Metadata .query (
137
+ user_id = current_user_id , key = MetadataKeys [key .upper ()].value
138
+ )
139
+ else :
140
+ metadata_sessions = Metadata .query (user_id = current_user_id )
115
141
data = [session .get_json () for session in metadata_sessions ]
116
142
except Exception :
117
143
self .logger .exception (
@@ -153,18 +179,20 @@ def get(self, id=None):
153
179
Get request for querying a metadata session of a user given a metadata id.
154
180
This requires a Pbench auth token in the header field
155
181
156
- The url requires a metadata session id such as /user/metadata/<string :id>
182
+ The url requires a metadata session id such as /user/metadata/<int :id>
157
183
158
184
Required headers include
159
185
Authorization: Bearer <Pbench authentication token (user received upon login)>
160
186
161
187
:return: JSON Payload
162
188
response_object = {
163
- "message": "success"
164
- "data" {
189
+ "message": "success",
190
+ "data": {
165
191
"id": "metadata_id",
166
- "config": "Config string"
167
- "description": "Description string"
192
+ "value": "client text blob"
193
+ "created": "Datetime string"
194
+ "updated": "Datetime String"
195
+ "key": "key string"
168
196
}
169
197
}
170
198
"""
@@ -174,7 +202,7 @@ def get(self, id=None):
174
202
175
203
try :
176
204
# Fetch the metadata session
177
- metadata_session = Metadata .query (id = id )
205
+ metadata_session = Metadata .query (id = id )[ 0 ]
178
206
except Exception :
179
207
self .logger .exception (
180
208
"Exception occurred in the GET request while querying the Metadata model, id: {}" ,
@@ -187,11 +215,7 @@ def get(self, id=None):
187
215
188
216
response_object = {
189
217
"message" : "success" ,
190
- "data" : {
191
- "id" : metadata_session .id ,
192
- "config" : metadata_session .config ,
193
- "description" : metadata_session .description ,
194
- },
218
+ "data" : metadata_session .get_json (),
195
219
}
196
220
return make_response (jsonify (response_object ), 200 )
197
221
@@ -201,23 +225,27 @@ def put(self, id=None):
201
225
Put request for updating a metadata session of a user given a metadata id.
202
226
This requires a Pbench auth token in the header field
203
227
204
- The url requires a metadata session id such as /user/metadata/<string :id>
228
+ The url requires a metadata session id such as /user/metadata/<int :id>
205
229
206
230
This requires a JSON data with required user metadata fields to update
207
231
{
208
- "description": "description",
232
+ "value": "new text value",
233
+ ...
209
234
}
235
+ To update the value field, it is required to send the complete text blob again
210
236
211
237
Required headers include
212
238
Authorization: Bearer <Pbench authentication token (user received upon login)>
213
239
214
240
:return: JSON Payload
215
241
response_object = {
216
- "message": "success"
217
- "data" {
242
+ "message": "success",
243
+ "data": {
218
244
"id": "metadata_id",
219
- "config": "Config string"
220
- "description": "Description string"
245
+ "value": "client text blob"
246
+ "created": "Datetime string"
247
+ "updated": "Datetime String"
248
+ "key": "key string"
221
249
}
222
250
}
223
251
"""
@@ -231,7 +259,7 @@ def put(self, id=None):
231
259
abort (400 , message = "Invalid json object in request" )
232
260
233
261
try :
234
- metadata_session = Metadata .query (id = id )
262
+ metadata_session = Metadata .query (id = id )[ 0 ]
235
263
except Exception :
236
264
self .logger .exception (
237
265
"Exception occurred in the PUT request while querying the Metadata model, id: {}" ,
@@ -274,11 +302,7 @@ def put(self, id=None):
274
302
else :
275
303
response_object = {
276
304
"message" : "success" ,
277
- "data" : {
278
- "id" : metadata_session .id ,
279
- "config" : metadata_session .config ,
280
- "description" : metadata_session .description ,
281
- },
305
+ "data" : metadata_session .get_json (),
282
306
}
283
307
return make_response (jsonify (response_object ), 200 )
284
308
@@ -288,7 +312,7 @@ def delete(self, id=None):
288
312
Delete request for deleting a metadata session of a user given a metadata id.
289
313
This requires a Pbench auth token in the header field
290
314
291
- The url requires a metadata session id such as /user/metadata/<string :id>
315
+ The url requires a metadata session id such as /user/metadata/<int :id>
292
316
293
317
Required headers include
294
318
Authorization: Bearer <Pbench authentication token (user received upon login)>
@@ -304,7 +328,7 @@ def delete(self, id=None):
304
328
305
329
try :
306
330
# Fetch the metadata session
307
- metadata_session = Metadata .query (id = id )
331
+ metadata_session = Metadata .query (id = id )[ 0 ]
308
332
except Exception :
309
333
self .logger .exception (
310
334
"Exception occurred in the Delete request while querying the Metadata model, id: {}" ,
0 commit comments