from pyramid httpexceptions import HTTPBadRequest from planio lib view

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
from pyramid.httpexceptions import HTTPBadRequest
from planio.lib.views import RESTView
from . import schemes
from planio.serializers import set_serializer_schema, serialize
class Checks(RESTView):
"""Check list view
"""
def get(self):
"""
items := {plans, checklists, tasks}
.. http:get:: /items/:id/checks
Get checks list
:statuscode 200: OK
"""
objs = self.context.list()
return self.serialize(schemes.check_strict, objs)
def post(self):
"""
.. http:post:: /items/:id/checks
Create new check
:statuscode 200: OK
"""
if 'check_id' in self.request.json_body:
raise HTTPBadRequest
data = self.request.json_body.copy()
obj = self.context.create(**data)
return self.serialize(schemes.check_strict, obj)
def serialize(self, schema, objs):
if not isinstance(objs, list):
return Check.serialize(schema, objs)
result = []
for obj in objs:
result.append(Check.serialize(schema, obj))
return result
class Check(RESTView):
"""Check view
"""
def get(self):
"""
items := {plans, checklists, tasks}
.. http:get:: /items/:id/checks/:id
Get check
:statuscode 200: OK
"""
obj = self.context.get()
return Check.serialize(schemes.check_strict, obj)
def delete(self):
"""
.. items/:id/checks/:id
Delete check
:statuscode 200: OK
"""
return self.context.remove()
def patch(self):
"""
.. items/:id/checks/:id
Update check
:statuscode 200: OK
"""
if 'check_id' in self.request.json_body:
raise HTTPBadRequest
check = self.context.update(**self.request.json_body)
return Check.serialize(schemes.check_strict, check)
def put(self):
"""
.. http:put:: items/:id/checks/:id
Resave check
:statuscode 200: OK
"""
if 'check_id' in self.request.json_body:
raise HTTPBadRequest
check = self.context.save(**self.request.json_body)
return Check.serialize(schemes.check_strict, check)
@set_serializer_schema(schemes.check_info)
def start_get(self):
"""
.. http:get:: /items/:id/checks/:id/start
start check
(копируем, заполняя user и id)
Вернётся новый id теста {'id': '8734ad2312515'}.
:statuscode 200: OK
"""
return self.context.start()
@staticmethod
def serialize(schema, obj):
questions = []
for question in obj.questions:
questions.append(question.serialize())
del obj.questions
result = serialize(schema, obj)
result['questions'] = questions
return result
class ChecksInfo(RESTView):
"""Check list view which user started
"""
serializer_schema = [schemes.check_info]
def get(self):
"""
.. http:get:: /checks
Get list of tests which began user
:statuscode 200: OK
"""
return self.context.list()
class CheckInfo(RESTView):
"""view for info about one test
which user started
"""
serializer_schema = schemes.check_info
def get(self):
"""
.. http:get:: /checks/:id
Get check test which began user
:statuscode 200: OK
"""
return self.context.get()
class Question(RESTView):
"""
"""
def get(self):
"""
.. http:get:: /checks/:id/questions/:id
Get question from test
:statuscode 200: OK
"""
return self.context.get().serialize()
def patch(self):
"""
.. /checks/:id/questions/:id
Update question from test
:statuscode 200: OK
"""
question = self.context.update(**self.request.json_body)
return question.serialize()