def business_area_handler(request, pk):
if request.method == 'POST':
data = json.loads(request.body)
if pk is None:
# New business area
BusinessArea.objects.create(name=data['name'])
return JsonResponse(ReturnStatus(True, 'OK').to_dict())
else:
# existing business area update
try:
ba = BusinessArea.objects.get(pk=pk)
ba.name = data['name']
ba.save()
except BusinessArea.DoesNotExist:
return JsonResponse(ReturnStatus(False, 'Key does not exist').to_dict())
return JsonResponse(ReturnStatus(True, 'OK').to_dict())
elif request.method == 'DELETE':
try:
ba = BusinessArea.objects.get(pk=pk)
if ba.mission_set.all().count() != 0:
return JsonResponse(ReturnStatus(False, 'Business Areas can not be deleted while missions are still associated with them.').to_dict())
ba.delete()
except BusinessArea.DoesNotExist:
return JsonResponse(ReturnStatus(False, 'Key does not exist').to_dict())
return JsonResponse(ReturnStatus(True, 'OK').to_dict())
评论列表
文章目录