def change_password(request): __data = request.POST __logged_user = UserSession.GetSessionData(request) __logged_user_data = User.get_by_id(__logged_user.id) __new_password = __data['new_password'] __old_password = __data['old_password'] user = { 'email': __logged_user_data.email, 'password': __old_password, 'new_password': __new_password } if User.signin(user): if User.update_password(user): return render( request, 'user/my-data.html', { "title": "Meus dados", "msg_text": 'Senha atualizada com sucesso!', "msg_type": 'primary', 'user': __logged_user_data }) return render( request, 'user/my-data.html', { "title": "Meus dados", "msg_text": "Houve um erro interno!", "msg_type": 'danger', 'user': __logged_user_data }) return render( request, 'my-data.html', { "title": "Meus dados", "msg_text": "Senha atual não confere!", "msg_type": 'warning', 'user': __logged_user_data })
def update_email(request): if request.method == 'POST': data = json.loads(request.POST['user']) if User.signin(data): if User.update_email(data): return JsonResponse(True, status=200, safe=False) else: return JsonResponse({"Error": "Email not found"}, status=400) return JsonResponse({"Error": "Incorrect password"}, status=400) else: return JsonResponse({"Error": "HTTP method not allowed"}, status=405, safe=False)
def get_ID(request): if request.method == 'POST': data = json.loads(request.POST['user']) if User.signin(data): __id = User.get_ID(data) if __id: return JsonResponse({"id": __id}, status=200, safe=False) else: return JsonResponse({"Error": "Email not found"}, status=400) else: return JsonResponse({"Error": "HTTP method not allowed"}, status=405, safe=False)
def signin(request): if UserSession.IsActive(request): __logged_user = UserSession.GetSessionData(request) return render(request, 'home.html', {'user': __logged_user}) form_login = FormLogin(request.POST) if form_login.is_valid(): user = { 'email': request.POST['email'], 'password': request.POST['password'] } __logged_user = User.signin(user) if (__logged_user != None): UserSession.SaveSession(request, __logged_user) return render(request, 'home.html', {'user': __logged_user}) else: # Se método de login do model retornar None o login é inválido return render( request, 'index.html', { "msg_text": 'Dados não conferem!', "msg_type": 'danger', 'form_login': form_login, 'user': __logged_user }) else: # Verificação de formulário válido return render( request, 'index.html', { "msg_text": 'Atenção aos campos!', "msg_type": 'danger', 'form_login': form_login, 'user': __logged_user })
def my_data(request): __logged_user = UserSession.GetSessionData(request) __logged_user_data = User.get_by_id(__logged_user.id) return render(request, 'user/my-data.html', { 'title': 'Meus dados', 'user': __logged_user_data })
def list_users(request): __logged_user = UserSession.GetSessionData(request) users = User.get_all() return render(request, 'user/list-users.html', { 'title': 'Gerenciar usuários', 'user': __logged_user, 'users': users })
def change_status(request, id): __logged_user = UserSession.GetSessionData(request) if User.changeActive(id): users = User.get_all() __text = 'Alterado com sucesso!' __type = 'success' else: __text = 'Erro!' __type = 'danger' users = User.get_all() return render( request, 'user/list-users.html', { 'title': 'Gerenciar usuários', 'user': __logged_user, 'users': users, 'msg_text': __text, 'msg_type': __type })
def GetSessionData(request): if(request.session['user']!=None): return User( id=request.session['user']['id'], name=request.session['user']['name'], email=request.session['user']['email'], level=request.session['user']['level'], ) return None
def delete_user(request, id): __logged_user = UserSession.GetSessionData(request) __text = "" __type = "" if (User.delete(id)): __text = "Usuário excluído com sucesso!" __type = "success" else: __text = "Erro ao excluir!" __type = "danger" users = User.get_all() return render( request, 'user/list-users.html', { 'title': 'Gerenciar usuários', 'msg_type': __type, 'msg_text': __text, 'user': __logged_user, 'users': users })
def change_level(request, id): __logged_user = UserSession.GetSessionData(request) __text = "" __type = "" if (User.changeLevel(id)): __text = "Alterado com sucesso!" __type = "success" else: __text = "Erro!" __type = "danger" users = User.get_all() return render( request, 'user/list-users.html', { 'title': 'Gerenciar usuários', 'msg_type': __type, 'msg_text': __text, 'user': __logged_user, 'users': users })
def update_data(request): __logged_user = UserSession.GetSessionData(request) __data = request.POST if (User.updateData(__data, __logged_user.id)): __text = "Alterado com sucesso!" __type = "success" __logged_user = User.get_by_id(__logged_user.id) __user_json = { 'id': __logged_user.id, 'name': __logged_user.name, 'email': __logged_user.email, 'level': __logged_user.level } UserSession.SaveSession(request, __user_json) else: __text = 'Erro ao alterar dados!' __type = 'danger' return render( request, 'user/my-data.html', { "title": "Meus dados", "msg_text": __text, "msg_type": __type, 'user': __logged_user })
def signin(request): if request.method == 'POST': __data = json.loads(request.POST['user']) __logged_user = User.signin(__data) if __logged_user != None: jsonObj = { 'id': __logged_user['id'], 'name': __logged_user['name'], 'email': __logged_user['email'], 'level': __logged_user['level'] } return JsonResponse(jsonObj, status=200, safe=False) return JsonResponse(False, status=401, safe=False) else: return JsonResponse({"Error": "HTTP method not allowed"}, status=405, safe=False)