1
+ from flask import Blueprint , jsonify , request , abort , redirect
2
+ from sqlalchemy .orm .exc import MultipleResultsFound , NoResultFound
3
+
4
+ from hwut_server .models import Boards , Microcontrollers
5
+ from hwut_server .models .runners import Runners
6
+ from hwut_server .decorators import requires_authentication , requires_superuser
7
+ from hwut_server .utils import dict_list_extended_if_authentication
8
+ from hwut_server .database import db
9
+
10
+ mod = Blueprint ('runners' , __name__ , url_prefix = '/runners' )
11
+
12
+
13
+ @mod .route ('/*' , methods = ['GET' ], defaults = {'filter_type' : 'any' , 'filter1' : None , 'filter2' : None })
14
+ @mod .route ('/<string:filter_type>/<string:filter1>' , methods = ['GET' ], defaults = {'filter2' : None })
15
+ @mod .route ('/<string:filter_type>/<string:filter1>/<string:filter2>' , methods = ['GET' ])
16
+ def runners_list (filter_type , filter1 , filter2 ):
17
+ runner_list = list ()
18
+ if filter_type == 'any' :
19
+ runner_list = Runners .query .all ()
20
+ elif (filter_type == 'by_board' ) and (filter1 is not None ) and (filter2 is None ):
21
+ runner_list = Runners .query .filter (Runners .target_board == filter1 ).all ()
22
+ elif (filter_type == 'by_microcontroller' ) and (filter1 is not None ) and (filter2 is None ):
23
+ runner_list = Runners .query .filter (Runners .target_microcontroller == filter1 ).all ()
24
+ elif (filter_type == 'by_board_and_microcontroller' ) and (filter1 is not None ) and (filter2 is not None ):
25
+ runner_list = Runners .query .filter (Runners .target_microcontroller == filter1 ) \
26
+ .filter (Runners .target_board == filter1 ).all ()
27
+ else :
28
+ abort (400 ) # Bad request
29
+ return jsonify (dict_list_extended_if_authentication (request , runner_list ))
30
+
31
+
32
+ @mod .route ('/<int:id>' , methods = ['GET' ])
33
+ def targets_boards_get (id ):
34
+ try :
35
+ runner = Runners .query .filter (Runners .id == id ).one ()
36
+ if True : # FIXME: if user is owner os superuser
37
+ return jsonify (runner .to_dict_long ())
38
+ else :
39
+ return jsonify (runner .to_dict_short ())
40
+ except :
41
+ abort (404 )
42
+
43
+
44
+ @mod .route ('/add' , methods = ['PUT' ])
45
+ @requires_authentication
46
+ def runner_create ():
47
+ if (request .args .get ('board' ) is None ) or (request .args .get ('microcontroller' ) is None ):
48
+ abort (400 )
49
+
50
+ # FIXME: get user
51
+ user = request .authorization .username
52
+
53
+ try :
54
+ board = Boards .query .filter (Boards .name == request .args .get ('board' )).one ()
55
+ except MultipleResultsFound :
56
+ abort (500 , 'board "{}" exists multiple time in database' .format (request .args .get ('board' )))
57
+ return
58
+ except NoResultFound :
59
+ abort (410 , 'board "{}" does not exist' .format (request .args .get ('board' )))
60
+ return
61
+ try :
62
+ microcontroller = Microcontrollers .query \
63
+ .filter (Microcontrollers .name == request .args .get ('microcontroller' )).one ()
64
+ except MultipleResultsFound :
65
+ abort (500 , 'microcontroller exists multiple time in database' )
66
+ return
67
+ except NoResultFound :
68
+ abort (410 , 'microcontroller does not exist' )
69
+ return
70
+ try :
71
+ runner = Runners (user , board .name , microcontroller .name )
72
+ db .session .add (runner )
73
+ db .session .commit ()
74
+ except :
75
+ abort (500 , 'unable to create runner' )
76
+ return
77
+ return redirect (mod .url_prefix + '/' + str (runner .id ), 201 )
78
+
79
+
80
+ @mod .route ('/<int:id>' , methods = ['DELETE' ])
81
+ @requires_superuser
82
+ def runner_delete (id ):
83
+ try :
84
+ runner = Runners .query .filter (Runners .id == id ).one ()
85
+ db .session .delete (runner )
86
+ db .session .commit ()
87
+ return '' , 204
88
+ except MultipleResultsFound :
89
+ abort (500 , 'runner exists multiple time in database' )
90
+ except NoResultFound :
91
+ abort (410 , 'runner does not exist' )
0 commit comments