Skip to content

Commit

Permalink
add flask custom json encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
afourmy committed Jul 9, 2019
1 parent c381640 commit 37fd0dd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
13 changes: 13 additions & 0 deletions eNMS/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from flask import Flask, jsonify, make_response, render_template
from flask_assets import Bundle
from flask.json import JSONEncoder
from flask.wrappers import Request, Response
from pathlib import Path
from sqlalchemy import Numeric
from sqlalchemy.orm import configure_mappers
from typing import Any, Tuple

Expand All @@ -27,6 +29,16 @@ def register_modules(app: Flask) -> None:
controller.init_app(app)


def configure_encoder(app: Flask) -> None:
class CustomJSONEncoder(JSONEncoder):
def default(self, obj: Any) -> Any:
if isinstance(obj, Numeric):
return str(obj)
return JSONEncoder.default(self, obj)

app.json_encoder = CustomJSONEncoder


def configure_login_manager(app: Flask) -> None:
@login_manager.user_loader
def user_loader(id: int) -> User:
Expand Down Expand Up @@ -102,6 +114,7 @@ def create_app(path: Path, config: str) -> Flask:
app.mode = app.config["MODE"]
app.path = path
register_modules(app)
configure_encoder(app)
configure_login_manager(app)
controller.init_services()
configure_database(app)
Expand Down
6 changes: 1 addition & 5 deletions eNMS/database/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from os import environ
from sqlalchemy import create_engine, Float, Numeric, PickleType
from sqlalchemy import create_engine, PickleType
from sqlalchemy.dialects.mysql.base import MSMediumBlob
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import scoped_session, sessionmaker
Expand Down Expand Up @@ -41,7 +41,3 @@ def session_factory() -> Any:
class CustomMediumBlobPickle(PickleType):
if DIALECT == "mysql":
impl = MSMediumBlob


class CustomFloat(Numeric if DIALECT == "mysql" else Float): # type: ignore
pass
16 changes: 12 additions & 4 deletions eNMS/models/inventory.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
from re import search
from sqlalchemy import Boolean, Column, Float, ForeignKey, Integer, String, Text
from sqlalchemy import (
Boolean,
Column,
Float,
ForeignKey,
Integer,
Numeric,
String,
Text,
)
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.ext.mutable import MutableDict
from sqlalchemy.orm import backref, relationship
from typing import Any, Dict, List, Union

from eNMS.controller import controller
from eNMS.database import (
CustomFloat,
CustomMediumBlobPickle,
LARGE_STRING_LENGTH,
SMALL_STRING_LENGTH,
Expand Down Expand Up @@ -76,8 +84,8 @@ class Device(CustomDevice):
operating_system = Column(String(SMALL_STRING_LENGTH), default="")
os_version = Column(String(SMALL_STRING_LENGTH), default="")
ip_address = Column(String(SMALL_STRING_LENGTH), default="")
longitude = Column(CustomFloat(18, 8), default=0.0)
latitude = Column(CustomFloat(18, 8), default=0.0)
longitude = Column(Numeric(18, 8), default=0.0)
latitude = Column(Numeric(18, 8), default=0.0)
port = Column(Integer, default=22)
username = Column(String(SMALL_STRING_LENGTH), default="")
password = Column(String(SMALL_STRING_LENGTH), default="")
Expand Down

0 comments on commit 37fd0dd

Please sign in to comment.