Merge pull request #297 from ProgrammerPlus1998/master

add:SmallInteger()
This commit is contained in:
collerek
2021-08-06 11:02:41 +02:00
committed by GitHub
11 changed files with 100 additions and 1 deletions

View File

@ -50,7 +50,7 @@ jobs:
python-version: ${{ matrix.python-version }} python-version: ${{ matrix.python-version }}
- name: Install dependencies - name: Install dependencies
run: | run: |
python -m pip install --upgrade pip python -m pip install pip==21.0.1
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Run mysql - name: Run mysql
env: env:

View File

@ -615,6 +615,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

@ -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

@ -586,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)]
) )
@ -109,6 +110,7 @@ def test_all_endpoints():
"ident": "ACME Ltd", "ident": "ACME Ltd",
"priority": 4, "priority": 4,
"priority2": 2, "priority2": 2,
"priority3": 1,
"expire_date": "2022-05-01", "expire_date": "2022-05-01",
"expire_time": "10:00:00", "expire_time": "10:00:00",
"expire_datetime": "2022-05-01T12:30:00", "expire_datetime": "2022-05-01T12:30:00",

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)