@@ -60,10 +60,10 @@ def perform_create(self, serializer):
60
60
serializer .save (user_id = self .request .user )
61
61
62
62
@action (
63
- detail = True ,
64
63
methods = ["post" ],
65
- permission_classes = [ IsClient ] ,
64
+ detail = True ,
66
65
url_path = "report" ,
66
+ permission_classes = [IsClient ],
67
67
)
68
68
def report_order (self , request , * args , ** kwargs ):
69
69
"""
@@ -90,10 +90,10 @@ def report_order(self, request, *args, **kwargs):
90
90
return Response (serializer .errors , status = status .HTTP_400_BAD_REQUEST )
91
91
92
92
@action (
93
- detail = True ,
94
93
methods = ["patch" ],
95
- permission_classes = [ IsDispatcher ] ,
94
+ detail = True ,
96
95
url_path = "asign_driver" ,
96
+ permission_classes = [IsDispatcher ],
97
97
)
98
98
def assign_driver (self , request , * args , ** kwargs ):
99
99
"""
@@ -123,10 +123,10 @@ def assign_driver(self, request, *args, **kwargs):
123
123
)
124
124
125
125
@action (
126
- detail = True ,
127
- permission_classes = [IsDriver ],
128
126
methods = ["patch" ],
127
+ detail = True ,
129
128
url_path = "accept" ,
129
+ permission_classes = [IsDriver ],
130
130
)
131
131
def accept_order (self , request , * args , ** kwargs ):
132
132
"""
@@ -171,10 +171,10 @@ def accept_order(self, request, *args, **kwargs):
171
171
)
172
172
173
173
@action (
174
+ methods = ["patch" ],
174
175
detail = True ,
175
- methods = ["post" ],
176
- permission_classes = [IsDriver ],
177
176
url_path = "reject" ,
177
+ permission_classes = [IsDriver ],
178
178
)
179
179
def reject_order (self , request , * args , ** kwargs ):
180
180
"""
@@ -205,10 +205,10 @@ def reject_order(self, request, *args, **kwargs):
205
205
)
206
206
207
207
@action (
208
- detail = True ,
209
208
methods = ["patch" ],
210
- permission_classes = [ IsDriver ] ,
209
+ detail = True ,
211
210
url_path = "picked_up" ,
211
+ permission_classes = [IsDriver ],
212
212
)
213
213
def picked_up_order (self , request , * args , ** kwargs ):
214
214
"""
@@ -243,6 +243,51 @@ def picked_up_order(self, request, *args, **kwargs):
243
243
status = status .HTTP_400_BAD_REQUEST ,
244
244
)
245
245
246
+ @action (
247
+ methods = ["post" ],
248
+ detail = True ,
249
+ url_path = "delivered" ,
250
+ permission_classes = [IsDriver ],
251
+ )
252
+ def delivered_order (self , request , * args , ** kwargs ):
253
+ """
254
+ Action to mark a delivery status to delivered.
255
+
256
+ Endpoints:
257
+ - PATCH api/v1/orders/{id}/delivered/
258
+ """
259
+ order = self .get_object ()
260
+ driver = request .user .driver
261
+ signature = request .data .get ("signature" )
262
+
263
+ # The driver is required to submit the user's signature to change the status.
264
+ if not signature :
265
+ return Response (
266
+ {"detail" : "Signature is required to mark the delivery as delivered." },
267
+ status = status .HTTP_400_BAD_REQUEST ,
268
+ )
269
+
270
+ delivery = Delivery .objects .get (
271
+ order_id = order ,
272
+ driver_id = driver ,
273
+ )
274
+
275
+ if delivery .status == StatusChoices .PICKED_UP :
276
+ # TODO: Add verification code
277
+ delivery .signature = signature
278
+ delivery .status = StatusChoices .DELIVERED
279
+ delivery .delivered_at = timezone .now ()
280
+ delivery .is_completed = True
281
+ delivery .save ()
282
+ return Response (
283
+ {"detail" : "Order successfully delivered." },
284
+ status = status .HTTP_200_OK ,
285
+ )
286
+ return Response (
287
+ {"detail" : "The status could not be changed, please try again." },
288
+ status = status .HTTP_400_BAD_REQUEST ,
289
+ )
290
+
246
291
247
292
class OrderItemViewSet (ModelViewSet ):
248
293
"""
0 commit comments