-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from rafael1717y/Rafael_Flask_Admin
Flask Admin
- Loading branch information
Showing
12 changed files
with
178 additions
and
30 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"buildTargets":[],"launchTargets":[],"customConfigurationProvider":{"workspaceBrowse":{"browsePath":[],"compilerArgs":[]},"fileIndex":[]}} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"makefile.extensionOutputFolder": "./.vscode" | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
clean: | ||
@find ./ -name '*.pyc' -exec rm -f {} \; | ||
@find ./ -name 'Thumbs.db' -exec rm -f {} \; | ||
@find ./ -name '*~' -exec rm -f {} \; | ||
rm -rf .cache | ||
rm -rf build | ||
rm -rf dist | ||
rm -rf *.egg-info | ||
rm -rf htmlcov | ||
rm -rf .tox/ | ||
rm -rf docs/_build | ||
pip install -e .[dev] --upgrade --no-cache | ||
|
||
install: | ||
pip install -e .['dev'] | ||
|
||
|
||
init_db: | ||
FLASK_APP=econovolt.py flask create-db | ||
FLASK_APP=econovolt.py flask db upgrade | ||
|
||
test: | ||
FLASK_ENV=test pytest tests/ -v --cov=delivery | ||
|
||
format: | ||
isort **/*.py | ||
black -l 79 **/*.py |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from flask_admin import Admin | ||
from flask_admin.contrib.sqla import ModelView | ||
from app.ext.db import db | ||
from app.ext.db.models import Simulation | ||
|
||
|
||
admin = Admin() | ||
|
||
|
||
def init_app(app): | ||
admin.name = "Econovolt" | ||
admin.template_mode = "bootstrap2" | ||
admin.init_app(app) | ||
admin.add_view(ModelView(Simulation, db.session)) # se nao quer especializar uma classe pode-se usar o ModelView direto. | ||
# Adicionar o model de Resultados | ||
# TODO: Traduzir para pt-br. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,16 @@ | ||
from audioop import add | ||
from app.ext.auth import models | ||
from app.ext.auth.commands import list_users, add_user | ||
|
||
from app.ext.db import db | ||
from app.ext.admin import admin as main_admin | ||
from app.ext.auth.admin import UserAdmin | ||
from app.ext.auth.models import User | ||
|
||
|
||
|
||
def init_app(app): | ||
"""TODO: inicializar Flask simple login + JWT """ | ||
pass | ||
app.cli.command()(list_users) # usando o decorator como uma função que recebe outra função. | ||
app.cli.command()(add_user) | ||
main_admin.add_view(UserAdmin(User, db.session)) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
from email.errors import FirstHeaderLineIsContinuationDefect | ||
from flask_admin.contrib.sqla import ModelView | ||
from flask_admin.actions import action | ||
from app.ext.auth.models import User | ||
from app.ext.db import db | ||
from flask import flash, Markup | ||
from flask_admin.contrib.sqla import filters | ||
# from flask_admin.contrib.mongoengine | ||
|
||
|
||
def format_user(self, request, user, *args): | ||
"""Customiza o campo 'user' para retornar | ||
apenas o nome do email.Obs.: Self representa a | ||
própria instância do admin nesse caso.""" | ||
return user.email.split("@")[0] # só o nome | ||
|
||
|
||
|
||
class UserAdmin(ModelView): | ||
"""Interface administrativa de usuários.""" | ||
# formatação de uma coluna | ||
#column_formatters = {"email": format_user} | ||
column_formatters = { | ||
"email": lambda s, r, u, *a: Markup(f'<b>{u.email.split("@")[0]}</b>') | ||
} | ||
# lambda s, r, u, *a: Markup(f'<b>(u.email.split("a")[0]}</b>') | ||
# escolha das colunas que serão exibidas. | ||
column_list = ["email", "admin"] | ||
|
||
column_searchable_list = ["email"] | ||
|
||
column_filters = ["email", "admin", filters.FilterLike(User.email, "dominio", | ||
options=(("gmail", "Gmail"), ("uol", "Uol")))] | ||
|
||
# customização do perfil | ||
can_edit = False | ||
can_create = True | ||
can_delete = True | ||
|
||
|
||
@action( | ||
'toggle_admin', | ||
'Toggle admin status', | ||
'Are you sure?' | ||
) | ||
def toggle_admin_status(self, ids): | ||
"""Mudar o status de admin para usuário""" | ||
users = User.query.filter(User.id.in_(ids)).all() | ||
for user in users: | ||
#select pra trazer os usuários com id selecionados | ||
user.admin = not user.admin | ||
db.session.commit() | ||
flash(f"{len(users)} usuários alterados com sucesso!", "success") | ||
|
||
|
||
@action( | ||
'send email', | ||
'Send email to all users', | ||
'Are you sure?' | ||
) | ||
def send_email(self, ids): | ||
"""Enviar email.""" | ||
users = User.query.filter(User.id.in_(ids)).all() | ||
# TODO: 1) redirect para um form para escrever a mensagem do email. | ||
# 2) enviar o email | ||
for user in users: | ||
pass | ||
#select pra trazer os usuários com id selecionados | ||
# enviar email | ||
#db.session.commit() | ||
flash(f"{len(users)} um email enviado", "success") | ||
|
||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import click | ||
from app.ext.auth.models import User | ||
from app.ext.db import db | ||
|
||
|
||
@click.option("--username", "-u") | ||
@click.option("--email", "-e") | ||
@click.option("--password_hash", "-p") | ||
@click.option("--admin", "-a",is_flag=True, default=False) | ||
def add_user(username, email, password_hash, admin): | ||
"Adiciona um novo usuário." | ||
# TODO: tratar erros - Operation error? | ||
user = User( | ||
username=username, | ||
email = email, | ||
password_hash=password_hash, | ||
admin=admin, | ||
) | ||
db.session.add(user) | ||
db.session.commit() | ||
|
||
click.echo(f"Usuário {email} criado com sucesso!") | ||
|
||
|
||
def list_users(): | ||
users = User.query.all() | ||
click.echo(f"lista de usuários {users}") |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import pytest | ||
|
||
|
||
|
||
""" | ||
@pytest.fixture(scope="module") | ||
def app(): | ||
"Instance of Main flask app" | ||
return create_app() | ||
""" |