예제 #1
0
    def test_none_existing(self):
        self.assertIsNone(get_user(self.id))

        created = create_or_update_user(self.id, self.name, self.email,
                                        self.image_url)

        saved = get_user(self.id)
        self.assertIsNotNone(saved)
        self.assertEqual(created.name, saved.name)
        self.assertEqual(created.email, saved.email)
        self.assertEqual(created.image_url, saved.image_url)
        self.assertEqual(created.preferences, saved.preferences)
예제 #2
0
def post_create_league():
    name = request.form.get('league-name')
    num_tracks = request.form.get('tracks-submitted')
    upvote_size = request.form.get('point-bank-size')
    limit_upvotes = request.form.get('limit-upvotes')
    max_up_per_song = request.form.get('max-points-per-song')
    allow_downvotes = request.form.get('allow-downvotes')
    downvote_size = request.form.get('downvote-bank-size')
    limit_downvotes = request.form.get('limit-downvotes')
    max_down_per_song = request.form.get('max-downvotes-per-song')

    user_ids = json.loads(request.form.get('added-members', []))
    members = [get_user(uid) for uid in user_ids]

    emails = json.loads(request.form.get('invited-members', []))

    rounds = json.loads(request.form.get('added-rounds', []))

    league = create_league(g.user, name=name, users=members)
    league.preferences.track_count = int(num_tracks)
    league.preferences.point_bank_size = int(upvote_size)

    league.preferences.max_points_per_song = 0
    if limit_upvotes == 'yes':
        league.preferences.max_points_per_song = int(max_up_per_song or 0)

    league.preferences.downvote_bank_size = 0
    league.preferences.max_downvotes_per_song = 0
    if allow_downvotes == 'yes':
        league.preferences.downvote_bank_size = int(downvote_size or 0)
        if limit_downvotes == 'yes':
            league.preferences.max_downvotes_per_song = int(max_down_per_song
                                                            or 0)
    upsert_league_preferences(league)

    for email in emails:
        add_user(league, email, notify=True)

    for new_round in rounds:
        submission_due_date_str = new_round['submission-due-date-utc']
        submission_due_date = utc.localize(
            datetime.strptime(submission_due_date_str, '%m/%d/%y %I%p'))

        vote_due_date_str = new_round['voting-due-date-utc']
        vote_due_date = utc.localize(
            datetime.strptime(vote_due_date_str, '%m/%d/%y %I%p'))

        create_submission_period(league, new_round['name'],
                                 new_round['description'], submission_due_date,
                                 vote_due_date)

    track_user_created_league(g.user.id, league)
    app.logger.info('User created league',
                    extra={
                        'league': league.id,
                        'user': g.user.id
                    })

    return redirect(url_for('view_league', league_id=league.id))
예제 #3
0
    def test_existing(self):
        User(id=self.id,
             name=self.name,
             email=self.email,
             image_url=self.image_url,
             joined=datetime.utcnow())

        user = get_user(self.id)
        self.assertIsNotNone(user)
        self.assertEqual(self.name, user.name)
        self.assertEqual(self.email, user.email)
        self.assertEqual(self.image_url, user.image_url)
예제 #4
0
    def test_create_user_and_save(self):
        created = create_user(self.id, self.name, self.email, self.image_url)
        self.assertIsNotNone(created)
        self.assertEqual(self.name, created.name)
        self.assertEqual(self.email, created.email)
        self.assertEqual(self.image_url, created.image_url)

        saved = get_user(created.id)
        self.assertIsNotNone(saved)
        self.assertEqual(created.name, saved.name)
        self.assertEqual(created.email, saved.email)
        self.assertEqual(created.image_url, saved.image_url)
        self.assertEqual(created.preferences, saved.preferences)
예제 #5
0
def login():
    # If no current login, send user through Spotify OAuth process.
    # If current login, send user to his/her profile.
    if 'current_user' not in session:
        url = request.url
        oauth = get_spotify_oauth()
        code = oauth.parse_response_code(url)
        if code:
            token_info = oauth.get_access_token(code)
            access_token = token_info['access_token']
            refresh_token = token_info['refresh_token']
            expires_at = int(token_info['expires_at'])

            spotify = Spotify(access_token)
            spotify_user = spotify.current_user()
            user_id = spotify_user.get('id')

            user = get_user(user_id)

            # If user logging in w/ Spotify does not yet exist, create it
            if not user:
                user = create_user_from_spotify_user(spotify_user)
                track_new_user(user.id)
            else:
                track_user_login(user_id)

                # If user's image is from Facebook, token may have expired.
                # TODO: This needs to be smarter
                if 'fbcdn.net' in user.image_url:
                    user = update_user_from_spotify_user(user, spotify_user)

            _update_session(user_id, access_token, refresh_token, expires_at)
            session.permanent = True

            # If user was going to a particular destination before logging in,
            # send them there after login.
            if 'next_url' in session:
                app.logger.info('Found next_url in session: %s', session['next_url'])
                next_url = session['next_url'].decode('base64', 'strict')
                app.logger.info('Decoded next_url in session, redirecting: %s', next_url)
                session.pop('next_url')
                return redirect(next_url)

    return redirect(url_for('profile'))
예제 #6
0
    def test_existing(self):
        User(id=self.id,
             name=self.name,
             email=self.email,
             image_url=self.image_url,
             joined=datetime.utcnow())

        updated = create_or_update_user(self.id, 'New Name',
                                        '*****@*****.**', self.image_url)

        self.assertEqual('New Name', updated.name)
        self.assertEqual('*****@*****.**', updated.email)

        saved = get_user(self.id)
        self.assertIsNotNone(saved)
        self.assertEqual(updated.name, saved.name)
        self.assertEqual(updated.email, saved.email)
        self.assertEqual(updated.image_url, saved.image_url)
        self.assertEqual(updated.preferences, saved.preferences)
예제 #7
0
def view_user(user_id):
    if user_id == str(g.user.id):
        return redirect(url_for('profile'))

    page_user = get_user(user_id)

    from musicleague.persistence.select import select_leagues_for_user
    from musicleague.persistence.select import select_memberships_placed
    leagues = select_leagues_for_user(
        page_user.id,
        exclude_properties=['rounds', 'invited_users', 'scoreboard'])
    placed_leagues = select_memberships_placed(page_user.id)

    return {
        'user': g.user,
        'page_user': page_user,
        'leagues': leagues,
        'contributor_leagues': select_memberships_count(page_user.id),
        'placed_leagues': placed_leagues
    }
예제 #8
0
 def test_none_existing(self):
     self.assertIsNone(get_user(self.id))