Пример #1
0
def view_submit(league_id, submission_period_id):
    league = select_league(league_id)
    if league.version == 2:
        return redirect(
            url_for('view_submit_2',
                    league_id=league_id,
                    submission_period_id=submission_period_id))

    submission_period = next(
        (sp
         for sp in league.submission_periods if sp.id == submission_period_id),
        None)
    if not league.has_user(g.user):
        return redirect(url_for('view_league', league_id=league.id))

    # Only allow submissions for the current round
    if (league.current_submission_period is None
            or league.current_submission_period.id != submission_period_id):
        return redirect(url_for('view_league', league_id=league.id))

    if not submission_period.accepting_submissions:
        return redirect(url_for('view_league', league_id=league.id))

    my_submission = get_my_submission(g.user, submission_period)

    return {
        'user': g.user,
        'league': league,
        'round': submission_period,
        'my_submission': my_submission,
        'access_token': session['access_token'],
    }
Пример #2
0
def view_vote(league_id, submission_period_id):
    league = select_league(league_id)
    submission_period = next(
        (sp
         for sp in league.submission_periods if sp.id == submission_period_id),
        None)
    if not league.has_user(g.user):
        return redirect(url_for('view_league', league_id=league.id))

    if (league.current_submission_period is None
            or league.current_submission_period.id != submission_period_id):
        return redirect(url_for('view_league', league_id=league.id))

    if not submission_period.accepting_votes:
        return redirect(url_for('view_league', league_id=league.id))

    my_submission = get_my_submission(g.user, submission_period)

    # If this user didn't submit for this round, don't allow them to vote
    if not my_submission:
        return redirect(url_for('view_league', league_id=league.id))

    # If this user already voted for this round, don't allow them to vote
    if get_my_vote(g.user, submission_period):
        return redirect(url_for('view_league', league_id=league.id))

    # my_vote = get_my_vote(g.user, submission_period)
    my_vote = None

    tracks = []
    if submission_period.all_tracks:
        tracks = g.spotify.tracks(submission_period.all_tracks).get('tracks')

    tracks_by_uri = OrderedDict()
    for track in tracks:
        if track:
            tracks_by_uri[track.get('uri')] = track

    # Remove user's own submitted songs from tracks shown on page
    if my_submission:
        for uri in my_submission.tracks:
            tracks_by_uri.pop(uri, None)

    return {
        'user': g.user,
        'league': league,
        'round': submission_period,
        'tracks_by_uri': tracks_by_uri,
        'my_vote': my_vote,
        'access_token': session['access_token'],
    }
Пример #3
0
def view_submit(league_id, submission_period_id):
    league = select_league(league_id)
    submission_period = next(
        (sp
         for sp in league.submission_periods if sp.id == submission_period_id),
        None)
    if not league.has_user(g.user):
        return redirect(url_for('view_league', league_id=league.id))

    if not submission_period.accepting_submissions:
        return redirect(url_for('view_league', league_id=league.id))

    my_submission = get_my_submission(g.user, submission_period)

    return {
        'user': g.user,
        'league': league,
        'round': submission_period,
        'my_submission': my_submission,
        'access_token': session['access_token'],
    }
Пример #4
0
def vote(league_id, submission_period_id):
    try:
        league = select_league(league_id)
        submission_period = next((sp for sp in league.submission_periods
                                  if sp.id == submission_period_id), None)

        if not league or not submission_period:
            return "No submission period or league", httplib.INTERNAL_SERVER_ERROR

        if not league.has_user(g.user):
            return "Not a member of this league", httplib.UNAUTHORIZED

        # If this user didn't submit for this round, don't allow them to vote
        if not get_my_submission(g.user, submission_period):
            return redirect(url_for('view_league', league_id=league_id))

        # If this user already voted for this round, don't allow them to vote
        if get_my_vote(g.user, submission_period):
            return redirect(url_for('view_league', league_id=league_id))

        # If this round is no longer accepting votes, redirect
        if not submission_period.accepting_votes:
            return redirect(request.referrer)

        try:
            votes = json.loads(request.form.get('votes'))
            comments = json.loads(request.form.get('comments'))
        except Exception:
            app.logger.exception(
                "Failed to load JSON from form with votes: %s", request.form)
            return 'There was an error processing votes', 500

        # Remove all unnecessary zero-values
        votes = {k: v for k, v in votes.iteritems() if v}
        comments = {k: v for k, v in comments.iteritems() if v}

        # Process votes
        vote = create_or_update_vote(votes, comments, submission_period,
                                     league, g.user)

        # If someone besides owner is voting, notify the owner
        if not league.has_owner(g.user):
            owner_user_voted_notification(vote)

        remaining = submission_period.have_not_voted
        if not remaining:
            complete_submission_period.delay(submission_period.id)

        elif vote.count < 2 and len(remaining) == 1:
            last_user = remaining[0]
            user_last_to_vote_notification(last_user, submission_period)

        track_user_voted(g.user.id, submission_period)
        if comments:
            track_user_voted_with_comments(g.user.id, submission_period,
                                           len(comments))

        return redirect(
            url_for('view_submission_period',
                    league_id=league_id,
                    submission_period_id=submission_period_id))

    except Exception:
        app.logger.exception('Failed to process votes',
                             extra={
                                 'user': g.user.id,
                                 'league': league.id,
                                 'round': submission_period_id
                             })
