def entity(id, rev=None): review = get_review_or_404(id) # Not showing review if it isn't published yet and not viewed by author. if review["is_draft"] and not (current_user.is_authenticated and current_user == review["user"]): raise NotFound(gettext("Can't find a review with the specified ID.")) if review["is_hidden"]: if not current_user.is_admin(): raise Forbidden( gettext("Review has been hidden. " "You need to be an administrator to view it.")) else: flash.warn(gettext("Review has been hidden.")) spotify_mappings = None soundcloud_url = None if review["entity_type"] == 'release_group': spotify_mappings = mbspotify.mappings(str(review["entity_id"])) soundcloud_url = soundcloud.get_url(str(review["entity_id"])) count = db_revision.get_count(id) if not rev: rev = count if rev < count: flash.info( gettext( 'You are viewing an old revision, the review has been updated since then.' )) elif rev > count: raise NotFound( gettext("The revision you are looking for does not exist.")) revision = db_revision.get(id, offset=count - rev)[0] if not review[ "is_draft"] and current_user.is_authenticated: # if user is logged in, get their vote for this review try: vote = db_vote.get(user_id=current_user.id, revision_id=revision['id']) except db_exceptions.NoDataFoundException: vote = None else: # otherwise set vote to None, its value will not be used vote = None if revision["text"] is None: review["text_html"] = None else: review["text_html"] = markdown(revision['text'], safe_mode="escape") user_all_reviews, review_count = db_review.list_reviews( # pylint: disable=unused-variable user_id=review["user_id"], sort="random", exclude=[review["id"]], ) other_reviews = user_all_reviews[:3] avg_rating = get_avg_rating(review["entity_id"], review["entity_type"]) return render_template('review/entity/%s.html' % review["entity_type"], review=review, spotify_mappings=spotify_mappings, soundcloud_url=soundcloud_url, vote=vote, other_reviews=other_reviews, avg_rating=avg_rating)
def unhide(id): review = get_review_or_404(id) if not review["is_hidden"]: flash.info(gettext("Review is not hidden.")) return redirect(url_for('.entity', id=review["id"])) form = AdminActionForm() if form.validate_on_submit(): db_review.set_hidden_state(review["id"], is_hidden=False) db_moderation_log.create(admin_id=current_user.id, action=AdminActions.ACTION_UNHIDE_REVIEW, reason=form.reason.data, review_id=review["id"]) flash.success(gettext("Review is not hidden anymore.")) return redirect(url_for('.entity', id=review["id"])) return render_template('log/action.html', review=review, form=form, action=AdminActions.ACTION_UNHIDE_REVIEW.value)
def create(entity_type=None, entity_id=None): if not (entity_id or entity_type): for allowed_type in ENTITY_TYPES: if mbid := request.args.get(allowed_type): entity_type = allowed_type entity_id = mbid break if entity_type: return redirect( url_for('.create', entity_type=entity_type, entity_id=entity_id)) flash.info(gettext("Please choose an entity to review.")) return redirect(url_for('search.selector', next=url_for('.create')))
def hide(id): review = get_review_or_404(id) if review["is_hidden"]: flash.info(gettext("Review is already hidden.")) return redirect(url_for('.entity', id=review["id"])) form = AdminActionForm() if form.validate_on_submit(): db_review.set_hidden_state(review["id"], is_hidden=True) db_moderation_log.create(admin_id=current_user.id, action=ACTION_HIDE_REVIEW, reason=form.reason.data, review_id=review["id"]) review_reports, count = db_spam_report.list_reports(review_id=review["id"]) # pylint: disable=unused-variable for report in review_reports: db_spam_report.archive(report["user_id"], report["revision_id"]) flash.success(gettext("Review has been hidden.")) return redirect(url_for('.entity', id=review["id"])) return render_template('log/action.html', review=review, form=form, action=ACTION_HIDE_REVIEW)
def unblock(user_id): user = db_users.get_by_id(user_id) if not user: raise NotFound("Can't find a user with ID: {user_id}".format(user_id=user_id)) if not user['is_blocked']: flash.info(gettext("This account is not blocked.")) return redirect(url_for('user.reviews', user_id=user['id'])) form = AdminActionForm() if form.validate_on_submit(): db_users.unblock(user['id']) db_moderation_log.create(admin_id=current_user.id, action=AdminActions.ACTION_UNBLOCK_USER, reason=form.reason.data, user_id=user['id']) flash.success(gettext("This user account has been unblocked.")) return redirect(url_for('user.reviews', user_id=user['id'])) return render_template('log/action.html', user=user, form=form, action=AdminActions.ACTION_UNBLOCK_USER.value)
def block(user_id): user = db_users.get_by_id(user_id) if not user: raise NotFound("Can't find a user with ID: {user_id}".format(user_id=user_id)) if user['is_blocked']: flash.info(gettext("This account is already blocked.")) return redirect(url_for('user.reviews', user_id=user['id'])) form = AdminActionForm() if form.validate_on_submit(): db_users.block(user['id']) db_moderation_log.create(admin_id=current_user.id, action=ACTION_BLOCK_USER, reason=form.reason.data, user_id=user['id']) flash.success(gettext("This user account has been blocked.")) return redirect(url_for('user.reviews', user_id=user['id'])) return render_template('log/action.html', user=user, form=form, action=ACTION_BLOCK_USER)
def block(user_id): user = User.query.get_or_404(str(user_id)) if user.is_blocked: flash.info(gettext("This account is already blocked.")) return redirect(url_for('user.reviews', user_id=user.id)) form = AdminActionForm() if form.validate_on_submit(): user.block() ModerationLog.create(admin_id=current_user.id, action=ACTION_BLOCK_USER, reason=form.reason.data, user_id=user.id) flash.success(gettext("This user account has been blocked.")) return redirect(url_for('user.reviews', user_id=user.id)) return render_template('log/action.html', user=user, form=form, action=ACTION_BLOCK_USER)
def create(): entity_id, entity_type = None, None for entity_type in ENTITY_TYPES: entity_id = request.args.get(entity_type) if entity_id: entity_type = entity_type break if not (entity_id or entity_type): logging.warning("Unsupported entity type") raise BadRequest("Unsupported entity type") if not entity_id: flash.info(gettext("Please choose an entity to review.")) return redirect(url_for('search.selector', next=url_for('.create'))) if current_user.is_blocked: flash.error( gettext("You are not allowed to write new reviews because your " "account has been blocked by a moderator.")) return redirect(url_for('user.reviews', user_id=current_user.id)) # Checking if the user already wrote a review for this entity review = db_review.list_reviews(user_id=current_user.id, entity_id=entity_id)[0] if review: flash.error( gettext("You have already published a review for this entity!")) return redirect(url_for('review.entity', id=review["id"])) form = ReviewCreateForm(default_language=get_locale()) if form.validate_on_submit(): if current_user.is_review_limit_exceeded: flash.error( gettext("You have exceeded your limit of reviews per day.")) return redirect(url_for('user.reviews', user_id=current_user.id)) is_draft = form.state.data == 'draft' if form.text.data == '': form.text.data = None review = db_review.create(user_id=current_user.id, entity_id=entity_id, entity_type=entity_type, text=form.text.data, rating=form.rating.data, license_id=form.license_choice.data, language=form.language.data, is_draft=is_draft) if is_draft: flash.success(gettext("Review has been saved!")) else: flash.success(gettext("Review has been published!")) return redirect(url_for('.entity', id=review['id'])) entity = get_entity_by_id(entity_id, entity_type) if not entity: flash.error( gettext( "You can only write a review for an entity that exists on MusicBrainz!" )) return redirect(url_for('search.selector', next=url_for('.create'))) if entity_type == 'release_group': spotify_mappings = mbspotify.mappings(entity_id) soundcloud_url = soundcloud.get_url(entity_id) if not form.errors: flash.info( gettext( "Please provide some text or a rating for this review.")) return render_template('review/modify/write.html', form=form, entity_type=entity_type, entity=entity, spotify_mappings=spotify_mappings, soundcloud_url=soundcloud_url) if not form.errors: flash.info( gettext("Please provide some text or a rating for this review.")) return render_template('review/modify/write.html', form=form, entity_type=entity_type, entity=entity)
gettext("Sorry, we couldn't find a %s with that MusicBrainz ID." % entity_type)) if not entity: flash.error( gettext( "You can only write a review for an entity that exists on MusicBrainz!" )) return redirect(url_for('search.selector', next=url_for('.create'))) if entity_type == 'release_group': spotify_mappings = mbspotify.mappings(entity_id) soundcloud_url = soundcloud.get_url(entity_id) if not form.errors: flash.info( gettext( "Please provide some text or a rating for this review.")) return render_template('review/modify/write.html', form=form, entity_type=entity_type, entity=entity, spotify_mappings=spotify_mappings, soundcloud_url=soundcloud_url) entity_title = None if 'title' in entity: entity_title = entity['title'] elif 'name' in entity: entity_title = entity['name'] if not form.errors: