Skip to content

Commit 739acfd

Browse files
committed
minor error modified
1 parent b4f9518 commit 739acfd

2 files changed

Lines changed: 15 additions & 8 deletions

File tree

items/tests.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ def setUp(self):
2727
self.headers = {'HTTP_AUTHORIZATION': self.token}
2828
cache.clear()
2929

30+
cache.set('item_list_version', 1)
31+
3032
def tearDown(self):
3133
cache.clear()
3234

items/views.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,10 @@ def patch(self, request, item_id):
136136

137137
if updated_data_exists:
138138
item.save(update_fields=fields_to_save)
139-
cache.incr('item_list_version')
139+
if cache.get('item_list_version') is None:
140+
cache.set('item_list_version', 1)
141+
else:
142+
cache.incr('item_list_version')
140143

141144
return JsonResponse({'MESSAGE': 'UPDATED'}, status=200)
142145

@@ -151,22 +154,24 @@ def patch(self, request, item_id):
151154
except KeyError:
152155
return JsonResponse({'ERROR': 'KEY_ERROR'}, status=400)
153156

154-
@authorization
155157
def delete(self, request, item_id):
156158
try:
157159
item = Item.objects.get(id=item_id)
160+
item.delete()
158161

159-
if item:
160-
item.delete()
162+
try:
163+
cache.incr('item_list_version')
164+
except ValueError:
165+
cache.set('item_list_version', 1)
161166

162-
return JsonResponse({'MESSAGE': 'DELETED'}, status=200)
167+
return JsonResponse({'MESSAGE': 'DELETED'}, status=200)
163168

164-
else:
165-
return JsonResponse({'ERROR': 'ITEM_DOES_NOT_EXIST'}, status=400)
169+
except Item.DoesNotExist:
170+
return JsonResponse({'ERROR': 'ITEM_DOES_NOT_EXIST'}, status=404)
166171

167172
except ValidationError as e:
168173
return JsonResponse({'ERROR': e.message}, status=400)
169-
174+
170175
except KeyError:
171176
return JsonResponse({'ERROR': 'KEY_ERROR'}, status=400)
172177

0 commit comments

Comments
 (0)