def before_request(): if request.endpoint == 'logout': return current_user = session['current_user'] if 'current_user' in session else '' g.user = None if current_user: g.user = select_user(current_user) access_token = session['access_token'] if 'access_token' in session else '' g.access_token = access_token g.spotify = None if access_token: expiration = session['expires_at'] # If auth token has expired, refresh it if int(expiration) < int(time()): refresh_token = session['refresh_token'] oauth = get_spotify_oauth() # Calls to refresh token sometimes fail, so retry token_info = None retries = 0 while token_info is None and retries < 3: token_info = oauth.refresh_access_token(refresh_token) retries += 1 access_token = token_info['access_token'] _update_session( current_user, access_token, token_info['refresh_token'], int(token_info['expires_at'])) g.spotify = Spotify(access_token)
def get_user(user_id): try: user = select_user(user_id) if not user: return json.dumps(None) except Exception: return json.dumps(None) leagues = [{ 'name': league.name, 'active': league.is_active, 'complete': league.is_complete, 'users': [{ 'id': user.id, 'name': user.name, } for user in league.users] } for league in select_leagues_for_user(user)] return json.dumps({ 'id': user.id, 'name': user.name, 'leagues': leagues, })
def admin_make_user_admin(user_id): if not user_id: return user = select_user(user_id) if not user: return user.is_admin = True update_user(user) return redirect(request.referrer)
def process_link_user(context, message_text): user = select_user(message_text) if not user: send_message(context.id, "I'm sorry. I didn't find a user with that ID.") return context.status = STATUS_DEFAULT context.user = user context.save() user.messenger = context upsert_user(user) send_message( context.id, "I've linked your Facebook and Music League accounts!\n" "You will now receive notifications from me in Messenger.")
def get_user(id): user = select_user(id) if user is None: return None updated = False if not user.image_url: user.image_url = choice(DEFAULT_AVATARS) updated = True if not user.profile_background: user.profile_background = choice(PROFILE_BACKGROUNDS) updated = True if updated: upsert_user(user) return user
def post_manage_league(league_id): 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', [])) added_members = [select_user(uid) for uid in user_ids] emails = json.loads(request.form.get('invited-members', [])) deleted_members = json.loads(request.form.get('deleted-members', [])) added_rounds = json.loads(request.form.get('added-rounds', [])) edited_rounds = json.loads(request.form.get('edited-rounds', [])) deleted_rounds = json.loads(request.form.get('deleted-rounds', [])) league = select_league(league_id) if name != league.name: league.name = name update_league(league) 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 added_member in added_members: add_user(league, added_member.email, notify=True, user=added_member) for email in emails: add_user(league, email, notify=True) for deleted_member in deleted_members: remove_user(league, deleted_member) for added_round in added_rounds: submission_due_date_str = added_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 = added_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, added_round['name'], added_round['description'], submission_due_date, vote_due_date) for edited_round in edited_rounds: submission_due_date_str = edited_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 = edited_round['voting-due-date-utc'] vote_due_date = utc.localize( datetime.strptime(vote_due_date_str, '%m/%d/%y %I%p')) round = select_round(edited_round['id']) if not round: continue round.league = league update_submission_period(edited_round['id'], edited_round['name'], edited_round['description'], submission_due_date, vote_due_date, submission_period=round) for deleted_round in deleted_rounds: try: remove_submission_period(deleted_round) except Exception as e: app.logger.warning('Error while attempting to delete round %s: %s', deleted_round, str(e)) if league.scoreboard: league = select_league(league_id) calculate_league_scoreboard(league) app.logger.info('User modified league', extra={ 'league': league.id, 'user': g.user.id }) return redirect(url_for('view_league', league_id=league_id))