1
+ from django .shortcuts import render
2
+ from django .http import HttpResponse
3
+ from django .http .response import JsonResponse
4
+ from django .views .decorators .csrf import csrf_exempt
5
+ from rest_framework .parsers import JSONParser
6
+ from rest_framework import status
7
+
8
+ from customers .models import Customer
9
+ from customers .serializers import CustomerSerializer
10
+
11
+ from rest_framework .decorators import api_view
12
+
13
+ @csrf_exempt
14
+ @api_view (['GET' , 'POST' , 'DELETE' ])
15
+ def customer_list (request ):
16
+ if request .method == 'GET' :
17
+ try :
18
+ customers = Customer .objects .all ()
19
+ customers_serializer = CustomerSerializer (customers , many = True )
20
+
21
+ response = {
22
+ 'message' : "Get all Customers'Infos Successfully" ,
23
+ 'customers' : customers_serializer .data ,
24
+ 'error' : ""
25
+ }
26
+ return JsonResponse (response , status = status .HTTP_200_OK );
27
+ except :
28
+ error = {
29
+ 'message' : "Fail! -> can NOT get all the customers List. Please check again!" ,
30
+ 'customers' : "[]" ,
31
+ 'error' : "Error"
32
+ }
33
+ return JsonResponse (error , status = status .HTTP_500_INTERNAL_SERVER_ERROR )
34
+
35
+ elif request .method == 'POST' :
36
+ try :
37
+ customer_data = JSONParser ().parse (request )
38
+ customer_serializer = CustomerSerializer (data = customer_data )
39
+
40
+ if customer_serializer .is_valid ():
41
+ customer_serializer .save ()
42
+ print (customer_serializer .data )
43
+ response = {
44
+ 'message' : "Successfully Upload a Customer with id = %d" % customer_serializer .data .get ('id' ),
45
+ 'customers' : [customer_serializer .data ],
46
+ 'error' : ""
47
+ }
48
+ return JsonResponse (response , status = status .HTTP_201_CREATED )
49
+ else :
50
+ error = {
51
+ 'message' :"Can Not upload successfully!" ,
52
+ 'customers' :"[]" ,
53
+ 'error' : customer_serializer .errors
54
+ }
55
+ return JsonResponse (error , status = status .HTTP_400_BAD_REQUEST )
56
+ except :
57
+ exceptionError = {
58
+ 'message' : "Can Not upload successfully!" ,
59
+ 'customers' : "[]" ,
60
+ 'error' : "Having an exception!"
61
+ }
62
+ return JsonResponse (exceptionError , status = status .HTTP_500_INTERNAL_SERVER_ERROR );
63
+
64
+ elif request .method == 'DELETE' :
65
+ try :
66
+ Customer .objects .all ().delete ()
67
+ return HttpResponse (status = status .HTTP_204_NO_CONTENT )
68
+ except :
69
+ exceptionError = {
70
+ 'message' : "Can Not Deleted successfully!" ,
71
+ 'customers' : "[]" ,
72
+ 'error' : "Having an exception!"
73
+ }
74
+ return JsonResponse (exceptionError , status = status .HTTP_500_INTERNAL_SERVER_ERROR );
75
+
76
+ @csrf_exempt
77
+ @api_view (['GET' , 'PUT' , 'DELETE' ])
78
+ def customer_detail (request , pk ):
79
+ try :
80
+ customer = Customer .objects .get (pk = pk )
81
+ except Customer .DoesNotExist :
82
+ exceptionError = {
83
+ 'message' : "Not found a Customer with id = %s!" % pk ,
84
+ 'customers' : "[]" ,
85
+ 'error' : "404 Code - Not Found!"
86
+ }
87
+ return JsonResponse (exceptionError , status = status .HTTP_404_NOT_FOUND )
88
+
89
+ if request .method == 'GET' :
90
+ customer_serializer = CustomerSerializer (customer )
91
+ response = {
92
+ 'message' : "Successfully get a Customer with id = %s" % pk ,
93
+ 'customers' : [customer_serializer .data ],
94
+ 'error' : ""
95
+ }
96
+ return JsonResponse (response , status = status .HTTP_200_OK );
97
+
98
+ elif request .method == 'PUT' :
99
+ try :
100
+ customer_data = JSONParser ().parse (request )
101
+ customer_serializer = CustomerSerializer (customer , data = customer_data )
102
+
103
+ if customer_serializer .is_valid ():
104
+ customer_serializer .save ()
105
+ response = {
106
+ 'message' : "Successfully Update a Customer with id = %s" % pk ,
107
+ 'customers' : [customer_serializer .data ],
108
+ 'error' : ""
109
+ }
110
+ return JsonResponse (response )
111
+
112
+ response = {
113
+ 'message' : "Fail to Update a Customer with id = %s" % pk ,
114
+ 'customers' : [customer_serializer .data ],
115
+ 'error' : customer_serializer .errors
116
+ }
117
+ return JsonResponse (response , status = status .HTTP_400_BAD_REQUEST )
118
+ except :
119
+ exceptionError = {
120
+ 'message' : "Fail to update a Customer with id = %s!" % pk ,
121
+ 'customers' : [customer_serializer .data ],
122
+ 'error' : "Internal Error!"
123
+ }
124
+ return JsonResponse (exceptionError , status = status .HTTP_500_INTERNAL_SERVER_ERROR )
125
+
126
+ elif request .method == 'DELETE' :
127
+ print ("Deleting a Customer with id=%s" % pk )
128
+ customer .delete ()
129
+ customer_serializer = CustomerSerializer (customer )
130
+ response = {
131
+ 'message' : "Successfully Delete a Customer with id = %s" % pk ,
132
+ 'customers' : [customer_serializer .data ],
133
+ 'error' : ""
134
+ }
135
+ return JsonResponse (response )
136
+
137
+ @csrf_exempt
138
+ @api_view (['GET' ])
139
+ def customer_list_age (request , age ):
140
+ try :
141
+ customers = Customer .objects .filter (age = age )
142
+
143
+ if request .method == 'GET' :
144
+ customers_serializer = CustomerSerializer (customers , many = True )
145
+ response = {
146
+ 'message' : "Successfully filter all Customers with age = %s" % age ,
147
+ 'customers' : customers_serializer .data ,
148
+ 'error' : ""
149
+ }
150
+ return JsonResponse (response , safe = False )
151
+ # In order to serialize objects, we must set 'safe=False'
152
+ except :
153
+ exceptionError = {
154
+ 'message' : "Fail to get a Customer with age = %s" % age ,
155
+ 'customers' : "[]" ,
156
+ 'error' : "Raise an Exception!"
157
+ }
158
+ return JsonResponse (exceptionError , status = status .HTTP_500_INTERNAL_SERVER_ERROR );
0 commit comments