Example #1
0
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)
Example #2
0
                                  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!"
            ))
        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)