Example #1
0
        def callback(ch, method, properties, body):
            event = json_serializer.body_to_dic(body.decode('utf-8'))
            if(len(validator.validateSchema(EVENT, event)) > 0):
                return

            if (event["type"] == "article-change"):

                message = event["message"]

                if(len(validator.validateSchema(MSG_ARTICLE_CHANGE, message)) > 0):
                    return

                article_id = message['_id']
                print("RabbitMQ Pricing POST article-change article_id: {}".format(article_id))

                try:
                    price = {}
                    price['max_price'] = message['price']
                    price['min_price'] = message['price']
                    price['price'] = message['price']
                    price['price_currency'] = 'ARS'
                    price['article_id'] = article_id
                    price['state'] = ACTIVE
                    crud._addOrUpdatePrice(price)

                except Exception:
                    print('Cannot handle article-change')
Example #2
0
        def callback(ch, method, properties, body):
            event = json.body_to_dic(body.decode('utf-8'))
            if (len(validator.validateSchema(EVENT_CALLBACK, event)) > 0):
                return

            if (event["type"] == "article-exist"):
                message = event["message"]
                if (len(validator.validateSchema(MSG_ARTICLE_EXIST, message)) >
                        0):
                    return

                exchange = event["exchange"]
                queue = event["queue"]
                referenceId = message["referenceId"]
                articleId = message["articleId"]

                print(
                    "RabbitMQ Catalog GET article-exist catalogId:%r , articleId:%r",
                    referenceId, articleId)

                try:
                    articleValidation.validateArticleExist(articleId)
                    sendArticleValid(exchange, queue, referenceId, articleId,
                                     True)
                except Exception:
                    sendArticleValid(exchange, queue, referenceId, articleId,
                                     False)

            if (event["type"] == "article-data"):
                message = event["message"]
                if (len(validator.validateSchema(MSG_ARTICLE_EXIST, message)) >
                        0):
                    return

                exchange = event["exchange"]
                queue = event["queue"]
                referenceId = message["referenceId"]
                articleId = message["articleId"]

                print(
                    "RabbitMQ Catalog GET article-data catalogId:%r , articleId:%r",
                    referenceId, articleId)

                try:
                    article = crud.getArticle(articleId)
                    valid = ("enabled" in article and article["enabled"])
                    stock = article["stock"]
                    price = article["price"]
                    articleValidation.validateArticleExist(articleId)
                    sendArticleData(exchange, queue, referenceId, articleId,
                                    valid, stock, price)
                except Exception:
                    sendArticleData(exchange, queue, referenceId, articleId,
                                    False, 0, 0)
Example #3
0
        def callback(ch, method, properties, body):
            event = json.body_to_dic(body.decode('utf-8'))
            if (len(validator.validateSchema(EVENT, event)) > 0):
                return

            if (event["type"] == "Delete_article"):
                message = event["message"]
                if (len(validator.validateSchema(MSG_DELETE_ARTICLE, message))
                        > 0):
                    return
                articleId = message["articleId"]

                results = db.category.find({"validation": True})
                for r in results:
                    listArticle = r["articlesCategory"]
                    for element in listArticle:
                        if (element["_id"] == articleId):
                            listArticle.remove(element)
                            r["articlesCategory"] = listArticle
                            r["updated"] = datetime.datetime.utcnow()
                            db.category.replace_one(
                                {"_id": bson.ObjectId(r["_id"])}, r)

            if (event["type"] == "Updated_article"):
                message = event["message"]
                if (len(validator.validateSchema(MSG_UPDATE_ARTICLE, message))
                        > 0):
                    return
                articleId = message["articleId"]
                new_element = {
                    "_id": message["articleId"],
                    "name": message["name"],
                    "description": message["description"],
                    "image": message["image"],
                    "price": message["price"],
                    "stock": message["stock"],
                    "updated": message["updated"],
                    "created": message["created"],
                    "enabled": message["enabled"]
                }

                results = db.category.find({"validation": True})
                for r in results:
                    listArticle = r["articlesCategory"]
                    for element in listArticle:
                        if (element["_id"] == articleId):
                            n = listArticle.index(element)
                            listArticle.remove(element)
                            listArticle.insert(n, new_element)
                            r["articlesCategory"] = listArticle
                            r["updated"] = datetime.datetime.utcnow()
                            db.category.replace_one(
                                {"_id": bson.ObjectId(r["_id"])}, r)
Example #4
0
        def callback(ch, method, properties, body):
            event = json.body_to_dic(body.decode('utf-8'))
            if len(validator.validateSchema(EVENT, event)) > 0:
                return

            if event["type"] == "logout":
                security.invalidate_session(event["message"])
Example #5
0
def send_new_score(id_article, score):
    """
    Envía eventos cuando hay un nuevo score para un artículo

    @api {fanout} score/new_score Nuevo Score

    @apiGroup RabbitMQ POST

    @apiDescription Enviá mensajes de nuevo score

    @apiSuccessExample {json} Mensaje
      {
        "type": "new_score",
        "message" : {
            "id_article": "{id del articulo}",
            "score": {score promedio del articulo}
        }
      }
    """
    exchange = "score"
    connection = pika.BlockingConnection(
        pika.ConnectionParameters(host=config.get_rabbit_server_url()))
    channel = connection.channel()

    channel.exchange_declare(exchange=exchange, exchange_type='fanout')

    message = {
        "type": "new_score",
        "message": {
            "id_article": id_article,
            "score": score,
        }
    }

    validator.validateSchema(MSG_NEW_SCORE, message)

    channel.basic_publish(exchange=exchange,
                          routing_key='',
                          body=json.dic_to_json(message))

    connection.close()

    print("RabbitMQ Cart POST new_score id_article:%r", id_article)
Example #6
0
        def callback(ch, method, properties, body):
            event = json_serializer.body_to_dic(body.decode('utf-8'))
            if(len(validator.validateSchema(EVENT, event)) > 0):
                return

            if (event["type"] == "article-exist"):
                message = event.get('message')

                if(len(validator.validateSchema(MSG_ARTICLE_EXIST, message)) > 0):
                    return

                article_id = message.get('articleId')
                print("RabbitMQ Catalog POST article-exist article_id: {}".format(article_id))

                try:
                    if not message['valid']:
                        crud.del_price(article_id)

                except Exception:
                    print('Cannot handle article-exist')
def validate_schema(document):

    err = validator.validateSchema(PAYMENT_DB_SCHEMA, document)

    if (len(err) > 0):
        raise errors.MultipleArgumentException(err)
def validate_schema(document):
    err = validator.validateSchema(REVIEW_DB_SCHEMA, document)

    if len(err) > 0:
        raise errors.MultipleArgumentException(err)
Example #9
0
def validateSchema(document):
    err = schema_validator.validateSchema(DISCOUNT_DB_SCHEMA, document)

    if (len(err) > 0):
        raise errors.MultipleArgumentException(err)