From c542388e12cfe144db28e6c556e28a27984a1c5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Goran=20Meki=C4=87?= Date: Mon, 30 Mar 2020 01:51:11 +0200 Subject: [PATCH] Try import of specific DB type model first --- freenit/__init__.py | 23 ++++++++++++++++++----- freenit/schemas/base.py | 3 ++- setup.py | 2 +- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/freenit/__init__.py b/freenit/__init__.py index b17373a..087fc91 100644 --- a/freenit/__init__.py +++ b/freenit/__init__.py @@ -1,12 +1,13 @@ import sys from importlib import import_module -import freenit.schemas.user from flask import Flask, send_file from flask_collect import Collect from flask_cors import CORS from flask_jwt_extended import JWTManager from flask_security import Security + +import freenit.schemas.user from freenit.schemas.paging import PageOutSchema from . import cli @@ -19,8 +20,14 @@ def sqlinit(app): from .db import db db.init_app(app) app.db = db - User = import_module(f"{app.config['NAME']}.models.user").User - role_module = import_module(f"{app.config['NAME']}.models.role") + try: + User = import_module(f"{app.config['NAME']}.models.sql.user").User + except ModuleNotFoundError: + User = import_module(f"{app.config['NAME']}.models.user").User + try: + role_module = import_module(f"{app.config['NAME']}.models.role") + except ModuleNotFoundError: + role_module = import_module(f"{app.config['NAME']}.models.sql.role") Role = role_module.Role UserRoles = role_module.UserRoles app.user_datastore = PeeweeUserDatastore( @@ -35,8 +42,14 @@ def mongoinit(app): from flask_security import MongoEngineUserDatastore from flask_mongoengine import MongoEngine app.db = MongoEngine(app) - User = import_module(f"{app.config['NAME']}.models.user").User - Role = import_module(f"{app.config['NAME']}.models.role").Role + try: + User = import_module(f"{app.config['NAME']}.models.user").User + except ModuleNotFoundError: + User = import_module(f"{app.config['NAME']}.models.mongo.user").User + try: + Role = import_module(f"{app.config['NAME']}.models.role").Role + except ModuleNotFoundError: + Role = import_module(f"{app.config['NAME']}.models.mongo.role").Role app.user_datastore = MongoEngineUserDatastore(app.db, User, Role) diff --git a/freenit/schemas/base.py b/freenit/schemas/base.py index d060d3e..62b2533 100644 --- a/freenit/schemas/base.py +++ b/freenit/schemas/base.py @@ -1,4 +1,4 @@ -from marshmallow import Schema +from marshmallow import EXCLUDE, Schema from ..fields.objectid import ID @@ -9,3 +9,4 @@ class BaseSchema(Schema): class Meta: strict = True ordered = True + unknown = EXCLUDE diff --git a/setup.py b/setup.py index a77bc1b..554a3c9 100644 --- a/setup.py +++ b/setup.py @@ -16,7 +16,7 @@ setup( name='freenit', - version='0.1.3', + version='0.1.4', description='REST API framework based on Flask-Smorest', long_description=README, long_description_content_type='text/markdown',