1
|
|
|
""" |
2
|
|
|
Define the REST verbs relative to the users |
3
|
|
|
""" |
4
|
|
|
|
5
|
|
|
from flask.ext.restful import Resource |
6
|
|
|
from flask.ext.restful.reqparse import Argument |
7
|
|
|
from flask.json import jsonify |
8
|
|
|
|
9
|
|
|
from repositories import UserRepository |
10
|
|
|
from util import parse_params |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
class UserResource(Resource): |
14
|
|
|
""" Verbs relative to the users """ |
15
|
|
|
|
16
|
|
|
@staticmethod |
17
|
|
|
def get(last_name, first_name): |
18
|
|
|
""" |
19
|
|
|
Return an user key information based on his name |
20
|
|
|
--- |
21
|
|
|
tags: |
22
|
|
|
- user |
23
|
|
|
parameters: |
24
|
|
|
- name: last_name |
25
|
|
|
in: path |
26
|
|
|
type: string |
27
|
|
|
description: the last name of the user |
28
|
|
|
- name: first_name |
29
|
|
|
in: path |
30
|
|
|
type: string |
31
|
|
|
description: the last name of the user |
32
|
|
|
responses: |
33
|
|
|
200: |
34
|
|
|
description: The user's information were successfully retrieved |
35
|
|
|
schema: |
36
|
|
|
example: |
37
|
|
|
user: |
38
|
|
|
last_name: Doe |
39
|
|
|
first_name: John |
40
|
|
|
age: 30 |
41
|
|
|
""" |
42
|
|
|
user = UserRepository.get(last_name=last_name, first_name=first_name) |
43
|
|
|
return jsonify({'user': user.json}) |
44
|
|
|
|
45
|
|
|
@staticmethod |
46
|
|
|
@parse_params( |
47
|
|
|
Argument( |
48
|
|
|
'age', |
49
|
|
|
location='json', |
50
|
|
|
required=True, |
51
|
|
|
help='The age of the user.' |
52
|
|
|
), |
53
|
|
|
) |
54
|
|
|
def post(last_name, first_name, age): |
55
|
|
|
""" |
56
|
|
|
Create an user based on the sent information |
57
|
|
|
--- |
58
|
|
|
tags: |
59
|
|
|
- user |
60
|
|
|
parameters: |
61
|
|
|
- name: last_name |
62
|
|
|
in: path |
63
|
|
|
type: string |
64
|
|
|
description: the last name of the user |
65
|
|
|
- name: first_name |
66
|
|
|
in: path |
67
|
|
|
type: string |
68
|
|
|
description: the last name of the user |
69
|
|
|
- name: body |
70
|
|
|
in: body |
71
|
|
|
schema: |
72
|
|
|
type: object |
73
|
|
|
properties: |
74
|
|
|
age: |
75
|
|
|
type: integer |
76
|
|
|
description: The age of the user |
77
|
|
|
responses: |
78
|
|
|
200: |
79
|
|
|
description: The user was successfully created |
80
|
|
|
schema: |
81
|
|
|
example: |
82
|
|
|
user: |
83
|
|
|
last_name: Doe |
84
|
|
|
first_name: John |
85
|
|
|
age: 30 |
86
|
|
|
""" |
87
|
|
|
user = UserRepository.create( |
88
|
|
|
last_name=last_name, |
89
|
|
|
first_name=first_name, |
90
|
|
|
age=age |
91
|
|
|
) |
92
|
|
|
return jsonify({'user': user.json}) |
93
|
|
|
|
94
|
|
|
@staticmethod |
95
|
|
|
@parse_params( |
96
|
|
|
Argument( |
97
|
|
|
'age', |
98
|
|
|
location='json', |
99
|
|
|
required=True, |
100
|
|
|
help='The age of the user.' |
101
|
|
|
), |
102
|
|
|
) |
103
|
|
|
def put(last_name, first_name, age): |
104
|
|
|
""" |
105
|
|
|
Create an user based on the sent information |
106
|
|
|
--- |
107
|
|
|
tags: |
108
|
|
|
- user |
109
|
|
|
parameters: |
110
|
|
|
- name: last_name |
111
|
|
|
in: path |
112
|
|
|
type: string |
113
|
|
|
description: the last name of the user |
114
|
|
|
- name: first_name |
115
|
|
|
in: path |
116
|
|
|
type: string |
117
|
|
|
description: the last name of the user |
118
|
|
|
- name: body |
119
|
|
|
in: body |
120
|
|
|
schema: |
121
|
|
|
type: object |
122
|
|
|
properties: |
123
|
|
|
age: |
124
|
|
|
type: integer |
125
|
|
|
description: The age of the user |
126
|
|
|
responses: |
127
|
|
|
200: |
128
|
|
|
description: The user was successfully created |
129
|
|
|
schema: |
130
|
|
|
example: |
131
|
|
|
user: |
132
|
|
|
last_name: Doe |
133
|
|
|
first_name: John |
134
|
|
|
age: 30 |
135
|
|
|
""" |
136
|
|
|
repository = UserRepository() |
137
|
|
|
user = repository.update( |
138
|
|
|
last_name=last_name, |
139
|
|
|
first_name=first_name, |
140
|
|
|
age=age |
141
|
|
|
) |
142
|
|
|
return jsonify({'user': user.json}) |
143
|
|
|
|