Пример #5
0
def submit(league_id, submission_period_id):
    try:
        league = select_league(league_id)
        submission_period = next((sp for sp in league.submission_periods
                                  if sp.id == submission_period_id), None)
        if not league or not submission_period:
            return "No submission period or league", httplib.INTERNAL_SERVER_ERROR

        if not league.has_user(g.user):
            return "Not a member of this league", httplib.UNAUTHORIZED

        if not submission_period.accepting_submissions:
            return redirect(request.referrer)

        try:
            tracks = json.loads(request.form.get('songs'))
            warned_artists = json.loads(
                request.form.get('duplicate-artists') or '[]')
            warned_repeats = json.loads(
                request.form.get('repeat-submissions') or '[]')
        except Exception:
            app.logger.exception(
                "Failed to load JSON from form with submit: %s", request.form)
            return 'There was an error processing your submission', 500

        if len(filter(None, tracks)) != len(tracks):
            return redirect(request.referrer)

        # Don't allow user to submit duplicate tracks
        if len(tracks) != len(set(tracks)):
            return redirect(request.referrer)

        # Don't include user's own previous submission when checking duplicates
        my_submission = get_my_submission(g.user, submission_period)
        their_tracks = []
        if submission_period.all_tracks:
            their_tracks = set(submission_period.all_tracks)
            if my_submission is not None:
                their_tracks.difference_update(set(my_submission.tracks))
            their_tracks = list(their_tracks)

        s_tracks = tracks + their_tracks
        s_tracks = g.spotify.tracks(s_tracks).get('tracks')

        my_tracks, their_tracks = s_tracks, []
        if len(s_tracks) > len(tracks):
            my_tracks = s_tracks[:len(tracks)]
            their_tracks = s_tracks[len(tracks):]

        # Don't allow user to submit already submitted track, album or artist
        duplicate_tracks = check_duplicate_tracks(my_tracks, their_tracks)
        duplicate_artists = check_duplicate_artists(my_tracks, their_tracks)
        repeat_submissions = check_repeat_submissions(g.user.id, tracks,
                                                      league_id)

        proceeding_dups = set(warned_artists).intersection(
            set(duplicate_artists))
        if proceeding_dups:
            duplicate_artists = list(
                set(duplicate_artists) - set(warned_artists))
            track_user_proceeded_duplicate_artist(g.user.id, submission_period,
                                                  list(proceeding_dups))

        proceeding_repeats = set(warned_repeats).intersection(
            set(repeat_submissions))
        if proceeding_repeats:
            repeat_submissions = list(
                set(repeat_submissions) - set(warned_repeats))
            track_user_proceeded_repeat_submission(g.user.id,
                                                   submission_period,
                                                   list(proceeding_repeats))

        if duplicate_tracks or duplicate_artists or repeat_submissions:

            if duplicate_tracks:
                track_user_submitted_duplicate_song(g.user.id,
                                                    submission_period,
                                                    duplicate_tracks)
            # elif duplicate_albums:
            #     track_user_submitted_duplicate_album(g.user.id, submission_period, duplicate_albums)
            elif duplicate_artists:
                track_user_submitted_duplicate_artist(g.user.id,
                                                      submission_period,
                                                      duplicate_artists)
            elif repeat_submissions:
                track_user_submitted_repeat_submission(g.user.id,
                                                       submission_period,
                                                       repeat_submissions)

            return render_template('submit/page.html',
                                   user=g.user,
                                   league=league,
                                   round=submission_period,
                                   previous_tracks=tracks,
                                   duplicate_songs=duplicate_tracks,
                                   duplicate_albums=[],
                                   duplicate_artists=duplicate_artists,
                                   repeat_submissions=repeat_submissions,
                                   access_token=session['access_token'])

        # Create a new submission on the round as current user
        submission = create_or_update_submission(tracks, submission_period,
                                                 league, g.user)

        # If someone besides owner is submitting, notify the owner
        if not league.has_owner(g.user):
            owner_user_submitted_notification(submission)

        remaining = submission_period.have_not_submitted
        if not remaining:
            app.logger.warning('No remaining members to submit',
                               extra={'round': submission_period_id})
            # If this is not the first round, roll forward
            if len(league.submission_periods) > 1:
                app.logger.warning('This is not the only round',
                                   extra={'round': submission_period_id})
                if league.submission_periods[0].id != submission_period_id:
                    # This makes the request a little heavy for the final submitter,
                    # but asyncing means the submitter gets a playlist button but no playlist.
                    app.logger.warning(
                        'This is not the first round, completing the submission process',
                        extra={'round': submission_period_id})
                    complete_submission_process(submission_period.id)

        # Don't send submission reminder if this user is resubmitting. In this
        # case, the last user to submit will have already gotten a notification.
        elif submission.count < 2 and len(remaining) == 1:
            last_user = remaining[0]
            user_last_to_submit_notification(last_user, submission_period)

        track_user_submitted(g.user.id, submission_period)

        return redirect(url_for('view_league', league_id=league_id))

    except Exception:
        app.logger.exception('Failed to process submissions',
                             extra={
                                 'user': g.user.id,
                                 'league': league_id,
                                 'round': submission_period_id,
                                 'form_values': request.form
                             })

        return 'There was an error processing your submission', 500