def find_user_by_name(request, pk): find_by_surname = False try: item = User.objects.get(name__contains=pk) except User.DoesNotExist: find_by_surname = True if (find_by_surname): try: item = User.objects.get(surname__contains=pk) except User.DoesNotExist: return HttpResponse(status=404) if request.method == 'GET': serializer = UserSerializer(item) return JsonResponse(serializer.data) elif request.method == 'PUT': data = JSONParser().parse(request) serializer = UserSerializer(item, data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data) return JsonResponse(serializer.errors, status=400) elif request.method == 'DELETE': item.delete() return HttpResponse(status=204)
def post(self,request, *args, **kwargs): serializer = UserSerializer(data=request.data) serializer.is_valid(raise_exception=True) serializer.save() user = User.objects.get(email=serializer.data.get('email')) print('this is',user.id, user.email) mail_for_new_user(get_user=user) return Response(serializer.data, status=status.HTTP_201_CREATED)
def put(self, request, *args, **kwargs): serializer_data = request.data.get('user', {}) serializer = UserSerializer(request.user, data=serializer_data, partial=True) serializer.is_valid(raise_exception=True) serializer.save( ) # Обновляет данные о пользователе, если запрос типа PUT return Response(serializer.data, status=status.HTTP_200_OK)
def users_list(request): """ List all code snippets, or create a new snippet. """ if request.method == 'GET': item = User.objects.all() serializer = UserSerializer(item, many=True) return JsonResponse(serializer.data, safe=False) elif request.method == 'POST': data = JSONParser().parse(request) serializer = UserSerializer(data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data, status=201) return JsonResponse(serializer.errors, status=400)
def user_detail(request, pk): try: item = User.objects.get(pk=pk) except User.DoesNotExist: return HttpResponse(status=404) if request.method == 'GET': serializer = UserSerializer(item) return JsonResponse(serializer.data) elif request.method == 'PUT': data = JSONParser().parse(request) serializer = UserSerializer(item, data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data) return JsonResponse(serializer.errors, status=400) elif request.method == 'DELETE': item.delete() return HttpResponse(status=204)
def post(self, request): user = request.data serializer = UserSerializer(data=user) serializer.is_valid(raise_exception=True) serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED)