Ejemplo n.º 1
0
def new_user(request):

    if request.method == 'POST':
        serializer = UserSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
Ejemplo n.º 2
0
def AddNewUser(request):
    if 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)
Ejemplo n.º 3
0
 def list(self, request):
     queryset = User.objects.all()
     serializer = UserSerializer(queryset, many=True)
     return Response({
         'ok': True,
         'members': serializer.data
         })
Ejemplo n.º 4
0
def get_user_details(request):
    if 'user_id' in request.query_params :

        id_provided = request.query_params['user_id']

        if id_provided and len(id_provided) :
            current_user = Users.objects.filter(id=id_provided).first()

            if current_user:
                return Response(UserSerializer(instance=current_user).data, status=200)

        return Response({'error description ': 'No user with id = ' + id_provided + ' was found '}, status=200)

    else:
        all_users = Users.objects.all()
        return Response(UserSerializer(instance=all_users, many=True).data, status=200)
Ejemplo n.º 5
0
def List_All_Users(request):
    if request.method == 'GET':
        user = User.objects.filter(is_inactive=False)  # Get all User Data
        serializer = UserSerializer(user,
                                    many=True)  # Passes user to the serializer
        resp = Response(True, None, serializer.data)
        return JsonResponse(resp.to_json(), safe=False)  # Return Data
Ejemplo n.º 6
0
 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)
Ejemplo n.º 7
0
class PostSerializer(serializers.ModelSerializer):
    author = UserSerializer(read_only=True)
    comments = CommentSerializer(many=True, required=False)

    class Meta:
        model = Post
        fields = [
            'id', 'author', 'text', 'image', 'datetime', 'likes', 'comments'
        ]
