Beispiel #1
0
def updateUser(request, pk):
    user = User.objects.get(id=pk)
    data = request.data
    if data["name"] != "":
        fullname = data['name'].split(" ")
        if len(fullname) == 1:
            user.first_name = fullname[0]

        if len(fullname) == 2:
            user.first_name = fullname[0]
            user.last_name = fullname[1]

    if data["email"] != "":
        user.username = data["email"]
        user.email = data["email"]

    if data['password'] != '':
        user.password = make_password(str(data["password"]))

    if data["isAdmin"] != "":
        user.is_staff = data["isAdmin"]

    user.save()
    serializer = UserSerializer(user, many=False)

    return Response(serializer.data)
Beispiel #2
0
def createProductReview(request, pk):
    user = request.user
    product = Product.objects.get(_id=pk)
    data = request.data

    # 1 - Review already exists
    #alreadyExists = product.review_set.filter(user=user).exists()
    alreadyExists = False
    if alreadyExists:
        content = {'detail': 'Product already reviewed'}
        return Response(content, status=status.HTTP_400_BAD_REQUEST)

    # 2 - No Rating or 0
    elif data['productPoint'] == 0:
        content = {'detail': 'Please select a rating'}
        return Response(content, status=status.HTTP_400_BAD_REQUEST)

    # 3 - Create review
    else:
        review = Review.objects.create(
            user=user,
            product=product,
            name=user.first_name,
            productPoint=data['productPoint'],
            deliveryPoint=data['deliveryPoint'],
            comment=data['comment'],
        )

        reviews = product.review_set.all()
        product.numReviews = len(reviews)

        total = 0
        for i in reviews:
            total += i.productPoint

        product.productPoint = total / len(reviews)
        

        #4- Go User find all product rating and update farmerPoint
        pUser= product.user
        serializer = UserSerializer(pUser, many=False)
        
        productUser = get_user_model().objects.get(id=pUser.id)
        
        productFarmerPoint = 0
        productFarmerPoint += productUser.farmerPoint
        allPoint = productFarmerPoint * productUser.numReviews 
        productUser.numReviews += 1
        productUser.farmerPoint = (allPoint + data['productPoint']) / productUser.numReviews

        productUser.save()
        product.save()
        

        return Response("Reviewed Succesfully")
Beispiel #3
0
def getFarmersOrders(request):
    user = request.user
    farmerUser = UserSerializer(user, many=False)
    with connection.cursor() as cursor:
        cursor.execute(
            "SELECT base_order._id FROM base_order,base_orderitem,base_product,base_user WHERE base_order._id = base_orderitem.order_id AND base_orderitem.product_id = base_product._id AND base_product.user_id = base_user.id AND base_user.id = %s",
            [user.id],
        )
        farmerOrder = cursor.fetchone()
        # serializer = OrderSerializer(farmerOrder, many=True)
    return Response(farmerOrder)
Beispiel #4
0
def updateUser(request, pk):  # for admin to update other users
    user = User.objects.get(id=pk)

    data = request.data
    user.first_name = data['name']
    user.username = data['email']
    user.email = data['email']
    user.is_staff = data['isAdmin']

    user.save()
    serializer = UserSerializer(user, many=False)
    return Response(serializer.data)
def createProduct(request):
    user = request.user
    farmerUser = UserSerializer(user, many=False)
    if user.isFarmer == True:
        product = Product.objects.create(
            user=user,
            name='Sample Name',
            description='Deneme',
            unit='Sample Unit',
            countInStock=0,
            category='Sample Category',
            unitPrice=0,
            isFarmProduct=True,
        )

        serializer = ProductSerializer(product, many=False)
        return Response(serializer.data)
    else:
        return Response("You are did not complete Farmer informations yet")
Beispiel #6
0
def getFarmersOrders(request):
    user = request.user
    farmerUser = UserSerializer(user, many=False)

    #Order.orderItem_set.all()

    #Order      where order.id = OrderItem.orderid and orderItem.productId = product.id and product.user = user.userid and user=userıd
    #orderUsers = get_user_model().objects.get(id=user.id)
    #orderUsers.

    #query1= OrderItem.objects.all()

    #orders = Order.objects.filter(Q(_id_contains=query1) | Q(last_name__contains=query)

    productFilter = user.product_set.all()
    orderItem = OrderItem.objects.filter()
    orders = Order.objects.filter(user_id=user.id)
    serializer = OrderSerializer(orders, many=True)
    return Response(serializer.data)
Beispiel #7
0
def updateUser(request, pk):
    user = get_user_model().objects.get(id=pk)

    data = request.data

    user.first_name = data['name']
    user.username = data['email']
    user.email = data['email']
    user.is_staff = data['isAdmin']
    user.isFarmer = data['isFarmer']
    user.locationX = data['locationX']
    user.locationY = data['locationY']
    user.farmName = data['farmName']
    user.address = data['address']
    user.description = data['description']
    user.farmerPoint = data['farmerPoint']
    user.numReviews = data['numReviews']

    user.save()

    serializer = UserSerializer(user, many=False)

    return Response(serializer.data)
Beispiel #8
0
def getUsers(request):
    users = User.objects.all()
    serializer = UserSerializer(users, many=True)

    return Response(serializer.data)
Beispiel #9
0
def getUserProfile(request):
    user = request.user

    serializer = UserSerializer(user, many=False)

    return Response(serializer.data)
Beispiel #10
0
def getUserById(request, pk):
    user = User.objects.get(id=pk)
    serializer = UserSerializer(
        user, many=False)  # many = True # serializing many object or one
    return Response(serializer.data)
Beispiel #11
0
def getUsers(request):
    users = User.objects.all()
    serializer = UserSerializer(
        users, many=True)  # many = True # serializing many object or one
    return Response(serializer.data)
Beispiel #12
0
def getUserById(request, pk):
    user = User.objects.get(id=pk)
    serializer = UserSerializer(user, many=False)
    return Response(serializer.data)
Beispiel #13
0
def getUserById(request, pk):
    # Query the DB for all the products
    user = User.objects.get(id=pk)
    serializer = UserSerializer(user, many=False)
    return Response(serializer.data)
Beispiel #14
0
def getUsers(request):
    # Query the DB for all the products
    users = User.objects.all()
    serializer = UserSerializer(users, many=True)
    return Response(serializer.data)
Beispiel #15
0
def getUsers(request):
    users = User.objects.all()
    serializer = UserSerializer(users, many=True)
    # many or single object to serialize required
    # serializer required for every model we want to return
    return Response(serializer.data)
Beispiel #16
0
def getUserProfile(request):  #for logged in user to modify their own data
    user = request.user
    serializer = UserSerializer(user, many=False)
    return Response(serializer.data)