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)
def create(review_id, text=None, rating=None): """Creates a new revision for the given review. Args: review_id (uuid): ID of the review. text (str): Updated/New text part of the review. rating (int): Updated/New rating part of the review """ if text is None and rating is None: raise db_exceptions.BadDataException("Text part and rating part of a revision can not be None simultaneously") if rating not in VALID_RATING_VALUES: raise ValueError("{} is not a valid rating value. It must be on the scale 1-5".format(rating)) # Convert ratings to values on a scale 0-100 rating = RATING_SCALE_0_100.get(rating) with db.engine.connect() as connection: connection.execute(sqlalchemy.text(""" INSERT INTO revision(review_id, timestamp, text, rating) VALUES (:review_id, :timestamp, :text, :rating) """), { "review_id": review_id, "timestamp": datetime.now(), "text": text, "rating": rating, }) # Update average rating if rating part of the review has changed review = db_review.get_by_id(review_id) rev_num = get_revision_number(review["id"], review["last_revision"]["id"]) if rev_num > 1: revisions = get(review["id"], limit=2, offset=0) if revisions[0]["rating"] != revisions[1]["rating"]: db_avg_rating.update(review["entity_id"], review["entity_type"]) elif rating is not None: db_avg_rating.update(review["entity_id"], review["entity_type"])
def create(connection, review_id, text=None, rating=None): """Creates a new revision for the given review. Args: connection: connection to database to update/create the review review_id (uuid): ID of the review. text (str): Updated/New text part of the review. rating (int): Updated/New rating part of the review """ if text is None and rating is None: raise db_exceptions.BadDataException("Text part and rating part of a revision can not be None simultaneously") if rating not in VALID_RATING_VALUES: raise ValueError("{} is not a valid rating value. It must be on the scale 1-5".format(rating)) # Convert ratings to values on a scale 0-100 rating = RATING_SCALE_0_100.get(rating) query = sqlalchemy.text("""INSERT INTO revision(review_id, timestamp, text, rating) VALUES (:review_id, :timestamp, :text, :rating)""") params = { "review_id": review_id, "timestamp": datetime.now(), "text": text, "rating": rating, } connection.execute(query, params)
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)