Created
February 20, 2014 20:10
-
-
Save Anderson0abr/9122203 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
import pickle | |
from db import Users | |
Logged = False | |
def authenticate(username, password): | |
try: | |
global Users | |
with open("database.db","r") as f: | |
Users = pickle.load(f) | |
except Exception: | |
pass | |
for user in Users: | |
if user['username'] == username and user['password'] == password: | |
return True | |
return False | |
def login(): | |
global Logged | |
if Logged: | |
return | |
print u"\nLogin necessário!\n" | |
username = raw_input(u"Digite o usuário: ") | |
password = getpass("Digite a senha: ") | |
if authenticate(username, password): | |
Logged = True | |
print u"Usuário autenticado com sucesso!" | |
else: | |
print u"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 hidden or 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 hidden or 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 | |
import pickle | |
import os.path | |
from auth import login_required | |
from screens import menu | |
from db import Users | |
@login_required | |
def create(): | |
new_name = raw_input(u"Digite o nome do admin a ser adicionado: ") | |
new_user = raw_input(u"Digite o usuário do admin: ") | |
new_password = raw_input("Digite a senha: ") | |
Users.append({'name': new_name, 'username': new_user, 'password': new_password}) | |
print u"\nUsuário cadastrado com sucesso\n" | |
with open("database.db", "w") as f: | |
pickle.dump(Users, f) | |
@login_required | |
def update(): | |
update_user = raw_input(u"Digite o nome do usuário a ser modificado: ") | |
for user in Users: | |
if user['name'] == update_user: | |
new_username = raw_input(u"Digite o novo usuário: ") | |
new_password = raw_input("Digite a nova senha: ") | |
new_user = {'username': new_username, 'password': new_password} | |
user.update(new_user) | |
print "\nSeus dados foram editados com sucesso\n" | |
with open("database.db", "w") as f: | |
pickle.dump(Users, f) | |
return | |
print u"Usuário não existe" | |
@login_required | |
def delete(): | |
delete_user = raw_input(u"Digite o nome do usuário a ser deletado: ") | |
for user in Users: | |
if user['name'] == delete_user: | |
Users.remove(user) | |
print u"\nUsuário deletado com sucesso\n" | |
with open("database.db", "w") as f: | |
pickle.dump(Users, f) | |
return | |
else: | |
continue | |
print u"Usuário não existe" | |
def show_users(): | |
i = 1 | |
for user in Users: | |
print "-" * 3 | |
print "{i} - {user}".format(i = i, user = user['name']) | |
print "-" * 3 | |
i += 1 | |
def exit(): | |
print 'Bye bye!' | |
sys.exit(0) | |
@login_required | |
def admin(): | |
menu_text = u""" | |
Menu admin: | |
1 - Criar usuário | |
2 - Mostrar usuários | |
3 - Editar usuário | |
4 - Deletar usuário | |
5 - Exit | |
""" | |
options = { | |
'1': create, | |
'2': show_users, | |
'3': update, | |
'4': delete, | |
'5': exit | |
} | |
menu(menu_text, options) | |
def home(): | |
menu_text = u""" | |
Menu: | |
1 - Mostrar usuários | |
2 - Admin | |
9 - Exit | |
""" | |
options = { | |
'1': show_users, | |
'2': admin, | |
'9': exit | |
} | |
menu(menu_text, options) | |
if __name__ == '__main__': | |
try: | |
global Users | |
with open("database.db", "r") as f: | |
Users = pickle.load(f) | |
except Exception: | |
with open("database.db", "w") as f: | |
pass | |
home() |
This file contains hidden or 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(u"Escolha uma opção: ") | |
screen = options.get(option) | |
if screen: | |
screen() | |
else: | |
print u"Opção inválida!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment