Beispiel #1
0
def register(request):
    """
    Register a new user
    """
    response = {}

    serializer = Serializers.UserSerializer(data=request.POST)

    if serializer.is_valid():
        user = serializer.save()
        userProfileSerializer = Serializers.UserProfileSerializer(
            data=request.POST)
        if userProfileSerializer.is_valid():
            userProfileSerializer.save(user=user)
            response["result"] = result.RESULT_SUCCESS
            response["message"] = message.MESSAGE_REGISTRATION_SUCCESSFUL
            response["error"] = []
            #send a verification email
            send_verification_email(user)
        else:
            response["result"] = result.RESULT_FAILURE
            response["message"] = message.MESSAGE_REGISTRATION_FAILED
            response["error"] = userProfileSerializer.errors
    else:
        response["result"] = result.RESULT_FAILURE
        response["message"] = message.MESSAGE_REGISTRATION_FAILED
        response["error"] = serializer.errors

    return response
 def get(self, request, *args, **kwargs):
     user = lm.RegisteredUser.objects.filter(id=self.kwargs['pk'])
     if user.exists():
         serializer = ls.UserSerializer(user.first())
         return Response(serializer.data)
     else:
         return Response({"Status": "User does not exist."}, status=st.HTTP_400_BAD_REQUEST)
    def post(self, *args, **kwargs):
        data = self.request.data
        user = self.request.user

        updated_user = lm.RegisteredUser.objects.get(id=user.id)
        if "first_name" in data:
            updated_user.first_name = data["first_name"]
        if "last_name" in data:
            updated_user.last_name = data["last_name"]
        if "location" in data:
            updated_user.location = data["location"]
        if "gender" in data:
            updated_user.gender = data["gender"]
        if "advanced_location" in data:

            pl = lm.PointLocation.objects.filter(id=updated_user.advanced_location_id)
            if pl.exists():
                pl.location_name = data["advanced_location"]["location_name"]
                pl.location_coordinate_latitude = data["advanced_location"]["location_coordinate_latitude"]
                pl.location_coordinate_longitude = data["advanced_location"]["location_coordinate_longitude"]
                pl.save()
            else:
                pl = lm.PointLocation(
                    location_name=data["advanced_location"]["location_name"],
                    location_coordinate_latitude=data["advanced_location"]["location_coordinate_latitude"],
                    location_coordinate_longitude=data["advanced_location"]["location_coordinate_longitude"]
                )
                pl.save()
                updated_user.advanced_location = pl
        updated_user.save()

        serializer = ls.UserSerializer(updated_user)
        return Response(serializer.data, status=st.HTTP_200_OK)
Beispiel #4
0
    def post(self, request, *args, **kwargs):
        user = lm.RegisteredUser.objects.filter(id=self.request.user.id)
        if user.exists():
            user = user.first()
        else:
            return Response({"Status": "User does not exist."}, status=HTTP_400_BAD_REQUEST)

        if user.selectedCourses.exists():
            user.selectedCourses.clear()

        for c_id in request.data["courses"]:
            course = cm.Course.objects.filter(id=int(c_id)).first()
            user.selectedCourses.add(course)

        user.save()
        serializer = ls.UserSerializer(user)
        return Response(serializer.data, status=HTTP_200_OK)