Ejemplo n.º 1
0
 def put(self, request, pk=None):
     profile = self.get_object(pk)
     serializer = ProfileSerializer(instance=profile, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response(serializer.errors)
Ejemplo n.º 2
0
 def put(self, request):
     profile = Profile.objects.get(user=self.request.user)
     serializer = ProfileSerializer(instance=profile, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response(serializer.errors)
Ejemplo n.º 3
0
 def put(self, request, username, format=None):
     profile = self.get_object(username)
     serializer = ProfileSerializer(profile, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 4
0
def fetch_profile(request, pk):
    profile = Profile.objects.filter(uuid=pk)
    if not profile.exists():
        return Response({'error': 'Profile doesn\'t exist.'},
                        status=status.HTTP_200_OK)

    if request.method == 'GET':
        serializer = ProfileSerializer(profile.first(),
                                       many=False,
                                       context={'request': request})
        return Response({'profile': serializer.data},
                        status=status.HTTP_200_OK)
    elif request.method == 'PATCH':
        serializer = ProfileSerializer(profile.first(),
                                       data=request.data,
                                       partial=True,
                                       context={'request': request})
        try:
            serializer.is_valid(raise_exception=True)
        except:
            return Response({'error': str(serializer.errors)},
                            status=status.HTTP_400_BAD_REQUEST)
        serializer.save()
        return Response({'profile': serializer.data},
                        status=status.HTTP_200_OK)
Ejemplo n.º 5
0
 def save(self, profile, data=None):
     profile = ProfileSerializer(profile, data=data)
     if profile.is_valid():
         profile.save()
         response, status = profile.data, STATUS_200
     else:
         response, status = profile.errors, STATUS_400
     return response, status
Ejemplo n.º 6
0
def register(request):
    serializer = UserSerializer(data=request.data)
    serializer_profile = ProfileSerializer(data=request.data.get("profile"))
    if serializer.is_valid() and serializer_profile.is_valid():
        serializer.save()
        serializer_profile.save()
        return Response(serializer_profile.data)
    return Response(serializer.errors)
Ejemplo n.º 7
0
    def post(self, request: Request) -> Response:
        serializer = ProfileSerializer(data=request.data)

        if serializer.is_valid():
            serializer.save()

            return Response(serializer.data, status=status.HTTP_201_CREATED)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 8
0
 def post(self, request, format=None):
     image = req['display_image']
     req['user'] = self.request.user.id
     #self.request.user.
     serializer = ProfileSerializer(data=req)
     if serializer.is_valid():
         serializer.save(owner=self.request.user)
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     return Response(serializer.errors, status=status.HTTP_403_FORBIDDEN)
Ejemplo n.º 9
0
    def patch(self, request: Request, pk: int) -> Response:
        profile = get_object_or_404(Profile, pk=pk)

        serializer = ProfileSerializer(profile, data=request.data)

        if serializer.is_valid():
            serializer.save()

            return Response(serializer.data)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 10
0
    def put(self, request, pk, format=None):
        #data= JSONParser().parse(request)
        profile = self.get_object(pk)
        req = request.data
        req['user'] = self.request.user.id
        # self.request.user.

        serializer = ProfileSerializer(profile, data=req)
        if serializer.is_valid():
            serializer.save(owner=self.request.user)
            return Response(serializer.data, status=status.HTTP_200_OK)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 11
0
def profile_view(request):
    user = Userss.objects.get(username=request.data['username'])
    request.data['user'] = user
    serializer = ProfileSerializer(data=request.data)
    if serializer.is_valid():
        serializer.save()
        #print(user.profile)
        #print(serializer.data['picture'])
        j = {}
        j['user'] = serializer.data['user']
        j['picture'] = user.profile.picture.url
        return Response(data=j, status=status.HTTP_201_CREATED)
    else:
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 12
0
 def post(self, request):
     profile = Profile.objects.filter(user=request.user)
     profile.FirstName = request.data.get('firstname')
     profile.LastName = request.data.get('lastname')
     profile.EmailAddress = request.data.get('email')
     profile.Location = request.data.get('location')
     #reget their coordinates if they change
     profile.coordinates = getCoordinates(request.data.get('location'))
     profile.save()
     serializer = ProfileSerializer(profile)
     if not serializer.is_valid():
         return Response({'serializer': serializer, 'user': profile})
     serializer.save()
     return Response({'user': serializer.data})
Ejemplo n.º 13
0
 def put(self, request, provider_id=None, identity_id=None):
     """
     Authentication Required, Update the users profile.
     Returns: 203 - Success, no body.
     400 - Bad key/value on update, errors in body.
     """
     user = request.user
     profile = user.get_profile()
     serializer = ProfileSerializer(profile, data=request.DATA)
     if serializer.is_valid():
         serializer.save()
         response = Response(serializer.data)
         return response
     else:
         return Response(serializer.errors)
Ejemplo n.º 14
0
 def put(self, request, provider_id=None, identity_id=None):
     """
     Update a users profile
     If VALID save the profile
     else raise ValidationError
     """
     user = request.user
     profile = user.get_profile()
     serializer = ProfileSerializer(profile, data=request.DATA)
     if serializer.is_valid():
         serializer.save()
         response = Response(serializer.data)
         return response
     else:
         return Response(serializer.errors)
Ejemplo n.º 15
0
 def put(self, request, provider_id=None, identity_id=None):
     """
     Update a users profile
     If VALID save the profile
     else raise ValidationError
     """
     user = request.user
     profile = user.get_profile()
     serializer = ProfileSerializer(profile, data=request.DATA)
     if serializer.is_valid():
         serializer.save()
         response = Response(serializer.data)
         return response
     else:
         return Response(serializer.errors)
Ejemplo n.º 16
0
 def put(self, request, provider_uuid=None, identity_uuid=None):
     """
     Authentication Required, Update the users profile.
     Returns: 203 - Success, no body.
     400 - Bad key/value on update, errors in body.
     """
     user = request.user
     profile = user.userprofile
     serializer = ProfileSerializer(profile, data=request.data)
     if serializer.is_valid():
         serializer.save()
         response = Response(serializer.data)
         return response
     else:
         return Response(serializer.errors)
Ejemplo n.º 17
0
def get_update_user_profile(request, pk):
    try:
        user = User.objects.get(pk=pk)
        profile = user.profile
    except Profile.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':
        serializer = ProfileSerializer(profile)
        return Response(serializer.data, status=status.HTTP_200_OK)
    elif request.method == 'PUT':
        serializer = ProfileSerializer(instance=profile, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors)
Ejemplo n.º 18
0
def profile_list(request, format=None):
    """
    List all code serie, or create a new serie.
    """
    if request.method == 'GET':
        profiles = Profile.objects.all()
        serializer = ProfileSerializer(profiles, many = True)
        return JSONResponse(serializer.data, status = 200)

    elif request.method == 'POST':



        #data = JSONParser().parse(json_data)
        #Display parameters
        serializer = ProfileSerializer(data=request.data)

        if serializer.is_valid():
            serializer.save()
            return JSONResponse(serializer.data, status=201)
        return JSONResponse(serializer.errors, status=400)
Ejemplo n.º 19
0
def get_author(uuid, host):
    """
    Gets the Author, creats a new one if necesary
    """
    if not uuid or not host:
        return None

    # Get Author
    author = Author.objects.filter(uuid=uuid).first()

    if author is None:
        # New foreign author, create them
        profile_data = get_foreign_profile_data(uuid, get_node(host))
        serializer = ProfileSerializer(data=profile_data, partial=True)

        # try:
        serializer.is_valid(raise_exception=True)
        author = serializer.save()

        # except:
        #     logger.log("Couldn't parse new Author profile for %s from %s." % (uuid, host))
        #     author = None

    elif author.user is None:
        # Existing Foreign Author, update them
        profile_data = get_foreign_profile_data(uuid, get_node(host))
        serializer = ProfileSerializer(author, data=profile_data, partial=True)

        # try:
        serializer.is_valid(raise_exception=True)
        author = serializer.save()

        # except:
            # logger.log("Couldn't parse update data of Author profile for %s from %s." % (uuid, host.host))
            # TBH, we don't care if we can't handle their bad data

    return author
Ejemplo n.º 20
0
def profile_detail(request, pk):
    """
    Retrieve, update or delete a serie.
    """
    try:
        profile = Profile.objects.get(pk=pk)
    except Profile.DoesNotExist:
        return HttpResponse(status=404)

    if request.method == 'GET':
        serializer = ProfileSerializer(profile)
        return JSONResponse(serializer.data)

    elif request.method == 'PUT':
        data = JSONParser().parse(request)
        serializer = ProfileSerializer(viaje, data=data)
        if serializer.is_valid():
            serializer.save()
            return JSONResponse(serializer.data)
        return JSONResponse(serializer.errors, status=400)

    elif request.method == 'DELETE':
        profile.delete()
        return HttpResponse(status=204)
Ejemplo n.º 21
0
 def post(self, request):
     user = get_user_object(request)
     serializer = ProfileSerializer(data=request.data)
     if serializer.is_valid():
         try:
             profile = serializer.save(user=user)
         except IntegrityError:
             raise ValidationError(
                 {'integrity error': 'Duplicate creation attempt'})
         except Exception as e:
             raise ValidationError({'detail': e})
         else:
             user.profiles.add(profile)
             return Response(serializer.data)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 22
0
def profile(request, user_id):
    if request.method == 'GET':
        profile = get_object_or_404(Profile, id=user_id)
        # profile = Profile.objects.get(id=user_id)
        serializer = ProfileSerializer(profile)
        return Response(data=serializer.data, status=status.HTTP_200_OK)

    if request.method == 'PATCH':
        profile = get_object_or_404(Profile, pk=user_id)
        # serializer = ProfileSerializer(profile)
        serializer = ProfileSerializer(profile,
                                       data=request.data,
                                       partial=True)
        if serializer.is_valid():
            profile = serializer.save()
            return Response(ProfileSerializer(profile).data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 23
0
    def patch(self, request, *args, **kwargs):
        """機能
        Profileオブジェクトを更新する
        endpoint: api/profiles/
        """
        """テスト項目
        usernameのみの変更を行う場合usernameの変更実行できる

        """
        print(request.data)
        print(type(request.data))

        serializerContext = {}

        token = getTokenFromHeader(self)
        user_obj = getUserByToken(token)
        profile_obj = Profile.objects.get(user=user_obj)

        # print(request.data)

        # 画像データが含まれている時 -> 画像も含めてProfileオブジェクトを更新する

        # 画像データの送信と文字列データは別々に送信されるのでmultipart通信はありえないことが前提
        if len(request.FILES.keys()) > 0:
            print("TRUEが🐸")
            profile_obj.image = request.FILES["imageProfile"]
            profile_obj.save()

            serializerForSend = ProfileSerializer(
                Profile.objects.get(user=user_obj))
            serializerContext["PROFILE_OBJ"] = serializerForSend.data
            serializerContext["result"] = "success"
            Response(serializerContext)

        serializer = ProfileSerializer(profile_obj,
                                       data=request.data,
                                       partial=True)

        if serializer.is_valid() is False:
            # 入力内容が変わってないとエラーが出るようだ。
            print("serializer.is_valid() : ", serializer.is_valid())
            text = ""
            for ele in serializer.errors:
                print(ele)
                text += ele + " / "

            return Response({"result": "fail", "detail": text})

        # 画像データが含まれていない時 -> Profileオブジェクトを更新する
        if serializer.is_valid():
            print("ここ通ってる is_valid")
            print(request.data)
            # print(serializer)
            # print("serializer.is_valid() : ",serializer.is_valid())

            serializer._validated_data["user_obj_id"] = user_obj.id
            # print(serializer._validated_data)
            if "point" in request.data.keys():
                # aviso.modelsのシグナルを実行するためにmodel.save()を実行する
                serializer.save()
                serializer.instance.save(update_fields=["point"])
            else:
                serializer.save()

            # print(serializer.data)
            serializerForSend = ProfileSerializer(
                Profile.objects.get(user=user_obj))
            serializerContext["PROFILE_OBJ"] = serializerForSend.data
            serializerContext["result"] = "success"
            return Response(serializerContext)