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)) db_users.unblock(user['id']) flash.success(gettext("This user account has been unblocked.")) return redirect(url_for('user.reviews', user_id=user['id']))
def report(id): review = get_review_or_404(id) if review["is_hidden"] and not current_user.is_admin(): raise NotFound(gettext("Review has been hidden.")) if review["user"] == current_user: flash.error(gettext("You cannot report your own review.")) return redirect(url_for('.entity', id=id)) if current_user.is_blocked: flash.error( gettext("You are not allowed to report this review because " "your account has been blocked by a moderator.")) return redirect(url_for('.entity', id=id)) last_revision_id = review["last_revision"]["id"] report = db_spam_report.get(current_user.id, last_revision_id) if report: flash.error(gettext("You have already reported this review.")) return redirect(url_for('.entity', id=id)) form = ReviewReportForm() if form.validate_on_submit(): db_spam_report.create(last_revision_id, current_user.id, form.reason.data) flash.success(gettext("Review has been reported.")) return redirect(url_for('.entity', id=id)) return render_template('review/report.html', review=review, form=form)
def vote_submit(review_id): review_id = str(review_id) if 'yes' in request.form: vote = True elif 'no' in request.form: vote = False else: vote = None review = get_review_or_404(review_id) if review["is_hidden"] and not current_user.is_admin(): raise NotFound(gettext("Review has been hidden.")) if review["user"] == current_user: flash.error(gettext("You cannot rate your own review.")) return redirect(url_for('.entity', id=review_id)) if current_user.is_vote_limit_exceeded is True and current_user.has_voted( review) is False: flash.error(gettext("You have exceeded your limit of votes per day.")) return redirect(url_for('.entity', id=review_id)) if current_user.is_blocked: flash.error( gettext("You are not allowed to rate this review because " "your account has been blocked by a moderator.")) return redirect(url_for('.entity', id=review_id)) db_vote.submit( user_id=current_user.id, revision_id=review["last_revision"]["id"], vote=vote, # overwrites an existing vote, if needed ) flash.success(gettext("You have rated this review!")) return redirect(url_for('.entity', id=review_id))
def unblock(user_id): user = db_users.get_by_id(user_id) if not user: abort(404) db_users.unblock(user['id']) flash.success(gettext("This user account has been unblocked.")) return redirect(url_for('user.reviews', user_id=user['id']))
def delete(client_id): client = OAuthClient.query.get_or_404(client_id) if client.user != current_user: raise NotFound() client.delete() flash.success(gettext('You have deleted an application.')) return redirect(url_for('.index'))
def edit(id): review = get_review_or_404(id) if review["is_draft"] and current_user != review["user"]: raise NotFound(gettext("Can't find a review with the specified ID.")) if review["user"] != current_user: raise Unauthorized(gettext("Only author can edit this review.")) if review["is_hidden"] and not current_user.is_admin(): raise NotFound(gettext("Review has been hidden.")) form = ReviewEditForm(default_license_id=review["license_id"], default_language=review["language"]) if not review["is_draft"]: # Can't change license if review is published. del form.license_choice if form.validate_on_submit(): if review["is_draft"]: license_choice = form.license_choice.data else: license_choice = None if form.text.data == '': form.text.data = None try: db_review.update( review_id=review["id"], drafted=review["is_draft"], text=form.text.data, rating=form.rating.data, is_draft=(form.state.data == 'draft'), license_id=license_choice, language=form.language.data, ) except db_exceptions.BadDataException: raise BadRequest( lazy_gettext("Changing license of a published review\ or converting a published review back to drafts is not allowed." )) flash.success(gettext("Review has been updated.")) return redirect(url_for('.entity', id=review["id"])) else: form.text.data = review["text"] form.rating.data = review["rating"] if review["entity_type"] == 'release_group': spotify_mappings = mbspotify.mappings(str(review["entity_id"])) soundcloud_url = soundcloud.get_url(str(review["entity_id"])) return render_template('review/modify/edit.html', form=form, review=review, entity_type=review["entity_type"], entity=entity, spotify_mappings=spotify_mappings, soundcloud_url=soundcloud_url) return render_template('review/modify/edit.html', form=form, review=review, entity_type=review["entity_type"])
def spotify_confirm(): """Confirmation page for adding new Spotify mapping.""" release_group_id = request.args.get('release_group_id') if not release_group_id: raise BadRequest("Didn't provide `release_group_id`!") try: release_group = mb_release_group.get_release_group_by_id( release_group_id) except mb_exceptions.NoDataFoundException: flash.error( gettext("Only existing release groups can be mapped to Spotify!")) return redirect(url_for('search.index')) spotify_ref = request.args.get('spotify_ref', default=None) if not spotify_ref: flash.error(gettext("You need to select an album from Spotify!")) return redirect( url_for('.spotify_add', release_group_id=release_group_id)) try: spotify_id = parse_spotify_id(spotify_ref) except UnsupportedSpotifyReferenceTypeException: flash.error( gettext( "You need to specify a correct link to this album on Spotify!") ) return redirect( url_for('.spotify_add', release_group_id=release_group_id)) except Exception: raise BadRequest("Could not parse Spotify ID!") try: album = spotify_api.get_album(spotify_id) except ExternalServiceException: flash.error( gettext("You need to specify existing album from Spotify!")) return redirect( url_for('.spotify_add', release_group_id=release_group_id)) if request.method == 'POST': # TODO(roman): Check values that are returned by add_mapping (also take a look at related JS). res, error = mbspotify.add_mapping(release_group_id, 'spotify:album:%s' % spotify_id, current_user.id) if res: flash.success(gettext("Spotify mapping has been added!")) else: flash.error(gettext("Could not add Spotify mapping!")) current_app.logger.error( "Failed to create new Spotify mapping! Error: {}".format( error)) return redirect( url_for('.spotify_list', release_group_id=release_group_id)) return render_template('mapping/confirm.html', release_group=release_group, spotify_album=album)
def delete(id): review = get_review_or_404(id) if review["user"] != current_user and not current_user.is_admin(): raise Unauthorized(gettext("Only the author or an admin can delete this review.")) if request.method == 'POST': db_review.delete(review["id"]) flash.success(gettext("Review has been deleted.")) return redirect(url_for('user.reviews', user_id=current_user.id)) return render_template('review/delete.html', review=review)
def create(): form = CommentEditForm() if form.validate_on_submit(): comment = db_comment.create( review_id=form.review_id.data, user_id=current_user.id, text=form.text.data, ) flash.success('Comment has been saved!') return redirect(url_for('review.entity', id=comment['review_id']))
def spotify_report(): """Endpoint for reporting incorrect Spotify mappings. Shows confirmation page before submitting report to mbspotify. """ release_group_id = request.args.get('release_group_id') if not release_group_id: raise BadRequest("Didn't provide `release_group_id`!") spotify_id = request.args.get('spotify_id') if not spotify_id: raise BadRequest("Didn't provide `spotify_id`!") spotify_uri = "spotify:album:" + spotify_id # Checking if release group exists try: release_group = mb_release_group.get_release_group_by_id( release_group_id) except mb_exceptions.NoDataFoundException: raise NotFound("Can't find release group with a specified ID.") # Checking if release group is mapped to Spotify spotify_mappings = mbspotify.mappings(str(release_group_id)) if spotify_uri not in spotify_mappings: flash.error(gettext("This album is not mapped to Spotify yet!")) return redirect( url_for('.spotify_list', release_group_id=release_group_id)) if request.method == 'POST': res, error = mbspotify.vote(release_group_id, spotify_uri, current_user.id) if res: flash.success( gettext( "Incorrect Spotify mapping has been reported. Thank you!")) else: flash.error(gettext("Could not report incorrect Spotify mapping!")) current_app.logger.error( "Failed to report incorrect Spotify mapping! Error: {}".format( error)) return redirect( url_for('.spotify_list', release_group_id=release_group_id)) try: album = spotify_api.get_album(spotify_id) except ExternalServiceException: flash.error( gettext("You need to specify existing album from Spotify!")) return redirect( url_for('.spotify_list', release_group_id=release_group_id)) return render_template('mapping/report.html', release_group=release_group, spotify_album=album)
def delete(client_id): try: application = db_oauth_client.get_client(client_id) except db_exceptions.NoDataFoundException: raise NotFound() if str(application["user_id"]) != current_user.id: raise NotFound() db_oauth_client.delete(application["client_id"]) flash.success(gettext('You have deleted an application.')) return redirect(url_for('.index'))
def vote_delete(id): review = get_review_or_404(id) if review["is_hidden"] and not current_user.is_admin(): raise NotFound(gettext("Review has been hidden.")) try: vote = db_vote.get(user_id=current_user.id, revision_id=review["last_revision"]["id"]) flash.success(gettext("You have deleted your vote for this review!")) db_vote.delete(user_id=vote["user_id"], revision_id=vote["revision_id"]) except db_exceptions.NoDataFoundException: flash.error(gettext("This review is not rated yet.")) return redirect(url_for('.entity', id=id))
def create(): """Create application.""" form = ApplicationForm() if form.validate_on_submit(): OAuthClient.create(user=current_user, name=form.name.data, desc=form.desc.data, website=form.website.data, redirect_uri=form.redirect_uri.data) flash.success(gettext('You have created an application!')) return redirect(url_for('.index')) return render_template('profile/applications/create.html', form=form)
def delete(id): comment = get_comment_or_404(id) # if comment exists, review must exist review = get_review_or_404(comment["review_id"]) if comment["user"] != current_user: raise Unauthorized(gettext("Only the author can delete this comment.")) if request.method == 'POST': db_comment.delete(comment["id"]) flash.success(gettext("Comment has been deleted.")) return redirect(url_for('review.entity', id=comment["review_id"])) if comment: comment["text_html"] = markdown(comment["last_revision"]["text"], safe_mode="escape") return render_template('review/delete_comment.html', review=review, comment=comment)
def create(): """Create application.""" form = ApplicationForm() if form.validate_on_submit(): db_oauth_client.create( user_id=current_user.id, name=form.name.data, desc=form.desc.data, website=form.website.data, redirect_uri=form.redirect_uri.data, ) flash.success(gettext('You have created an application!')) return redirect(url_for('.index')) return render_template('profile/applications/create.html', form=form)
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 edit(): form = ProfileEditForm() if form.validate_on_submit(): db_users.update(current_user.id, user_new_info={ "display_name":form.display_name.data, "email":form.email.data, "show_gravatar":form.show_gravatar.data, }) flash.success(gettext("Profile updated.")) return redirect(url_for('user.reviews', user_id=current_user.id)) else: form.display_name.data = current_user.display_name form.email.data = current_user.email form.show_gravatar.data = current_user.show_gravatar return render_template('profile/edit.html', form=form)
def delete(id): comment = get_comment_or_404(id) # if comment exists, review must exist review = get_review_or_404(comment["review_id"]) if comment["user"] != current_user: raise Unauthorized(gettext("Only the author can delete this comment.")) if request.method == 'POST': db_comment.delete(comment["id"]) flash.success(gettext("Comment has been deleted.")) return redirect(url_for('review.entity', id=comment["review_id"])) if comment: comment["text_html"] = markdown(comment["last_revision"]["text"], safe_mode="escape") return render_template('review/delete_comment.html', review=review, comment=comment)
def edit(): form = ProfileEditForm() if form.validate_on_submit(): db_users.update(current_user.id, user_new_info={ "display_name": form.display_name.data, "email": form.email.data, "show_gravatar": form.show_gravatar.data, "license_choice": form.license_choice.data, }) flash.success(gettext("Profile updated.")) return redirect(url_for('user.reviews', user_id=current_user.id)) form.display_name.data = current_user.display_name form.email.data = current_user.email form.show_gravatar.data = current_user.show_gravatar form.license_choice.data = current_user.license_choice return render_template('profile/edit.html', form=form)
def rate(): form = RatingEditForm() if form.validate_on_submit(): if current_user.is_blocked: flash.error( gettext("You are not allowed to rate any entity because your " "account has been blocked by a moderator.")) return redirect( url_for('{}.entity'.format(form.entity_type.data), id=form.entity_id.data)) reviews, review_count = db_review.list_reviews( entity_id=form.entity_id.data, entity_type=form.entity_type.data, user_id=current_user.id, ) review = reviews[0] if review_count else None if not review and form.rating.data is None: raise BadRequest( "Cannot create a review with no rating and no text!") if not review and form.rating.data is not None: db_review.create( user_id=current_user.id, entity_id=form.entity_id.data, entity_type=form.entity_type.data, rating=form.rating.data, is_draft=False, ) elif review and review['text'] is None and form.rating.data is None: db_review.delete(review['id']) elif review and review['rating'] != form.rating.data: db_review.update( review_id=review['id'], drafted=review['is_draft'], text=review['text'], rating=form.rating.data, ) # TODO(code-master5): Make this message specify the entity flash.success("We have updated your rating for this entity!") else: flash.error("Error! Could not update the rating...") return redirect( url_for('{}.entity'.format(form.entity_type.data), id=form.entity_id.data))
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 edit(client_id): application = OAuthClient.query.get_or_404(client_id) if application.user != current_user: raise NotFound() form = ApplicationForm() if form.validate_on_submit(): application.update(name=form.name.data, desc=form.desc.data, website=form.website.data, redirect_uri=form.redirect_uri.data) flash.success(gettext("You have updated an application!")) return redirect(url_for('.index')) else: form.name.data = application.name form.desc.data = application.desc form.website.data = application.website form.redirect_uri.data = application.redirect_uri return render_template('profile/applications/edit.html', form=form)
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 spotify_report(): """Endpoint for reporting incorrect Spotify mappings. Shows confirmation page before submitting report to mbspotify. """ release_group_id = request.args.get('release_group_id') spotify_id = request.args.get('spotify_id') spotify_uri = "spotify:album:" + spotify_id # Checking if release group exists try: release_group = mb_release_group.get_release_group_by_id( release_group_id) except mb_exceptions.NoDataFoundException: flash.error(gettext("Can't find release group with that ID!")) return redirect( url_for('.spotify_list', release_group_id=release_group_id)) # Checking if release group is mapped to Spotify spotify_mappings = mbspotify.mappings(str(release_group_id)) if spotify_uri not in spotify_mappings: flash.error(gettext("This album is not mapped to Spotify yet!")) return redirect( url_for('.spotify_list', release_group_id=release_group_id)) if request.method == 'POST': mbspotify.vote(release_group_id, spotify_uri, current_user.id) flash.success( gettext("Incorrect Spotify mapping has been reported. Thank you!")) return redirect( url_for('.spotify_list', release_group_id=release_group_id)) try: album = spotify_api.get_album(spotify_id) except ExternalServiceException: flash.error( gettext("You need to specify existing album from Spotify!")) return redirect( url_for('.spotify_list', release_group_id=release_group_id)) return render_template('mapping/report.html', release_group=release_group, spotify_album=album)
def spotify_report(): """Endpoint for reporting incorrect Spotify mappings. Shows confirmation page before submitting report to mbspotify. """ release_group_id = request.args.get('release_group_id') if not release_group_id: raise BadRequest("Didn't provide `release_group_id`!") spotify_id = request.args.get('spotify_id') if not spotify_id: raise BadRequest("Didn't provide `spotify_id`!") spotify_uri = "spotify:album:" + spotify_id # Checking if release group exists try: release_group = mb_release_group.get_release_group_by_id(release_group_id) except mb_exceptions.NoDataFoundException: raise NotFound("Can't find release group with a specified ID.") # Checking if release group is mapped to Spotify spotify_mappings = mbspotify.mappings(str(release_group_id)) if spotify_uri not in spotify_mappings: flash.error(gettext("This album is not mapped to Spotify yet!")) return redirect(url_for('.spotify_list', release_group_id=release_group_id)) if request.method == 'POST': res, error = mbspotify.vote(release_group_id, spotify_uri, current_user.id) if res: flash.success(gettext("Incorrect Spotify mapping has been reported. Thank you!")) else: flash.error(gettext("Could not report incorrect Spotify mapping!")) current_app.logger.error("Failed to report incorrect Spotify mapping! Error: {}".format(error)) return redirect(url_for('.spotify_list', release_group_id=release_group_id)) try: album = spotify_api.get_album(spotify_id) except ExternalServiceException: flash.error(gettext("You need to specify existing album from Spotify!")) return redirect(url_for('.spotify_list', release_group_id=release_group_id)) return render_template('mapping/report.html', release_group=release_group, spotify_album=album)
def spotify_confirm(): """Confirmation page for adding new Spotify mapping.""" release_group_id = request.args.get('release_group_id') if not release_group_id: raise BadRequest("Didn't provide `release_group_id`!") release_group = musicbrainz.get_release_group_by_id(release_group_id) if not release_group: flash.error( gettext("Only existing release groups can be mapped to Spotify!")) return redirect(url_for('search.index')) spotify_ref = request.args.get('spotify_ref', default=None) if not spotify_ref: flash.error(gettext("You need to select an album from Spotify!")) return redirect(url_for('.spotify', release_group_id=release_group_id)) spotify_id = parse_spotify_id(spotify_ref) if not spotify_id: flash.error( gettext( "You need to specify a correct link to this album on Spotify!") ) return redirect(url_for('.spotify', release_group_id=release_group_id)) try: album = spotify_api.get_album(spotify_id) except ExternalServiceException: flash.error( gettext("You need to specify existing album from Spotify!")) return redirect(url_for('.spotify', release_group_id=release_group_id)) if request.method == 'POST': # TODO(roman): Check values that are returned by add_mapping (also take a look at related JS). mbspotify.add_mapping(release_group_id, 'spotify:album:%s' % spotify_id, current_user.id) flash.success(gettext("Spotify mapping has been added!")) return redirect( url_for('.spotify_list', release_group_id=release_group_id)) return render_template('mapping/confirm.html', release_group=release_group, spotify_album=album)
def create(): # TODO (code-master5): comment limit, revision and drafts, edit functionality form = CommentEditForm() if form.validate_on_submit(): get_review_or_404(form.review_id.data) if current_user.is_blocked: flash.error(gettext("You are not allowed to write new comments because your " "account has been blocked by a moderator.")) return redirect(url_for('review.entity', id=form.review_id.data)) # should be able to comment only if review exists db_comment.create( review_id=form.review_id.data, user_id=current_user.id, text=form.text.data, ) flash.success(gettext("Comment has been saved!")) elif not form.text.data: # comment must have some text flash.error(gettext("Comment must not be empty!")) return redirect(url_for('review.entity', id=form.review_id.data))
def spotify_confirm(): """Confirmation page for adding new Spotify mapping.""" release_group_id = request.args.get('release_group_id') if not release_group_id: raise BadRequest("Didn't provide `release_group_id`!") try: release_group = mb_release_group.get_release_group_by_id(release_group_id) except mb_exceptions.NoDataFoundException: flash.error(gettext("Only existing release groups can be mapped to Spotify!")) return redirect(url_for('search.index')) spotify_ref = request.args.get('spotify_ref', default=None) if not spotify_ref: flash.error(gettext("You need to select an album from Spotify!")) return redirect(url_for('.spotify_add', release_group_id=release_group_id)) try: spotify_id = parse_spotify_id(spotify_ref) except UnsupportedSpotifyReferenceTypeException: flash.error(gettext("You need to specify a correct link to this album on Spotify!")) return redirect(url_for('.spotify_add', release_group_id=release_group_id)) except Exception: raise BadRequest("Could not parse Spotify ID!") try: album = spotify_api.get_album(spotify_id) except ExternalServiceException: flash.error(gettext("You need to specify existing album from Spotify!")) return redirect(url_for('.spotify_add', release_group_id=release_group_id)) if request.method == 'POST': # TODO(roman): Check values that are returned by add_mapping (also take a look at related JS). res, error = mbspotify.add_mapping(release_group_id, 'spotify:album:%s' % spotify_id, current_user.id) if res: flash.success(gettext("Spotify mapping has been added!")) else: flash.error(gettext("Could not add Spotify mapping!")) current_app.logger.error("Failed to create new Spotify mapping! Error: {}".format(error)) return redirect(url_for('.spotify_list', release_group_id=release_group_id)) return render_template('mapping/confirm.html', release_group=release_group, spotify_album=album)
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 rate(): form = RatingEditForm() if form.validate_on_submit(): if current_user.is_blocked: flash.error(gettext("You are not allowed to rate any entity because your " "account has been blocked by a moderator.")) return redirect(url_for('{}.entity'.format(form.entity_type.data), id=form.entity_id.data)) reviews, review_count = db_review.list_reviews( entity_id=form.entity_id.data, entity_type=form.entity_type.data, user_id=current_user.id, ) review = reviews[0] if review_count else None if not review and form.rating.data is None: raise BadRequest("Cannot create a review with no rating and no text!") if not review and form.rating.data is not None: db_review.create( user_id=current_user.id, entity_id=form.entity_id.data, entity_type=form.entity_type.data, rating=form.rating.data, is_draft=False, ) elif review and review['text'] is None and form.rating.data is None: db_review.delete(review['id']) elif review and review['rating'] != form.rating.data: db_review.update( review_id=review['id'], drafted=review['is_draft'], text=review['text'], rating=form.rating.data, ) # TODO(code-master5): Make this message specify the entity flash.success("We have updated your rating for this entity!") else: flash.error("Error! Could not update the rating...") return redirect(url_for('{}.entity'.format(form.entity_type.data), id=form.entity_id.data))
def create(): # TODO (code-master5): comment limit, revision and drafts, edit functionality form = CommentEditForm() if form.validate_on_submit(): get_review_or_404(form.review_id.data) if current_user.is_blocked: flash.error( gettext( "You are not allowed to write new comments because your " "account has been blocked by a moderator.")) return redirect(url_for('review.entity', id=form.review_id.data)) # should be able to comment only if review exists db_comment.create( review_id=form.review_id.data, user_id=current_user.id, text=form.text.data, ) flash.success(gettext("Comment has been saved!")) elif not form.text.data: # comment must have some text flash.error(gettext("Comment must not be empty!")) return redirect(url_for('review.entity', id=form.review_id.data))
def edit(id): comment = get_comment_or_404(id) if comment["user"] != current_user: raise Unauthorized(gettext("Only the author can edit this comment.")) if current_user.is_blocked: flash.error( gettext("You are not allowed to edit comments because your " "account has been blocked by a moderator.")) return redirect(url_for('review.entity', id=comment["review_id"])) form = CommentEditForm() if form.validate_on_submit(): if form.text.data != comment["last_revision"]["text"]: db_comment.update(comment_id=comment["id"], text=form.text.data) flash.success(gettext("Comment has been updated.")) else: flash.error( gettext( "You must change some content of the comment to update it!" )) elif not form.text.data: # comment must have some text flash.error(gettext("Comment must not be empty!")) return redirect(url_for('review.entity', id=comment["review_id"]))
def edit(client_id): try: application = db_oauth_client.get_client(client_id) except db_exceptions.NoDataFoundException: raise NotFound() if str(application["user_id"]) != current_user.id: raise NotFound() form = ApplicationForm() if form.validate_on_submit(): db_oauth_client.update( client_id=application["client_id"], name=form.name.data, desc=form.desc.data, website=form.website.data, redirect_uri=form.redirect_uri.data, ) flash.success(gettext("You have updated an application!")) return redirect(url_for('.index')) form.name.data = application["name"] form.desc.data = application["desc"] form.website.data = application["website"] form.redirect_uri.data = application["redirect_uri"] return render_template('profile/applications/edit.html', form=form)
def unblock(user_id): user = User.query.get_or_404(str(user_id)) user.unblock() flash.success(gettext("This user account has been unblocked.")) return redirect(url_for('user.reviews', user_id=user.id))
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)
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 form.remember_license.data: db_users.update(current_user.id, user_new_info={ "license_choice": form.license_choice.data, }) 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'])) try: entity = get_entity_by_id(entity_id, entity_type) except NoDataFoundException: raise NotFound( 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!"
def unhide(id): review = get_review_or_404(id) db_review.set_hidden_state(review["id"], is_hidden=False) flash.success(gettext("Review is not hidden anymore.")) return redirect(request.referrer or url_for('user.reviews', user_id=current_user.id))