Пример #1
0
def like(productId):
    product = Product.query.get(productId)
    if product is None:
        return jsonify({
            "message": f"Sorry! Product ID ({productId}) DoesNotExist"
        }), 404

    # TODO -  Error handle required for HTTP request
    request = requests.get(f"http://{admin['host']}:{admin['port']}/api/user")
    user = request.json()

    try:
        productUser = ProductUser(user_id=user["id"], product_id=productId)
        db.session.add(productUser)
        db.session.commit()

        # Send Event to Admin
        publish("PRODUCT_LIKED", {"id": productId})
    except BaseException as e:
        # TODO - user_id & product_id unique constraints is NOT working.
        print(e.message, e.args)
        return jsonify({
            "message": f"Sorry! User-{user['id']} already liked Product-{productId}"
        }), 400

    return jsonify({
        "message": f"User ID ({user['id']}) liked Product ID ({productId})"
    }), 201
Пример #2
0
 def delete(self, friend_name):
     friend = FriendModel.find(friend_name)
     if friend:
         friend.delete()
         publish('friend_deleted', friend.json())
         return {'message': 'Friend deleted.'}
     return {'message': 'Friend not found.'}, 404
Пример #3
0
def general():
    """
    TODO: use cerberus default
    link: http://docs.python-cerberus.org/en/stable/normalization-rules.html#default-values
    """
    default_data = {
        "uuid": str(uuid.uuid4()),
        "docker_image": "node",
        "git_branch": "master",
        "npm_registry": "https://registry.npm.taobao.org/",
        "status": "activate",
        "jenkins_build_number": None,
        "jenkins_build_result": None,
        "jenkins_build_url": None,
        "jenkins_job_name": None,
        "mail": None,
        "dist_url": None,
    }
    schema = {'git_url': {'type': 'string', 'required': True}}
    v.allow_unknown = True
    if v.validate(request.json, schema):
        message = {**default_data, **request.json}
        producer.publish(scope.exchange_dict['build'], '', message)
        return jsonify(message), 200
    else:
        return jsonify(v.errors), 400
Пример #4
0
def result():
    schema = {
        'uuid': {
            'type': 'string',
            'required': True
        },
        'git_url': {
            'type': 'string',
            'required': True
        },
        'git_branch': {
            'type': 'string',
            'required': True
        },
        'docker_image': {
            'type': 'string',
            'required': True
        },
        'npm_registry': {
            'type': 'string',
            'required': True
        },
        'mail': {
            'type': 'string',
            'required': True,
        },
        'status': {
            'type': 'string',
            'allowed': ['activate', 'building', 'success', 'fail'],
            'required': True
        },
        'jenkins_build_number': {
            'type': 'integer',
            'required': True,
        },
        'jenkins_build_result': {
            'type': 'string',
            'required': True,
        },
        'jenkins_build_url': {
            'type': 'string',
            'required': True,
        },
        'jenkins_job_name': {
            'type': 'string',
            'required': True,
        },
        'dist_url': {
            'type': 'string',
            'required': True,
        },
    }
    v.allow_unknown = False
    if v.validate(request.json, schema):
        producer.publish(scope.exchange_dict['built'], '', request.json)
        return jsonify(request.json), 201
    else:
        return jsonify(v.errors), 400
Пример #5
0
def views(id):
    req = requests.get('http://docker.for.linux.localhost:8000/api/user')
    json = req.json()
    productUser = ProductUser(user_id=json['id'], product_id=id)
    db.session.add(productUser)
    db.session.commit()
    publish('product_viewed', id)

    return jsonify({'message': 'success'})
Пример #6
0
def like(id):
    req = requests.get('http://docker.for.win.localhost:8000/api/user')
    json = req.json()
    try:
        productUser = ProductUser(user_id=json['id'], product_id=id)
        db.session.add(productUser)
        db.session.commit()
        publish('product_liked', id)
    except:
        abort(400, "you already liked this product")
    return jsonify({"message": "Success"})
Пример #7
0
def like(id):
    # Call the api to get random user
    req = requests.get("http://docker.for.linux.localhost:8000/user/")
    json = req.json()
    try:
        product_user = ProductUser(user_id=json["id"], product_id=id)
        db.session.add(product_user)
        db.session.commit()
        publish("product_liked", id)
    except:
        abort(400, "You already liked this product.")
    return jsonify(req.json())
Пример #8
0
def test_producer():
    """instructions:

        These tests require a consumer to receive the published message

        1. in a different console start the server:
            import consumer
            consumer.main()

        2. run this test
    """
    import producer
    producer.publish("hey here's a message for you")
