Created
February 17, 2014 00:26
-
-
Save fabiocerqueira/9042659 to your computer and use it in GitHub Desktop.
Base para o exercício da Acens
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#-*- coding: utf-8 -*- | |
from functools import wraps | |
from getpass import getpass | |
import sys | |
from db import Users | |
Logged = False | |
def authenticate(username, password): | |
for user in Users: | |
if user['username'] == username and user['password'] == password: | |
return True | |
return False | |
def login(): | |
global Logged | |
if Logged: | |
return | |
username = raw_input("Digite o usuário: ") | |
password = getpass("Digite a senha: ") | |
if authenticate(username, password): | |
Logged = True | |
print "Usuário autenticado com sucesso!" | |
else: | |
print "Falha na autenticação do usuário!" | |
sys.exit(0) | |
def login_required(func): | |
@wraps(func) | |
def wrapper(*args, **kwargs): | |
login() | |
return func(*args, **kwargs) | |
return wrapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#-*- coding: utf-8 -*- | |
Users = [ | |
{ | |
'name': 'Fábio Cerqueira', | |
'username': 'fabio', | |
'password': 'acens', | |
} | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#-*- coding: utf-8 -*- | |
import sys | |
from auth import login_required | |
from screens import menu | |
from db import Users | |
def show_users(): | |
print "show_users" | |
def exit(): | |
print 'Bye bye!' | |
sys.exit(0) | |
@login_required | |
def admin(): | |
print "Admin!" | |
def home(): | |
menu_text = """ | |
Menu: | |
1 - Mostrar usuários | |
2 - Admin | |
9 - Exit | |
""" | |
options = { | |
'1': show_users, | |
'2': admin, | |
'9': exit | |
} | |
menu(menu_text, options) | |
if __name__ == '__main__': | |
home() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#-*- coding: utf-8 -*- | |
def menu(menu_text, options): | |
while True: | |
print menu_text | |
option = raw_input("Escolha uma opção: ") | |
screen = options.get(option) | |
if screen: | |
screen() | |
else: | |
print "Opção inválida!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment