def decorated(*args, **kwargs): assert not (account and all_accounts), 'Ambiguous arguments' # will raise a 500 error if headers not found #g.platform = request.headers['Platform'] if not getattr(g, 'save_account', False): g.save_account = save_account if hasattr(g, 'user') and \ (not account or hasattr(g, 'account')) and \ (not all_accounts or hasattr(g, 'accounts')) and \ (not main_account or hasattr(g, 'main_account')): return f(*args, **kwargs) g.user_id = userid_from_request() if account: # flask always passes url variables as keyword arguments g.account_id = kwargs[key] #request.headers.get('Account') else: g.account_id = None try: if account: g.user, g.account = UserAccount.from_user_account( g.user_id, g.account_id) elif all_accounts: g.user, g.accounts = UserAccount.from_user(g.user_id) else: g.user = User.select().where(User.id == g.user_id).get() if main_account and not hasattr(g, 'main_account'): # TODO: catch errors if key not found main_account_id = request.headers['Account'] #main_account_service = request.headers['Service'] if account and g.account_id == main_account_id: g.main_account = g.account elif all_accounts: for acct in g.accounts: if str(acct.id) == main_account_id: g.main_account = acct break if not hasattr(g, 'main_account'): _, g.main_account = UserAccount.from_user_account( g.user_id, main_account_id) except (UserAccount.DoesNotExist, User.DoesNotExist), e: logging.warning('User %s, account %s not found: %s' % \ (g.user_id, g.account_id, e)) if not fail_silently: raise abort(401, 'Not logged in')
def home(path=None): if path: # reloading url paths currently not supported in client javascript return redirect(url_for('home')) try: #assert session['logged_in'] user_id = session['user'] user, accounts = UserAccount.from_user(user_id) except (KeyError, AssertionError, User.DoesNotExist, UserAccount.DoesNotExist): session.clear() return redirect(url_for('index')) user = user.__jsonify__() accounts = [account.__jsonify__() for account in accounts] return render_template('home.html', user=user, accounts=accounts)