Пример #9
0
def likes(id):
    #django docker id : 83ebcf94ab77
    req = requests.get('http://83ebcf94ab77:8000/api/user')
    json = req.json()
    try:
        product_user = ProductUser(user_id=json['id'],product_id=id)
        db.session.add(product_user)
        db.session.commit()

        publish('product_liked', id)
    except:
        abort(400, 'you already liked this product')
    return jsonify({"message":"sucess"})
Пример #10
0
def like(id):
    request = requests.get('http://172.17.0.1:8000/api/user')
    user = request.json()
    print(user)
    try:
        product_user = ProductUser(user_id=user['id'], product_id=id)
        db.session.add(product_user)
        db.session.commit()

        publish('product_liked', id)
    except:
        abort(400, 'You already liked this product')
    return jsonify({"message": "success"})
Пример #11
0
def like(id):
    req = requests.get('http://172.168.0.20:8020/api/user', timeout=5)
    json = req.json()

    try:
        productUser = ProductUser(user_id=json['id'], product_id=id)
        db.session.add(productUser)
        db.session.commit()

        publish('product_liked', id)
    except:
        abort(400, 'You already liked this product')
    return jsonify({'message': 'success'})
Пример #12
0
def choose(id):
    #получаем id радомно, просто для примера коммуникации
    req = requests.get('http://docker.for.mac.localhost:8000/api/user')
    json = req.json()
    try:
        productUser = ProductUser(user_id=json['id'], product_id=id)
        db.session.add(productUser)
        db.session.commit()

        publish('product chosen', id)
    except:
        abort(400, "You already chose this product")

    return jsonify({'message': 'success'})
def like(id):
    req = requests.get('http://host.docker.internal:8000/api/user')
    json = req.json()

    try:
        productUser = ProductUser(user_id=json['id'], product_id=id)
        db.session.add(productUser)
        db.session.commit()

        publish('product_liked', id)
    except:
        abort(400, 'You already liked this product')

    return jsonify({'message': 'success'})
Пример #14
0
def like(id):
    req = requests.get(user_API)
    json = req.json()

    try:
        productUser = ProductUser(user_id=json['id'], product_id=id)
        db.session.add(productUser)
        db.session.commit()

        publish(event_liked, id)
    except:
        abort(400, 'You already liked this product')

    return jsonify({'message': 'success'})
Пример #15
0
def like(id):
    req = requests.get('http://172.17.0.1:8000/api/user')
    json = req.json()
    print (json)
    try:
        productUser = Product_User(user_id =json["id"], product_id=id)
        db.session.add(productUser)
        db.session.commit()
        
        publish('product_liked', id)
    except:
        abort(400, 'you already liked this photo')
        
    return jsonify({"message":"success"})
Пример #16
0
def curtidas(id):
    req = requests.get('http://docker.for.mac.localhost:8000/api/usuario')
    json = req.json()
    try:
        produto_usuario = ProdutoUsuario(usuario_id=json['id'], produto_id=id)
        db.session.add(produto_usuario)
        db.session.commit()

        publish('produto_curtido', id)

    except:
        abort(400, 'Você já curtiu esse produto!')

    return jsonify({'mensagem': 'Sucesso'})
Пример #17
0
def like(product_id):
    # req = requests.get("http://docker.for.linux.localhost:8000/api/user")    # here we are taking random user for liked post
    # json = req.json()
    print("aaaa", product_id)
    try:
        productUser = ProductUser(user_id=1, product_id=product_id)
        db.session.add(productUser)
        db.session.commit()

        publish("product_liked", product_id)
    except:
        abort(400, "You already liked this product")

    return jsonify({"message": "success"})
Пример #18
0
def like(id):
    req = requests.get('http://172.17.0.1:8000/api/user')
    json = req.json()
    try:
        productUser = ProductUser(user_id=json['id'], product_id=id)
        db.session.add(productUser)
        db.session.commit()
    # event
        publish('product_like', id)
    except:
        abort(400, 'You already like this product')
    return jsonify({
        'message': 'success'
    })
Пример #19
0
def like(id):
    req = requests.get('http://python_admin:8000/api/user')
    print(req.status_code)
    json = req.json()

    try:
        productUser = ProductUser(user_id=json['id'], product_id=id)
        db.session.add(productUser)
        db.session.commit()

        publish('product_liked', id)
    except:
        abort(400, 'You already liked this product')

    return jsonify({'message': 'success'})
Пример #20
0
def like(id):
    # https://docs.docker.jp/docker-for-mac/networking.html#mac-i-want-to-connect-from-a-container-to-a-service-on-the-host
    req = requests.get("http://host.docker.internal:8000/api/user")
    json = req.json()

    try:
        productUser = ProductUser(user_id=json["id"], product_id=id)
        db.session.add(productUser)
        db.session.commit()

        publish("product_liked", id)
    except:  # noqa: E722
        abort(400, "You already liked this product")

    return jsonify({"message": "success"})
