-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b515f1a
commit dcbb269
Showing
10 changed files
with
271 additions
and
63 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 |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
*.build* | ||
build/ | ||
*__pycache__* | ||
cms/database |
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,15 @@ | ||
import import_string | ||
|
||
|
||
def configure(app): | ||
"""Extension Factory, carrega as extensões definidas em | ||
app.config.EXTENSIONS | ||
""" | ||
for extension in app.config.get('EXTENSIONS', []): | ||
try: | ||
factory = import_string(extension) | ||
factory(app) | ||
except Exception as e: | ||
app.logger.error(f'Erro ao carregar {extension}: {e}') | ||
else: | ||
app.logger.debug(f'Extensão {extension} carregada com sucesso!') |
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,17 @@ | ||
from flask_admin import Admin | ||
from flask_admin.base import AdminIndexView | ||
from flask_admin.contrib.pymongo import ModelView | ||
from flask_simplelogin import login_required | ||
|
||
# decorate Flask-Admin view via Monkey Patching | ||
AdminIndexView._handle_view = login_required(AdminIndexView._handle_view) | ||
ModelView._handle_view = login_required(ModelView._handle_view) | ||
|
||
|
||
def configure(app): | ||
"""Inicia uma instância do Flask-Admin""" | ||
app.admin = Admin( | ||
app, | ||
name=app.config.get('FLASK_ADMIN_NAME', 'Flask CMS'), | ||
template_mode=app.config.get('FLASK_ADMIN_TEMPLATE_MODE', 'bootstrap3') | ||
) |
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,39 @@ | ||
from flask import current_app | ||
from flask_simplelogin import SimpleLogin | ||
from werkzeug.security import check_password_hash, generate_password_hash | ||
|
||
|
||
def configure(app): | ||
"""Inicializa o Flask Simple Login""" | ||
SimpleLogin(app, login_checker=login_checker) | ||
app.db.create_user = create_user | ||
|
||
# Functions | ||
|
||
|
||
def login_checker(user): | ||
"""Valida o usuário e senha para efetuar o login""" | ||
username = user.get('username') | ||
password = user.get('password') | ||
if not username or not password: | ||
return False | ||
|
||
existing_user = current_app.db.users.find_one({'username': username}) | ||
if not existing_user: | ||
return False | ||
|
||
if check_password_hash(existing_user.get('password'), password): | ||
return True | ||
|
||
return False | ||
|
||
|
||
def create_user(username, password): | ||
"""Registra um novo usuário caso não esteja cadastrado""" | ||
if current_app.db.users.find_one({'username': username}): | ||
raise RuntimeError(f'{username} já está cadastrado') | ||
|
||
user = {'username': username, | ||
'password': generate_password_hash(password)} | ||
|
||
current_app.db.users.insert_one(user) |
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,15 @@ | ||
from pathlib import Path | ||
from tinymongo import TinyMongoClient | ||
|
||
|
||
def configure(app): | ||
"""Inicia o client do TinyMongo e adiciona `app.db` | ||
*para usar MongoDB basta mudar para `pymongo.MongoClient` | ||
""" | ||
db_folder = app.config.get('DB_FOLDER', 'database') | ||
db_name = app.config.get('DB_NAME', 'cms_db') | ||
|
||
foldername = Path(db_folder) / Path(app.root_path) / Path('database') | ||
client = TinyMongoClient(foldername=foldername) | ||
|
||
app.db = client[db_name] |
Oops, something went wrong.