Exemplo n.º 1
0
def update(review_id,
           *,
           drafted,
           text=None,
           rating=None,
           license_id=None,
           language=None,
           is_draft=None):
    # TODO: Get rid of `drafted` argument. This information about review should be retrieved inside this function.
    """Update a review.

    Args:
        review_id (uuid): ID of the review.
        drafted (bool): Whether the review is currently set as a draft.
        license_id (str): ID of a license that needs to be associated with this review.
        is_draft (bool): Whether to publish review (False) or keep it as a graft (True).
        text (str): Updated text part of a review.
        rating (int): Updated rating part of a review.
    """
    if text is None and rating is None:
        raise db_exceptions.BadDataException(
            "Text part and rating part of a review can not be None simultaneously"
        )

    updates = []
    updated_info = {}

    if license_id is not None:
        if not drafted:  # If trying to convert published review into draft
            raise db_exceptions.BadDataException(
                "Changing license of a published review is not allowed.")
        updates.append("license_id = :license_id")
        updated_info["license_id"] = license_id

    if language is not None:
        updates.append("language = :language")
        updated_info["language"] = language

    if is_draft is not None:
        if not drafted and is_draft:  # If trying to convert published review into draft
            raise db_exceptions.BadDataException(
                "Converting published reviews back to drafts is not allowed.")
        updates.append("is_draft = :is_draft")
        updated_info["is_draft"] = is_draft

    setstr = ", ".join(updates)
    query = sqlalchemy.text("""
        UPDATE review
           SET {setstr}
         WHERE id = :review_id
    """.format(setstr=setstr))

    if setstr:
        updated_info["review_id"] = review_id
        with db.engine.connect() as connection:
            connection.execute(query, updated_info)

    db_revision.create(review_id, text, rating)
    cache.invalidate_namespace(REVIEW_CACHE_NAMESPACE)
Exemplo n.º 2
0
def update(review_id, *, drafted, text=None, rating=None, license_id=None, language=None, is_draft=None):
    # TODO: Get rid of `drafted` argument. This information about review should be retrieved inside this function.
    """Update a review.

    Args:
        review_id (uuid): ID of the review.
        drafted (bool): Whether the review is currently set as a draft.
        license_id (str): ID of a license that needs to be associated with this review.
        is_draft (bool): Whether to publish review (False) or keep it as a draft (True).
        text (str): Updated text part of a review.
        rating (int): Updated rating part of a review.
    """
    if text is None and rating is None:
        raise db_exceptions.BadDataException("Text part and rating part of a review can not be None simultaneously")

    updates = []
    updated_info = {}

    if license_id is not None:
        if not drafted:  # If trying to convert published review into draft
            raise db_exceptions.BadDataException("Changing license of a published review is not allowed.")
        updates.append("license_id = :license_id")
        updated_info["license_id"] = license_id

    if language is not None:
        updates.append("language = :language")
        updated_info["language"] = language

    if is_draft is not None:
        if not drafted and is_draft:  # If trying to convert published review into draft
            raise db_exceptions.BadDataException("Converting published reviews back to drafts is not allowed.")
        if drafted and not is_draft:
            published_on = datetime.now()
            updates.append("published_on = :published_on")
            updated_info["published_on"] = published_on
        updates.append("is_draft = :is_draft")
        updated_info["is_draft"] = is_draft

    setstr = ", ".join(updates)
    query = sqlalchemy.text("""
        UPDATE review
           SET {setstr}
         WHERE id = :review_id
    """.format(setstr=setstr))

    if setstr:
        updated_info["review_id"] = review_id
        with db.engine.connect() as connection:
            connection.execute(query, updated_info)

    db_revision.create(review_id, text, rating)
    cache.invalidate_namespace(REVIEW_CACHE_NAMESPACE)