Пример #21
0
def like(id):
    # Call the admin app admin/users to get a random user then like on his/her behalf
    req = requests.get('http://docker.for.mac.localhost:8000/api/users')
    json = req.json()

    # try:
    productUser = ProductUser(user_id=json['id'], product_id=id)
    db.session.add(productUser)
    db.session.commit()
    # Like product Event
    publish("like_product", id)
    # except:
    #     abort(400, 'You already liked this product!')

    return jsonify({'message': 'success'})
Пример #22
0
def like(id):
    # req = requests.get('http://localhost:8000/api/user') -- need to dockerfy this...
    req = requests.get('http://docker.for.mac.localhost:8000/api/user')
    json = req.json()

    try:
        productUser = ProductUser(user_id=json['id'], product_id=id)
        db.session.add(productUser)
        db.session.commit()

        publish('product_liked', id)
    except:
        abort(400, 'You already liked this product!')

    return jsonify({'message': 'success'})
Пример #23
0
    def post(self):
        data = body_request.parse_args()
        friend_name = data.get('friend_name')

        friend = FriendModel(**data)
        try:
            friend.save()
            publish('friend_added', friend.json())

        except:
            return {
                'message':
                'An internal error ocurred trying to create a new friend.'
            }, 500
        return friend.json()
Пример #24
0
def like(id):
    req = requests.get("http://django-backend:8000/api/user")
    json = req.json()

    try:
        product_user = ProductUser(user_id=json["id"], product_id=id)
        db.session.add(product_user)
        db.session.commit()

        publish("product_liked", id)
    except:
        abort(400, "You already liked this product")

    return jsonify({
        "message": "success"
    })
Пример #25
0
def like(id):
    req = requests.get("http://172.26.0.1:8000/api/user")
    result = req.json()

    try:
        productUser = ProductUser(user_id=result['id'], product_id=id)
        db.session.add(productUser)
        db.session.commit()

        publish('product_liked', id)
    except:
        abort(400, 'You already liked this product')

    return jsonify({
        'message': 'success'
    })
def like(id):
    req = requests.get('http://backend:8000/api/user')
    json = req.json()
    print(json)
    try:
        productuser = ProductUser(user_id=json['id'], product_id=id)
        db.session.add(productuser)
        product = Product.query.get(id)
        product.likes = product.likes + 1
        db.session.commit()
        publish('Product liked', id)

    except:
        abort(400, 'You already liked this product')

    return jsonify({'message': 'success'})
Пример #27
0
def vote(id):
    req = requests.get('http://docker.for.mac.localhost:8000/api/user')
    json = req.json()

    try:
        post_user = PostUser(user_id=json['id'], post_id=id)
        db.session.add(post_user)
        db.session.commit()

        publish('post voted for', id)
    except:
        abort(400, 'You already voted for this post')

    return jsonify({
        'message': 'success'
    })
Пример #28
0
def like(id):
    res = requests.get('http://docker.for.mac.localhost:8000/api/user')
    user = res.json()

    try:
        productUser = ProductUser(user_id=user['id'], product_id=id)
        db.session.add(productUser)
        db.session.commit()

        publish('product_liked', id)

    except:
        abort(400, 'User already liked product ')

    return jsonify({
        'message': 'success',
    })
Пример #29
0
def order_add_item():
    req = requests.get('http://localhost:8000/django/api/user')
    json = req.json()

    try:

        productUser = ProductsUser(user_id=json['id'], product_id=id)
        db.session.add(productUser)
        db.session.commit()
        publish('Item_added_to _order', id)
    except:
        abort(400, 'already added')

    return jsonify({'message': 'success'})

    # Find open order
    known_order = Order.query.filter_by(user_id=u_id, is_open=1).first()

    if known_order is None:
        # Create the order
        known_order = Order()
        known_order.is_open = True
        known_order.user_id = u_id

        order_item = OrderItem(p_id, qty)
        known_order.items.append(order_item)

    else:
        found = False
        # Check if we already have an order item with that product
        for item in known_order.items:

            if item.product_id == p_id:
                found = True
                item.quantity += qty

        if found is False:
            order_item = OrderItem(p_id, qty)
            known_order.items.append(order_item)

    db.session.add(known_order)
    db.session.commit()

    response = jsonify({'result': known_order.to_json()})

    return response
Пример #30
0
    def like(id):
        req = requests.get("http://172.27.0.1:8000/api/user")
        json = req.json()

        try:
            productUser = ProductUser(user_id=json["id"], product_id=id)
            db.session.add(productUser)
            db.session.commit()

            product = Product.query.get(id)
            product.likes += 1
            db.session.commit()
            print('Updated likes count')

            publish("product_liked", id)
        except:
            abort(400, "you already liked this")
        return jsonify({"message": "success"})