Ejemplo n.º 8
0
def user_list(request):

    if request.method == 'GET':

        users = User.objects.all()
        userSerializer = UserSerializer(users, many=True)
        locations = Location.objects.all()
        locationSerializer = LocationSerializer(locations, many=True)

        return Response(locationSerializer.data)

    elif request.method == 'POST':
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 9
0
def List_Single_User(request, uid):
    if request.method == 'GET':
        username = request.GET['un']
        try:
            user = User.objects.get(username=username)  # Get all User Data
            serializer = UserSerializer(
                user, many=False)  # Passes user to the serializer
            resp = Response(True, None, serializer.data)
            return JsonResponse(serializer.data, safe=False)  # Return Data
        except User.DoesNotExist:
            resp = Response(False,
                            'User \'{}\''.format(username) + ' does not exist')
            return JsonResponse(resp.to_json(),
                                status=status.HTTP_404_NOT_FOUND)
    def post(self, request, format=None):
        utility = Utilities()

        email = request.data['email']
        name = request.data['firstname']
        msg = "Hello " + name + "!!\n"
        msg += "Thanks for signing up on Madakoraa."
        print(msg)
        subject = "Welcome to the lifestyle"
        header = "Madakoraa"

        pk = utility.GenerateUserId()
        numberid = str(next(pk))
        username = name + numberid

        serializer = UserSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save(username=username)
            res = utility.sendgridNew(email, name, msg, subject, header)
            print(res)

            data = 'User successfully created'
            return Response(data=data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 11
0
    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()

        return Response(serializer.data, status=status.HTTP_200_OK)
Ejemplo n.º 12
0
def getAllUser(request):
    #method for list all users

    if request.method == 'GET':
        users = User.objects.all()
        serializer = UserSerializer(users, 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)
Ejemplo n.º 13
0
def users_nearby(request):

    if request.method == 'POST':
        users = []
        lat = request.data.get("Latitude")
        long = request.data.get("Longitude")

        for user in User.objects.all():

            dt = math.sqrt((float(lat) - user.Latitude) *
                           (float(lat) - user.Latitude) +
                           (float(long) - user.Longitude) *
                           (float(long) - user.Longitude))
            if dt <= 2:
                users.append(user)

        serializer = UserSerializer(users, many=True)
        return Response(serializer.data)
Ejemplo n.º 14
0
def users_list(request):

    if request.method == 'GET':

        users = User.objects.all()
        serializer = UserSerializer(users, many=True)
        return Response(serializer.data)

    elif request.method == 'POST':

        serializer = UserSerializer(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.º 15
0
def user_create(request):
    min_password_length = 6

    data = request.data
    user_name = None
    password = None
    name = None
    short_bio = ""

    if 'username' in request.data:
        user_name = request.data['username']

    if 'password' in request.data:
        password = request.data['password']

    if 'name' in request.data:
        name = request.data['name']

    if 'short_bio' in request.data:
        short_bio = data['short_bio']

    if name is None or len(name) == 0:
        return Response({'error_description': 'No Name entered'}, status=400)

    elif password is None or len(password) < min_password_length:    # to check if password length is at least 6
        return Response({'error_description': 'Password should have a minimum of 6 letters'}, status=400)

    elif user_name is None or len(user_name) == 0:
        return Response({'error_description': 'No UserName Entered'}, status=400)

    # To check if such a username already exists
    check_valid_username = Users.objects.filter(username=user_name).first()


    if check_valid_username:
        return Response({'error_description': 'Username already exists'}, status=400)

    else:
        new_user = Users.objects.create(name=name, username=user_name, password=make_password(password), short_bio=short_bio)
        new_user.save()
        return Response(UserSerializer(instance=new_user).data, status=200)
Ejemplo n.º 16
0
def user_detail(request, username):
    try:
        user = User.objects.get(username=username)

    except User.DoesNotExist:
        return HttpResponse(status=404)

    if request.method == 'GET':
        serializer = UserSerializer(user)
        return Response(serializer.data)

    elif request.method == 'PUT':
        data = JSONParser().parse(request)
        serializer = UserSerializer(user, data=data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.errors, status=400)

    elif request.method == 'DELETE':
        user.delete()
        return HttpResponse(status=204)
Ejemplo n.º 17
0
def users_id(request, pk):

    try:
        part = User.objects.get(pk=pk)
    except User.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':
        serializer = UserSerializer(part)
        return Response(serializer.data)

    elif request.method == 'PATCH':

        serializer = UserSerializer(part, data=request.data, partial=True)
        if serializer.is_valid():
            serializer.update(part, request.data)
            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    elif request.method == 'DELETE':

        part.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)
Ejemplo n.º 18
0
def getUserDetail(request, pk):
    #method for see user detail

    try:
        user = User.objects.get(pk=pk)
    except User.DoesNotExist:
        return HttpResponse(status=404)

    if request.method == 'GET':
        serializer = UserSerializer(user, many=False)

        return JsonResponse(serializer.data)

    elif request.method == 'PUT':
        data = JSONParser.parse(request)
        serializer = UserSerializer(user, data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data)
        return JsonResponse(serializer.errors, status=400)

    elif request.method == 'DELETE':
        user.delete()
        return HttpResponse(status=204)
Ejemplo n.º 19
0
def Add_User(request):
    if request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = UserSerializer(data=data)
        if serializer.is_valid():
            serializer.validated_data['password'] = hashlib.sha256(
                serializer.validated_data['password'].encode()).hexdigest()
            username = serializer.validated_data['username']
            if valid_username(request, username):
                serializer.save()
                resp = Response(
                    True,
                    'User \'{}\''.format(username) + ' Added succesfully')
                return JsonResponse(resp.to_json(),
                                    status=status.HTTP_201_CREATED)
            resp = Response(
                False, 'Username \'{}\''.format(username) + ' Already taken')
            return JsonResponse(resp.to_json(), status=status.HTTP_200_OK)
        else:
            resp = Response(False, "Incorrect data/invalid format sent to API")
            return JsonResponse(resp.to_json(),
                                status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 20
0
 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)
Ejemplo n.º 21
0
class CommentSerializer(serializers.ModelSerializer):
    author = UserSerializer(read_only=True)

    class Meta:
        model = Comment
        fields = ['id', 'post', 'author', 'text', 'datetime']
Ejemplo n.º 22
0
class MatchSerializer(serializers.Serializer):
    score = serializers.IntegerField()
    user = UserSerializer()
Ejemplo n.º 23
0
 def get(self, request: Request):
     serializer = UserSerializer(instance=request.user)
     return Response(serializer.data, status=status.HTTP_200_OK)