Skip to content
This repository has been archived by the owner on Mar 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #117 from lendingblock/master
Browse files Browse the repository at this point in the history
0.8.7
  • Loading branch information
markharley authored Sep 21, 2018
2 parents eeb4a1e + 85426d1 commit 2d5d6bf
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 15 deletions.
2 changes: 1 addition & 1 deletion openapi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Minimal OpenAPI asynchronous server application
"""

__version__ = '0.8.6'
__version__ = '0.8.7'
48 changes: 35 additions & 13 deletions openapi/data/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,74 +43,96 @@ def _(f):

@converter(sa.Boolean)
def bl(col, required):
return (bool, fields.bool_field(**info(col, required)))
data_field = col.info.get('data_field', fields.bool_field)
return (
bool,
data_field(**info(col, required))
)


@converter(sa.Integer)
def integer(col, required):
return (int, fields.number_field(precision=0, **info(col, required)))
data_field = col.info.get('data_field', fields.number_field)
return (
int,
data_field(precision=0, **info(col, required))
)


@converter(sa.Numeric)
def number(col, required):
data_field = col.info.get('data_field', fields.decimal_field)
return (
Decimal, fields.decimal_field(
precision=col.type.scale, **info(col, required)
)
Decimal,
data_field(precision=col.type.scale, **info(col, required))
)


@converter(sa.String, sa.Text, sa.CHAR, sa.VARCHAR)
def string(col, required):
return (str, fields.str_field(
max_length=col.type.length or 0, **info(col, required)))
data_field = col.info.get('data_field', fields.str_field)
return (
str,
data_field(max_length=col.type.length or 0, **info(col, required))
)


@converter(sa.DateTime)
def dt_ti(col, required):
return (datetime, fields.date_time_field(**info(col, required)))
data_field = col.info.get('data_field', fields.date_time_field)
return (
datetime,
data_field(**info(col, required))
)


@converter(sa.Date)
def dt(col, required):
return (date, fields.date_field(**info(col, required)))
data_field = col.info.get('data_field', fields.date_field)
return (
date,
data_field(**info(col, required))
)


@converter(sa.Enum)
def en(col, required):
data_field = col.info.get('data_field', fields.enum_field)
return (
col.type.enum_class,
fields.enum_field(col.type.enum_class, **info(col, required))
data_field(col.type.enum_class, **info(col, required))
)


@converter(sa.JSON)
def js(col, required):
data_field = col.info.get('data_field', fields.json_field)
val = None
if col.default:
arg = col.default.arg
val = arg() if col.default.is_callable else arg
return (
JsonTypes.get(type(val), typing.Dict),
fields.json_field(**info(col, required))
data_field(**info(col, required))
)


@converter(UUIDType)
def uuid(col, required):
data_field = col.info.get('data_field', fields.uuid_field)
return (
str,
fields.uuid_field(**info(col, required))
data_field(**info(col, required))
)


def info(col, required):

data = dict(
description=col.doc,
required=not col.nullable if required is not False else False
)
data.update(col.info)
data.pop('data_field', None)
return data


Expand Down
10 changes: 9 additions & 1 deletion tests/example/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from sqlalchemy_utils import UUIDType

from openapi.db.columns import UUIDColumn
from openapi.data import fields

from .models import TaskType

original_init = UUIDType.__init__
Expand All @@ -17,6 +19,10 @@ def patch_init(self, binary=True, native=True, **kw):
UUIDType.__init__ = patch_init


def title_field(**kwargs):
return fields.str_field(**kwargs)


def meta(meta=None):
"""Add task related tables
"""
Expand All @@ -28,7 +34,9 @@ def meta(meta=None):
UUIDColumn(
'id', make_default=True, doc='Unique ID'),
sa.Column(
'title', sa.String(64), nullable=False, info=dict(min_length=3)),
'title', sa.String(64), nullable=False,
info=dict(min_length=3, data_field=title_field)
),
sa.Column('done', sa.DateTime),
sa.Column('severity', sa.Integer),
sa.Column('type', sa.Enum(TaskType)),
Expand Down

0 comments on commit 2d5d6bf

Please sign in to comment.