def update_user(request): """update user""" try: data = json.loads(request.body) except JSONDecodeError: return Response({"response": "bad json format"}) user = User.objects.get(id=request.user.id) serialisers_user = UserSerializer(instance=user, data=data, partial=True) if serialisers_user.is_valid(raise_exception=True): user = serialisers_user.save() return Response({"response": "Success update user"}) return Response({"response": "bad data", "needs": serialisers_user.errors})
class ColonCancerScreeningSerializer(serializers.ModelSerializer): colon_studies = ColonCancerStudySerializer(many=True, read_only=True) colon_risk_factors = RiskFactorSerializer(many=True, read_only=True) colon_cancer_todos = TodoSerializer(many=True, read_only=True) colon_notes = ColonCancerTextNoteSerializer(many=True, read_only=True) problem = ProblemSerializer() patient = UserSerializer() last_risk_updated_user = SafeUserSerializer() class Meta: model = ColonCancerScreening fields = ( 'id', 'patient', 'problem', 'created_on', 'patient_refused', 'not_appropriate', 'colon_studies', 'risk', 'colon_risk_factors', 'last_risk_updated_date', 'last_risk_updated_user', 'todo_past_five_years', 'colon_cancer_todos', 'patient_refused_on', 'not_appropriate_on', 'colon_notes', )
def get(self, request: Request, pk: int): try: user = User.objects.get(pk=pk) except User.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) serializer = UserSerializer(instance=user) return Response(serializer.data, status=status.HTTP_200_OK)
def get_user(request, id=1): """get user by qs number""" user = User.objects.filter(id=id) serializers_user = UserSerializer(user, many=True) if not serializers_user.data: return Response({"error": "not found"}) return Response({"data": {"user": serializers_user.data[0]}})
def create_user(request): """create user""" data = json.loads(request.body.decode()) data.update({ "likes": 0, "dislikes": 0, "profile_image": ImageFile(open('/home/cuzkov/www.m-skull.ru/users/unnamed.jpg', 'rb')), "status": "" }) serializes_user = UserSerializer(data=data) if (serializes_user.is_valid(raise_exception=True)): if User.objects.last() is not None: buffer_id = User.objects.last().id + 1 else: buffer_id = 1 response = requests.post(url=subscribers_create_user, json={"user": { "id": buffer_id }}) if json.loads(response.text)['response'] == 'success': serializes_user.save() return Response({"response": "Success create user"}) else: return Response({"response": "friend service error, try again"}) if json.loads(response.text)['response'] != 'success': response_str = 'server error, try again' else: response_str = 'bad data' return Response({ "response": response_str, "needs": serializes_user.errors })
def get(self, request): print(request.user) serializer = UserSerializer(instance=request.user) print(serializer.data) return Response(serializer.data, status=status.HTTP_200_OK)
def get_me(request): """get user that auth""" user = User.objects.filter(id=request.user.id) serializers_user = UserSerializer(user, many=True) return Response({"data": {"user": serializers_user.data[0]}})
def get_all_users(request): """ get all users""" users = User.objects.all() serializers_users = UserSerializer(users, many=True) return Response({"data": {"users": serializers_users.data}})
def find_user(request, username=''): """find users by slug""" users = User.objects.filter(username__startswith=username) serializers_users = UserSerializer(users, many=True) return Response({"data": serializers_users.data})