Cleaned up user login.

This commit is contained in:
Michael Lazar
2015-04-02 09:59:23 -07:00
parent c7a760791a
commit 2141fe6e31

View File

@@ -230,39 +230,38 @@ class BasePage(object):
@BaseController.register('u')
def login(self):
"""
Prompt to log out if logged in.
Prompt to log in if looged out.
Prompt to log into the user's account. Log out if the user is already
logged in.
"""
if self.reddit.is_logged_in():
ch = show_notification(self.stdscr, ["Log out? (y/N)"])
self.logout()
return
if ch == 121: # 'y'
self.reddit.clear_authentication()
show_notification(self.stdscr,
['Logged out'])
else:
curses.flash()
else:
username = self.prompt_input('Enter username:')
password = self.prompt_input('Enter password:', hide=True)
if (username == '' or username is None) \
or (password == '' or password is None):
if not username or not password:
curses.flash()
return
try:
self.reddit.login(username, password)
except praw.errors.InvalidUserPass:
show_notification(self.stdscr,
['Invalid password for username: {}'.format(username)])
show_notification(self.stdscr, ['Invalid user/pass'])
else:
show_notification(self.stdscr,
['Successfully logged in as: {}'.format(username)])
show_notification(self.stdscr, ['Logged in'])
return
def logout(self):
"""
Prompt to log out of the user's account.
"""
ch = self.prompt_input("Log out? (y/n):")
if ch == 'y':
self.reddit.clear_authentication()
show_notification(self.stdscr, ['Logged out'])
elif ch != 'n':
curses.flash()
def prompt_input(self, prompt, hide=False):
"""Prompt the user for input"""
@@ -270,8 +269,8 @@ class BasePage(object):
n_rows, n_cols = self.stdscr.getmaxyx()
if hide:
self.stdscr.addstr(n_rows - 1, 0, prompt+' '*(n_cols-1-len(prompt)),
attr)
prompt += ' ' * (n_cols - len(prompt) - 1)
self.stdscr.addstr(n_rows-1, 0, prompt, attr)
out = self.stdscr.getstr(n_rows-1, 1)
else:
self.stdscr.addstr(n_rows - 1, 0, prompt, attr)
@@ -279,7 +278,6 @@ class BasePage(object):
window = self.stdscr.derwin(1, n_cols - len(prompt),
n_rows - 1, len(prompt))
window.attrset(attr)
out = text_input(window)
return out