def entity(id): id = str(id) try: place = mb_place.get_place_by_id(id) except mb_exceptions.NoDataFoundException: raise NotFound(gettext("Sorry, we couldn't find a place with that MusicBrainz ID.")) if current_user.is_authenticated: my_reviews, _ = db_review.list_reviews( entity_id=place['id'], entity_type='place', user_id=current_user.id ) my_review = my_reviews[0] if my_reviews else None else: my_review = None rating_form = RatingEditForm(entity_id=id, entity_type='place') rating_form.rating.data = my_review['rating'] if my_review else None limit = int(request.args.get('limit', default=10)) offset = int(request.args.get('offset', default=0)) reviews, count = db_review.list_reviews( entity_id=place['id'], entity_type='place', sort='popularity', limit=limit, offset=offset, ) avg_rating = get_avg_rating(place['id'], "place") return render_template('place/entity.html', id=place['id'], place=place, reviews=reviews, rating_form=rating_form, my_review=my_review, limit=limit, offset=offset, count=count, avg_rating=avg_rating, current_user=current_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 entity(id): id = str(id) try: event = mb_event.get_event_by_id(id) except mb_exceptions.NoDataFoundException: raise NotFound( gettext( "Sorry, we couldn't find an event with that MusicBrainz ID.")) if 'url-rels' in event: external_reviews = list( filter(lambda rel: rel['type'] == 'review', event['url-rels'])) else: external_reviews = [] if 'artist-rels' in event and event['artist-rels']: artists_unique = [] for artist in event['artist-rels']: if artist not in artists_unique: artists_unique.append(artist) artists_sorted = sorted(artists_unique, key=itemgetter('type')) event['artists_grouped'] = groupby(artists_sorted, itemgetter('type')) if current_user.is_authenticated: my_reviews, _ = db_review.list_reviews(entity_id=event['id'], entity_type='event', user_id=current_user.id) my_review = my_reviews[0] if my_reviews else None else: my_review = None rating_form = RatingEditForm(entity_id=id, entity_type='event') rating_form.rating.data = my_review['rating'] if my_review else None limit = int(request.args.get('limit', default=10)) offset = int(request.args.get('offset', default=0)) reviews, count = db_review.list_reviews(entity_id=event['id'], entity_type='event', sort='popularity', limit=limit, offset=offset) avg_rating = get_avg_rating(event['id'], "event") return render_template('event/entity.html', id=event['id'], event=event, reviews=reviews, rating_form=rating_form, my_review=my_review, external_reviews=external_reviews, limit=limit, offset=offset, count=count, avg_rating=avg_rating, current_user=current_user)
def entity(id): id = str(id) try: release_group = mb_release_group.get_release_group_by_id(id) except mb_exceptions.NoDataFoundException: raise NotFound(gettext("Sorry, we couldn't find a release group with that MusicBrainz ID.")) if 'url-rels' in release_group: external_reviews = list(filter(lambda rel: rel['type'] == 'review', release_group['url-rels'])) else: external_reviews = [] if 'tag-list' in release_group: tags = release_group['tag-list'] else: tags = None if 'release-list' in release_group and release_group['release-list']: release = mb_release.get_release_by_id(release_group['release-list'][0]['id']) else: release = None soundcloud_url = soundcloud.get_url(release_group['id']) if soundcloud_url: spotify_mappings = None else: spotify_mappings = mbspotify.mappings(release_group['id']) limit = int(request.args.get('limit', default=10)) offset = int(request.args.get('offset', default=0)) if current_user.is_authenticated: my_reviews, my_count = db_review.list_reviews( entity_id=release_group['id'], entity_type='release_group', user_id=current_user.id, ) my_review = my_reviews[0] if my_count else None else: my_review = None reviews, count = db_review.list_reviews( entity_id=release_group['id'], entity_type='release_group', sort='popularity', limit=limit, offset=offset, ) avg_rating = get_avg_rating(release_group['id'], "release_group") rating_form = RatingEditForm(entity_id=id, entity_type='release_group') rating_form.rating.data = my_review['rating'] if my_review else None return render_template('release_group/entity.html', id=release_group['id'], release_group=release_group, reviews=reviews, release=release, my_review=my_review, spotify_mappings=spotify_mappings, tags=tags, soundcloud_url=soundcloud_url, external_reviews=external_reviews, limit=limit, offset=offset, count=count, avg_rating=avg_rating, rating_form=rating_form, current_user=current_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 entity(id): id = str(id) try: recording = mb_recording.get_recording_by_id(id) except mb_exceptions.NoDataFoundException: raise NotFound( gettext( "Sorry, we couldn't find a recording with that MusicBrainz ID." )) if 'url-rels' in recording: external_reviews = list( filter(lambda rel: rel['type'] == 'review', recording['url-rels'])) else: external_reviews = [] limit = int(request.args.get('limit', default=RECORDING_REVIEWS_LIMIT)) offset = int(request.args.get('offset', default=0)) if current_user.is_authenticated: my_reviews, my_count = db_review.list_reviews( entity_id=recording['id'], entity_type='recording', user_id=current_user.id, ) my_review = my_reviews[0] if my_count else None else: my_review = None reviews, count = db_review.list_reviews( entity_id=recording['id'], entity_type='recording', sort='popularity', limit=limit, offset=offset, ) avg_rating = get_avg_rating(recording['id'], "recording") rating_form = RatingEditForm(entity_id=id, entity_type='recording') rating_form.rating.data = my_review['rating'] if my_review else None return render_template('recording/entity.html', id=recording['id'], recording=recording, reviews=reviews, my_review=my_review, external_reviews=external_reviews, limit=limit, offset=offset, count=count, avg_rating=avg_rating, rating_form=rating_form, current_user=current_user)
def entity(id): id = str(id) try: work = mb_work.get_work_by_id(id) except mb_exceptions.NoDataFoundException: raise NotFound(gettext("Sorry, we couldn't find a work with that MusicBrainz ID.")) work_reviews_limit = WORK_REVIEWS_LIMIT if request.args.get('reviews') == "all": work_reviews_limit = None if current_user.is_authenticated: my_reviews, _ = db_review.list_reviews( entity_id=work['id'], entity_type='work', user_id=current_user.id, ) my_review = my_reviews[0] if my_reviews else None else: my_review = None reviews_offset = 0 reviews, reviews_count = db_review.list_reviews( entity_id=work['id'], entity_type='work', sort='popularity', limit=work_reviews_limit, offset=reviews_offset, ) page = int(request.args.get('page', default=1)) if page < 1: return redirect(url_for('.reviews')) recording_rels = work['recording-rels'] recording_count = len(recording_rels) recording_offset = (page - 1) * BROWSE_RECORDING_LIMIT recording_rels = recording_rels[recording_offset:recording_offset + BROWSE_RECORDING_LIMIT] avg_rating = get_avg_rating(work['id'], "work") rating_form = RatingEditForm(entity_id=id, entity_type='work') rating_form.rating.data = my_review['rating'] if my_review else None return render_template('work/entity.html', id=work['id'], work=work, page=page, reviews=reviews, my_review=my_review, reviews_limit=work_reviews_limit, reviews_count=reviews_count, avg_rating=avg_rating, rating_form=rating_form, recording_limit=BROWSE_RECORDING_LIMIT, recording_rels=recording_rels, recording_count=recording_count, current_user=current_user)
def entity(id): id = str(id) try: label = mb_label.get_label_by_id(id) except mb_exceptions.NoDataFoundException: raise NotFound( gettext( "Sorry, we couldn't find a label with that MusicBrainz ID.")) label_reviews_limit = LABEL_REVIEWS_LIMIT if request.args.get('reviews') == "all": label_reviews_limit = None if current_user.is_authenticated: my_reviews, my_count = db_review.list_reviews( entity_id=label['id'], entity_type='label', user_id=current_user.id, ) my_review = my_reviews[0] if my_count else None else: my_review = None reviews_offset = 0 reviews, reviews_count = db_review.list_reviews( entity_id=label['id'], entity_type='label', sort='popularity', limit=label_reviews_limit, offset=reviews_offset, ) avg_rating = get_avg_rating(label['id'], "label") rating_form = RatingEditForm(entity_id=id, entity_type='label') rating_form.rating.data = my_review['rating'] if my_review else None return render_template('label/entity.html', id=label['id'], label=label, reviews=reviews, my_review=my_review, reviews_limit=label_reviews_limit, reviews_count=reviews_count, avg_rating=avg_rating, rating_form=rating_form, current_user=current_user)
def entity(id): """Artist page. Displays release groups (split up into several sections depending on their type), artist information (type, members/member of, external links). """ try: artist = mb_artist.get_artist_by_id(str(id)) except mb_exceptions.NoDataFoundException: raise NotFound( gettext( "Sorry, we couldn't find an artist with that MusicBrainz ID.")) # Note that some artists might not have a list of members because they are not a band band_members = _get_band_members(artist) artist_reviews_limit = ARTIST_REVIEWS_LIMIT if request.args.get('reviews') == "all": artist_reviews_limit = None my_review = None if current_user.is_authenticated: my_reviews, my_count = db_review.list_reviews( entity_id=artist['id'], entity_type='artist', user_id=current_user.id, ) my_review = my_reviews[0] if my_count else None reviews_offset = 0 reviews, reviews_count = db_review.list_reviews( entity_id=artist['id'], entity_type='artist', sort='popularity', limit=artist_reviews_limit, offset=reviews_offset, ) avg_rating = get_avg_rating(artist['id'], "artist") rating_form = RatingEditForm(entity_id=id, entity_type='artist') rating_form.rating.data = my_review['rating'] if my_review else None release_type = request.args.get('release_type', default='album') if release_type not in ['album', 'single', 'ep', 'broadcast', 'other']: # supported release types raise BadRequest("Unsupported release type.") page = int(request.args.get('page', default=1)) if page < 1: return redirect(url_for('.reviews')) release_groups_offset = (page - 1) * BROWSE_RELEASE_GROUPS_LIMIT release_groups, release_group_count = mb_release_group.browse_release_groups( artist_id=artist['id'], release_types=[release_type], limit=BROWSE_RELEASE_GROUPS_LIMIT, offset=release_groups_offset, ) for release_group in release_groups: # TODO(roman): Count reviews instead of fetching them. _, release_group_review_count = db_review.list_reviews( # pylint: disable=unused-variable entity_id=release_group['id'], entity_type='release_group', sort='published_on', limit=1, ) release_group['review_count'] = release_group_review_count return render_template( 'artist/entity.html', id=artist['id'], artist=artist, release_type=release_type, release_groups=release_groups, page=page, reviews=reviews, reviews_limit=artist_reviews_limit, reviews_count=reviews_count, avg_rating=avg_rating, rating_form=rating_form, release_groups_limit=BROWSE_RELEASE_GROUPS_LIMIT, release_group_count=release_group_count, band_members=band_members, )