Exemplo n.º 3
0
def create(*,
           entity_id,
           entity_type,
           user_id,
           is_draft,
           text=None,
           rating=None,
           language=DEFAULT_LANG,
           license_id=DEFAULT_LICENSE_ID,
           source=None,
           source_url=None):
    """Create a new review.

    Optionally, if a review is being imported from external source which needs
    a reference, you can specify `source` and `source_url` arguments. This
    reference will accompany the review.

    Args:
        entity_id (uuid): ID of an entity that review is for.
        entity_type (str): Entity type associated with the `entity_id`.
        user_id (uuid): ID of the reviewer.
        is_draft (bool): Whether this review is a draft (not shown to public).
        text (str): Text part of the review.
        rating (int): Rating part of the review.
        language (str): Language code that indicates which language the review
                        is written in.
        license_id (str): ID of the license.
        source (str): Name of the source of the review.
        source_url (str): URL pointing to the source of the review.

    Returns:
        Dictionary with the following structure
        {
            "id": uuid,
            "entity_id": uuid,
            "entity_type": str,
            "user_id": uuid,
            "user": dict,
            "edits": int,
            "is_draft": bool,
            "is_hidden": bool,
            "language": str,
            "license_id": str,
            "source": str,
            "source_url": str,
            "last_revision: dict,
            "votes": dict,
            "popularity": int,
            "text": str,
            "rating": int,
            "created": datetime,
            "license": dict,
        }
    """
    if text is None and rating is None:
        raise db_exceptions.BadDataException(
            "Text part and rating part of a review can not be None simultaneously"
        )
    if language not in supported_languages:
        raise ValueError("Language: {} is not supported".format(language))

    with db.engine.connect() as connection:
        result = connection.execute(sqlalchemy.text("""
            INSERT INTO review (id, entity_id, entity_type, user_id, edits, is_draft, is_hidden, license_id, language, source, source_url)
            VALUES (:id, :entity_id, :entity_type, :user_id, :edits, :is_draft, :is_hidden, :license_id, :language, :source, :source_url)
         RETURNING id;
        """), {  # noqa: E501
            "id": str(uuid.uuid4()),
            "entity_id": entity_id,
            "entity_type": entity_type,
            "user_id": user_id,
            "edits": 0,
            "is_draft": is_draft,
            "is_hidden": False,
            "language": language,
            "license_id": license_id,
            "source": source,
            "source_url": source_url,
        })
        review_id = result.fetchone()[0]
        # TODO(roman): It would be better to create review and revision in one transaction
        db_revision.create(review_id, text, rating)
        cache.invalidate_namespace(REVIEW_CACHE_NAMESPACE)
    return get_by_id(review_id)
Exemplo n.º 4
0
def create(*, entity_id, entity_type, user_id, is_draft, text=None, rating=None,
           language=DEFAULT_LANG, license_id=DEFAULT_LICENSE_ID,
           source=None, source_url=None):
    """Create a new review.

    Optionally, if a review is being imported from external source which needs
    a reference, you can specify `source` and `source_url` arguments. This
    reference will accompany the review.

    Args:
        entity_id (uuid): ID of an entity that review is for.
        entity_type (str): Entity type associated with the `entity_id`.
        user_id (uuid): ID of the reviewer.
        is_draft (bool): Whether this review is a draft (not shown to public).
        text (str): Text part of the review.
        rating (int): Rating part of the review.
        language (str): Language code that indicates which language the review
                        is written in.
        license_id (str): ID of the license.
        source (str): Name of the source of the review.
        source_url (str): URL pointing to the source of the review.

    Returns:
        Dictionary with the following structure
        {
            "id": uuid,
            "entity_id": uuid,
            "entity_type": str,
            "user_id": uuid,
            "user": dict,
            "edits": int,
            "is_draft": bool,
            "is_hidden": bool,
            "language": str,
            "license_id": str,
            "source": str,
            "source_url": str,
            "last_revision: dict,
            "votes": dict,
            "popularity": int,
            "text": str,
            "rating": int,
            "license": dict,
            "published_on": datetime,
        }
    """
    if text is None and rating is None:
        raise db_exceptions.BadDataException("Text part and rating part of a review can not be None simultaneously")
    if language not in supported_languages:
        raise ValueError("Language: {} is not supported".format(language))
    if is_draft:
        published_on = None
    else:
        published_on = datetime.now()

    with db.engine.connect() as connection:
        result = connection.execute(sqlalchemy.text("""
            INSERT INTO review (id, entity_id, entity_type, user_id, edits, is_draft, is_hidden, license_id, language, source, source_url, published_on)
            VALUES (:id, :entity_id, :entity_type, :user_id, :edits, :is_draft, :is_hidden, :license_id, :language, :source, :source_url, :published_on)
         RETURNING id;
        """), {  # noqa: E501
            "id": str(uuid.uuid4()),
            "entity_id": entity_id,
            "entity_type": entity_type,
            "user_id": user_id,
            "edits": 0,
            "is_draft": is_draft,
            "is_hidden": False,
            "language": language,
            "license_id": license_id,
            "source": source,
            "source_url": source_url,
            "published_on": published_on,
        })
        review_id = result.fetchone()[0]
        # TODO(roman): It would be better to create review and revision in one transaction
        db_revision.create(review_id, text, rating)
        cache.invalidate_namespace(REVIEW_CACHE_NAMESPACE)
    return get_by_id(review_id)