Skip to content

Commit 848a88e

Browse files
committed
Add Models to JWTRoutes class & init_app method #119
1 parent f7c6263 commit 848a88e

File tree

5 files changed

+60
-10
lines changed

5 files changed

+60
-10
lines changed

Pipfile

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pytest = "*"
88
twine = "*"
99
pyjwt = "*"
1010
Flask-SQLAlchemy = "*"
11+
Sphinx = "*"
1112

1213
[packages]
1314
flask = "*"

flask_jwt_router/_authentication.py

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def update_entity(self, extensions: Config, exp: int, table_name, **kwarg):
3838
# pylint:disable=missing-function-docstring
3939
pass
4040

41+
@abstractmethod
4142
def encode_token(self, extensions: Config, entity_id: Any, exp: int, table_name: str):
4243
# pylint:disable=missing-function-docstring
4344
pass

flask_jwt_router/_extensions.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
The main configuration class for Flask-JWT-Router
33
"""
44
from abc import ABC
5-
from typing import Dict, Any
5+
from typing import Dict, Any, List
6+
7+
from ._entity import _ORMType
68

79

810
class Config:
@@ -33,23 +35,26 @@ def __init__(self,
3335

3436
class BaseExtension(ABC):
3537
"""Abstract Base Class for Extensions"""
36-
def init_extensions(self, config: Dict[str, Any]) -> Config:
38+
def init_extensions(self, config: Dict[str, Any], **kwargs) -> Config:
3739
# pylint: disable=missing-function-docstring
3840
pass
3941

4042

4143
class Extensions(BaseExtension):
4244
"""Contains the main configuration values"""
43-
def init_extensions(self, config: Any) -> Config:
45+
entity_models: List[_ORMType]
46+
47+
def init_extensions(self, config: Any, **kwargs) -> Config:
4448
"""
4549
:param config:
4650
:return:
4751
"""
52+
entity_models = kwargs.get("entity_models")
4853
return Config(
4954
config.get("SECRET_KEY") or "DEFAULT_SECRET_KEY",
5055
config.get("ENTITY_KEY") or "id",
5156
config.get("WHITE_LIST_ROUTES") or [],
5257
config.get("JWT_ROUTER_API_NAME"),
5358
config.get("IGNORED_ROUTES") or [],
54-
config.get("ENTITY_MODELS") or [],
59+
entity_models or config.get("ENTITY_MODELS") or [],
5560
)

flask_jwt_router/_jwt_router.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
import logging
66
from warnings import warn
7+
from typing import List
78

89
from ._extensions import BaseExtension, Extensions, Config
9-
from ._entity import BaseEntity, Entity
10+
from ._entity import BaseEntity, Entity, _ORMType
1011
from ._routing import BaseRouting, Routing
1112
from ._authentication import BaseAuthStrategy
1213

@@ -27,6 +28,9 @@ class FlaskJWTRouter:
2728
#: The Flask application instance.
2829
app = None
2930

31+
#: A list of entity models
32+
entity_models: List[_ORMType]
33+
3034
#: Token expiry value. eg. 30 = 30 days from creation date.
3135
exp: int = 30
3236

@@ -50,20 +54,23 @@ class FlaskJWTRouter:
5054
#: for more information.
5155
ext: BaseExtension
5256

53-
def __init__(self, app=None):
57+
def __init__(self, app=None, **kwargs):
58+
self.entity_models = kwargs.get("entity_models")
5459
self.ext = Extensions()
60+
self.app = app
5561
if app:
56-
self.init_app(app)
62+
self.init_app(app, entity_models=self.entity_models)
5763

58-
def init_app(self, app):
64+
def init_app(self, app=None, **kwargs):
5965
"""
6066
You can use this to set up your config at runtime
6167
:param app: Flask application instance
6268
:return:
6369
"""
64-
self.app = app
70+
self.app = self.app or app
71+
entity_models = self.entity_models or kwargs.get("entity_models")
6572
config = self.get_app_config(app)
66-
self.extensions = self.ext.init_extensions(config)
73+
self.extensions = self.ext.init_extensions(config, entity_models=entity_models)
6774
self.entity = Entity(self.extensions)
6875
self.routing = Routing(self.app, self.extensions, self.entity)
6976
self.app.before_request(self.routing.before_middleware)

tests/test_jwt_routes.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from flask import Flask
2+
3+
4+
from flask_jwt_router._jwt_routes import JwtRoutes
5+
from tests.fixtures.model_fixtures import MockEntityModel
6+
7+
8+
class TestJwtRoutes:
9+
10+
app = Flask(__name__)
11+
12+
def test_init_app(self):
13+
jwt = JwtRoutes(self.app, entity_models=[MockEntityModel])
14+
assert jwt.extensions.entity_models[0] == MockEntityModel
15+
assert jwt.app == self.app
16+
17+
jwt = JwtRoutes()
18+
jwt.init_app(self.app, entity_models=[MockEntityModel])
19+
assert jwt.extensions.entity_models[0] == MockEntityModel
20+
assert jwt.app == self.app
21+
22+
jwt = JwtRoutes(self.app)
23+
jwt.init_app(entity_models=[MockEntityModel])
24+
assert jwt.extensions.entity_models[0] == MockEntityModel
25+
assert jwt.app == self.app
26+
27+
jwt = JwtRoutes(entity_models=[MockEntityModel])
28+
jwt.init_app(self.app)
29+
assert jwt.extensions.entity_models[0] == MockEntityModel
30+
assert jwt.app == self.app
31+
32+
self.app.config["ENTITY_MODELS"] = [MockEntityModel]
33+
jwt = JwtRoutes()
34+
jwt.init_app(self.app)
35+
assert jwt.extensions.entity_models[0] == MockEntityModel
36+
assert jwt.app == self.app

0 commit comments

Comments
 (0)