diff --git a/critiquebrainz/db/revision.py b/critiquebrainz/db/revision.py index bce09ee49..6b35ce0d3 100644 --- a/critiquebrainz/db/revision.py +++ b/critiquebrainz/db/revision.py @@ -230,3 +230,22 @@ def votes(revision_id): else: # False = negative revision_votes["negative"] += 1 return revision_votes + + +def delete_draft_revisons(review_id): + """Delete all draft revisions of a review after the review has been published. + + Args: + review_id: ID of the review. + """ + with db.engine.connect() as connection: + connection.execute(sqlalchemy.text(""" + DELETE + FROM revision + USING review + WHERE review.id = revision.review_id + AND revision.review_id = :review_id + AND revision.timestamp < review.published_on + """), { + "review_id": review_id, + }) diff --git a/critiquebrainz/frontend/views/review.py b/critiquebrainz/frontend/views/review.py index a00b4b239..3a543e302 100644 --- a/critiquebrainz/frontend/views/review.py +++ b/critiquebrainz/frontend/views/review.py @@ -417,6 +417,9 @@ def edit(id): raise BadRequest(lazy_gettext("Changing license of a published review\ or converting a published review back to drafts is not allowed.")) + if form.state.data == "publish" and review["is_draft"] == True: + db_revision.delete_draft_revisons(review["id"]) + flash.success(gettext("Review has been updated.")) return redirect(url_for('.entity', id=review["id"])) else: