Merge pull request #2 from ProgrammerPlus1998/origin/add_smallinteger

add:SmallInteger()
This commit is contained in:
ProgrammerPlus
2021-08-06 11:49:30 +08:00
committed by GitHub
10 changed files with 98 additions and 49 deletions

View File

@ -614,8 +614,8 @@ Available Model Fields (with required args - optional ones in docs):
* `Time()` * `Time()`
* `DateTime()` * `DateTime()`
* `JSON()` * `JSON()`
* `SmallInteger()`
* `BigInteger()` * `BigInteger()`
* `SmallInteger()`
* `Decimal(scale, precision)` * `Decimal(scale, precision)`
* `UUID()` * `UUID()`
* `LargeBinary(max_length)` * `LargeBinary(max_length)`

View File

@ -347,6 +347,34 @@ Accepts required and optional parameters that each column type accepts.
`sqlalchemy Column`: initialized column with proper options `sqlalchemy Column`: initialized column with proper options
<a name="fields.model_fields.SmallInteger"></a>
## SmallInteger Objects
```python
class SmallInteger(Integer, int)
```
SmallInteger field factory that construct Field classes and populated their values.
<a name="fields.model_fields.SmallInteger.get_column_type"></a>
#### get\_column\_type
```python
| @classmethod
| get_column_type(cls, **kwargs: Any) -> Any
```
Return proper type of db column for given field type.
Accepts required and optional parameters that each column type accepts.
**Arguments**:
- `kwargs` (`Any`): key, value pairs of sqlalchemy options
**Returns**:
`sqlalchemy Column`: initialized column with proper options
<a name="fields.model_fields.Decimal"></a> <a name="fields.model_fields.Decimal"></a>
## Decimal Objects ## Decimal Objects

View File

@ -69,6 +69,19 @@ Each of the `Fields` has assigned both `sqlalchemy` column class and python type
!!!tip !!!tip
For explanation of other parameters check [pydantic][pydantic] documentation. For explanation of other parameters check [pydantic][pydantic] documentation.
### SmallInteger
`SmallInteger(minimum: int = None,
maximum: int = None,
multiple_of: int = None)` has no required parameters.
* Sqlalchemy column: `sqlalchemy.SmallInteger`
* Type (used for pydantic): `int`
!!!tip
For explanation of other parameters check [pydantic][pydantic] documentation.
### Float ### Float
`Float(minimum: float = None, `Float(minimum: float = None,

View File

@ -636,6 +636,7 @@ Available Model Fields (with required args - optional ones in docs):
* `DateTime()` * `DateTime()`
* `JSON()` * `JSON()`
* `BigInteger()` * `BigInteger()`
* `SmallInteger()`
* `Decimal(scale, precision)` * `Decimal(scale, precision)`
* `UUID()` * `UUID()`
* `LargeBinary(max_length)` * `LargeBinary(max_length)`

View File

@ -41,6 +41,7 @@ from ormar.exceptions import ( # noqa: I100
from ormar.fields import ( from ormar.fields import (
BaseField, BaseField,
BigInteger, BigInteger,
SmallInteger,
Boolean, Boolean,
DECODERS_MAP, DECODERS_MAP,
Date, Date,
@ -80,6 +81,7 @@ __version__ = "0.10.15"
__all__ = [ __all__ = [
"Integer", "Integer",
"BigInteger", "BigInteger",
"SmallInteger",
"Boolean", "Boolean",
"Time", "Time",
"Text", "Text",

View File

@ -9,6 +9,7 @@ from ormar.fields.foreign_key import ForeignKey, ForeignKeyField, UniqueColumns
from ormar.fields.many_to_many import ManyToMany, ManyToManyField from ormar.fields.many_to_many import ManyToMany, ManyToManyField
from ormar.fields.model_fields import ( from ormar.fields.model_fields import (
BigInteger, BigInteger,
SmallInteger,
Boolean, Boolean,
Date, Date,
DateTime, DateTime,
@ -29,6 +30,7 @@ from ormar.fields.through_field import Through, ThroughField
__all__ = [ __all__ = [
"Decimal", "Decimal",
"BigInteger", "BigInteger",
"SmallInteger",
"Boolean", "Boolean",
"Date", "Date",
"DateTime", "DateTime",

View File

@ -538,54 +538,6 @@ else:
) )
class SmallInteger(Integer, int):
"""
SmallInteger field factory that construct Field classes and populated their values.
"""
_type = int
_sample = 0
def __new__( # type: ignore
cls,
*,
minimum: int = None,
maximum: int = None,
multiple_of: int = None,
**kwargs: Any
) -> BaseField:
autoincrement = kwargs.pop("autoincrement", None)
autoincrement = (
autoincrement
if autoincrement is not None
else kwargs.get("primary_key", False)
)
kwargs = {
**kwargs,
**{
k: v
for k, v in locals().items()
if k not in ["cls", "__class__", "kwargs"]
},
}
kwargs["ge"] = kwargs["minimum"]
kwargs["le"] = kwargs["maximum"]
return super().__new__(cls, **kwargs)
@classmethod
def get_column_type(cls, **kwargs: Any) -> Any:
"""
Return proper type of db column for given field type.
Accepts required and optional parameters that each column type accepts.
:param kwargs: key, value pairs of sqlalchemy options
:type kwargs: Any
:return: initialized column with proper options
:rtype: sqlalchemy Column
"""
return sqlalchemy.SmallInteger()
class BigInteger(Integer, int): class BigInteger(Integer, int):
""" """
BigInteger field factory that construct Field classes and populated their values. BigInteger field factory that construct Field classes and populated their values.
@ -634,6 +586,54 @@ class BigInteger(Integer, int):
return sqlalchemy.BigInteger() return sqlalchemy.BigInteger()
class SmallInteger(Integer, int):
"""
SmallInteger field factory that construct Field classes and populated their values.
"""
_type = int
_sample = 0
def __new__( # type: ignore
cls,
*,
minimum: int = None,
maximum: int = None,
multiple_of: int = None,
**kwargs: Any
) -> BaseField:
autoincrement = kwargs.pop("autoincrement", None)
autoincrement = (
autoincrement
if autoincrement is not None
else kwargs.get("primary_key", False)
)
kwargs = {
**kwargs,
**{
k: v
for k, v in locals().items()
if k not in ["cls", "__class__", "kwargs"]
},
}
kwargs["ge"] = kwargs["minimum"]
kwargs["le"] = kwargs["maximum"]
return super().__new__(cls, **kwargs)
@classmethod
def get_column_type(cls, **kwargs: Any) -> Any:
"""
Return proper type of db column for given field type.
Accepts required and optional parameters that each column type accepts.
:param kwargs: key, value pairs of sqlalchemy options
:type kwargs: Any
:return: initialized column with proper options
:rtype: sqlalchemy Column
"""
return sqlalchemy.SmallInteger()
class Decimal(ModelFieldFactory, decimal.Decimal): class Decimal(ModelFieldFactory, decimal.Decimal):
""" """
Decimal field factory that construct Field classes and populated their values. Decimal field factory that construct Field classes and populated their values.

View File

@ -67,6 +67,7 @@ class Author(ormar.Model):
test_time = ormar.Time(default=datetime.time, **default_fernet) test_time = ormar.Time(default=datetime.time, **default_fernet)
test_json = ormar.JSON(default={}, **default_fernet) test_json = ormar.JSON(default={}, **default_fernet)
test_bigint: int = ormar.BigInteger(default=0, **default_fernet) test_bigint: int = ormar.BigInteger(default=0, **default_fernet)
test_smallint: int = ormar.SmallInteger(default=0, **default_fernet)
test_decimal = ormar.Decimal(scale=2, precision=10, **default_fernet) test_decimal = ormar.Decimal(scale=2, precision=10, **default_fernet)
test_decimal2 = ormar.Decimal(max_digits=10, decimal_places=2, **default_fernet) test_decimal2 = ormar.Decimal(max_digits=10, decimal_places=2, **default_fernet)
custom_backend: str = ormar.String( custom_backend: str = ormar.String(

View File

@ -42,6 +42,7 @@ class Organisation(ormar.Model):
ident: str = ormar.String(max_length=100, choices=["ACME Ltd", "Other ltd"]) ident: str = ormar.String(max_length=100, choices=["ACME Ltd", "Other ltd"])
priority: int = ormar.Integer(choices=[1, 2, 3, 4, 5]) priority: int = ormar.Integer(choices=[1, 2, 3, 4, 5])
priority2: int = ormar.BigInteger(choices=[1, 2, 3, 4, 5]) priority2: int = ormar.BigInteger(choices=[1, 2, 3, 4, 5])
priority3: int = ormar.SmallInteger(choices=[1, 2, 3, 4, 5])
expire_date: datetime.date = ormar.Date( expire_date: datetime.date = ormar.Date(
choices=[datetime.date(2021, 1, 1), datetime.date(2022, 5, 1)] choices=[datetime.date(2021, 1, 1), datetime.date(2022, 5, 1)]
) )

View File

@ -35,6 +35,7 @@ class ExampleModel(Model):
test_time = ormar.Time(default=datetime.time) test_time = ormar.Time(default=datetime.time)
test_json = ormar.JSON(default={}) test_json = ormar.JSON(default={})
test_bigint: int = ormar.BigInteger(default=0) test_bigint: int = ormar.BigInteger(default=0)
test_smallint: int = ormar.SmallInteger(default=0)
test_decimal = ormar.Decimal(scale=2, precision=10) test_decimal = ormar.Decimal(scale=2, precision=10)
test_decimal2 = ormar.Decimal(max_digits=10, decimal_places=2) test_decimal2 = ormar.Decimal(max_digits=10, decimal_places=2)