sqlalchemy-serializer-meta 0.1.0__tar.gz → 0.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {sqlalchemy_serializer_meta-0.1.0/sqlalchemy_serializer_meta.egg-info → sqlalchemy_serializer_meta-0.2.0}/PKG-INFO +84 -1
- {sqlalchemy_serializer_meta-0.1.0 → sqlalchemy_serializer_meta-0.2.0}/README.md +83 -0
- {sqlalchemy_serializer_meta-0.1.0 → sqlalchemy_serializer_meta-0.2.0}/setup.py +2 -6
- {sqlalchemy_serializer_meta-0.1.0 → sqlalchemy_serializer_meta-0.2.0}/sqlalchemy_serializer_meta/__init__.py +7 -5
- sqlalchemy_serializer_meta-0.2.0/sqlalchemy_serializer_meta/validator.py +20 -0
- {sqlalchemy_serializer_meta-0.1.0 → sqlalchemy_serializer_meta-0.2.0/sqlalchemy_serializer_meta.egg-info}/PKG-INFO +84 -1
- {sqlalchemy_serializer_meta-0.1.0 → sqlalchemy_serializer_meta-0.2.0}/sqlalchemy_serializer_meta.egg-info/SOURCES.txt +1 -0
- {sqlalchemy_serializer_meta-0.1.0 → sqlalchemy_serializer_meta-0.2.0}/LICENSE +0 -0
- {sqlalchemy_serializer_meta-0.1.0 → sqlalchemy_serializer_meta-0.2.0}/setup.cfg +0 -0
- {sqlalchemy_serializer_meta-0.1.0 → sqlalchemy_serializer_meta-0.2.0}/sqlalchemy_serializer_meta/serializer.py +0 -0
- {sqlalchemy_serializer_meta-0.1.0 → sqlalchemy_serializer_meta-0.2.0}/sqlalchemy_serializer_meta.egg-info/dependency_links.txt +0 -0
- {sqlalchemy_serializer_meta-0.1.0 → sqlalchemy_serializer_meta-0.2.0}/sqlalchemy_serializer_meta.egg-info/requires.txt +0 -0
- {sqlalchemy_serializer_meta-0.1.0 → sqlalchemy_serializer_meta-0.2.0}/sqlalchemy_serializer_meta.egg-info/top_level.txt +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: sqlalchemy_serializer_meta
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.2.0
|
|
4
4
|
Summary: A simple serializer for SQLAlchemy models and Flask Models
|
|
5
5
|
Home-page: https://github.com/Wehda23/sqlalchemy_serializer_meta
|
|
6
6
|
Author: Waheed Khaled Elhariri
|
|
@@ -24,10 +24,21 @@ Requires-Dist: pydantic_core==2.18.4
|
|
|
24
24
|
|
|
25
25
|
This module provides a set of serializers for your Flask application, allowing you to easily validate and serialize data. The serializers are built with flexibility and extendability in mind, providing features such as nested serialization and integration with Pydantic models.
|
|
26
26
|
|
|
27
|
+
## 📜 Table of Contents
|
|
28
|
+
1. [Installation](#installation)
|
|
29
|
+
2. [Basic Usage](#usage)
|
|
30
|
+
3. [Creating Custom Serializers](#custom-serializers)
|
|
31
|
+
4. [Validators & Other Utilities](#validators--other-utilities)
|
|
32
|
+
5. [Support My Work](#support-my-work)
|
|
33
|
+
|
|
27
34
|
## Installation
|
|
28
35
|
|
|
29
36
|
To use the serializers in your project, simply include the necessary files in your application directory.
|
|
30
37
|
|
|
38
|
+
```bash
|
|
39
|
+
pip install sqlalchemy-serializer-meta==0.1.0
|
|
40
|
+
```
|
|
41
|
+
|
|
31
42
|
## Usage
|
|
32
43
|
|
|
33
44
|
Can be used to Serialize SQLAlchemy Model or Flask-SQLAlchemy model.
|
|
@@ -390,3 +401,75 @@ if __name__ == '__main__':
|
|
|
390
401
|
- **Focus on Business Logic:** By handling validation and serialization within the serializer, the main view functions can focus on business logic and API response handling, making the codebase cleaner and more maintainable.
|
|
391
402
|
|
|
392
403
|
By adopting this design, you can create scalable, maintainable, and robust APIs that leverage the power of SQLAlchemy and Flask-SQLAlchemy, ensuring that your application remains flexible and adaptable to changing requirements.
|
|
404
|
+
|
|
405
|
+
|
|
406
|
+
## Validators & Other Utilities
|
|
407
|
+
|
|
408
|
+
You can add a `read_only` to class meta to specify fields that can only be retrieved but not be updated.
|
|
409
|
+
|
|
410
|
+
Also you can add a `write_only` to a class to specify fields that can be put as inputs but never retrieved
|
|
411
|
+
|
|
412
|
+
By default field `password` is passively added as a `write_only` field.
|
|
413
|
+
|
|
414
|
+
|
|
415
|
+
In your application `validators.py`:
|
|
416
|
+
|
|
417
|
+
```py
|
|
418
|
+
from sqlalchemy_serializer_meta.validator import BaseValidator
|
|
419
|
+
|
|
420
|
+
class CustomValidator(BaseValidator):
|
|
421
|
+
def validate(self, value):
|
|
422
|
+
if not value:
|
|
423
|
+
raise self.raise_exception(ValueError, 'Value cannot be empty')
|
|
424
|
+
|
|
425
|
+
```
|
|
426
|
+
|
|
427
|
+
In your application `serializers.py`:
|
|
428
|
+
|
|
429
|
+
```py
|
|
430
|
+
from sqlalchemy_serializer_meta import Serializer
|
|
431
|
+
from your_application.models import CustomModel
|
|
432
|
+
from your_application.validators import CustomValidator
|
|
433
|
+
from pydantic import BaseModel, EmailStr, ValidationError
|
|
434
|
+
from typing import Union
|
|
435
|
+
|
|
436
|
+
class CustomPydantic(BaseModel):
|
|
437
|
+
id: str
|
|
438
|
+
name: str
|
|
439
|
+
description: EmailStr
|
|
440
|
+
|
|
441
|
+
class CustomModelSerializer(Serializer):
|
|
442
|
+
pydantic_model: BaseModel = CustomPydantic
|
|
443
|
+
|
|
444
|
+
class Meta:
|
|
445
|
+
model = CustomModel
|
|
446
|
+
fields = ['id', 'name', 'description']
|
|
447
|
+
read_only = ['id']
|
|
448
|
+
write_only = ['name']
|
|
449
|
+
|
|
450
|
+
def validate_name(self, value):
|
|
451
|
+
CustomValidator(SerializerError).validate(value)
|
|
452
|
+
return value
|
|
453
|
+
```
|
|
454
|
+
|
|
455
|
+
## Support My Work
|
|
456
|
+
|
|
457
|
+
If you find my free content and projects helpful, consider supporting me to enable more learning resources for the community. Your contributions will go towards creating new content and maintaining existing projects.
|
|
458
|
+
|
|
459
|
+
### How to Contribute
|
|
460
|
+
|
|
461
|
+
You can contribute by making a donation through PayPal:
|
|
462
|
+
|
|
463
|
+
[](https://paypal.me/Waheedkhaled?country.x=EG&locale.x=en_US)
|
|
464
|
+
|
|
465
|
+
You can contribute by making donation through Buy me coffee:
|
|
466
|
+
|
|
467
|
+
[](https://www.buymeacoffee.com/waheedKh)
|
|
468
|
+
|
|
469
|
+
Your generosity is greatly appreciated!
|
|
470
|
+
|
|
471
|
+
### Why Contribute?
|
|
472
|
+
|
|
473
|
+
By supporting this project, you help ensure the continued availability of free educational content and the development of new projects. Your contribution makes a meaningful impact on the community of learners.
|
|
474
|
+
|
|
475
|
+
Thank you for your support!
|
|
@@ -2,10 +2,21 @@
|
|
|
2
2
|
|
|
3
3
|
This module provides a set of serializers for your Flask application, allowing you to easily validate and serialize data. The serializers are built with flexibility and extendability in mind, providing features such as nested serialization and integration with Pydantic models.
|
|
4
4
|
|
|
5
|
+
## 📜 Table of Contents
|
|
6
|
+
1. [Installation](#installation)
|
|
7
|
+
2. [Basic Usage](#usage)
|
|
8
|
+
3. [Creating Custom Serializers](#custom-serializers)
|
|
9
|
+
4. [Validators & Other Utilities](#validators--other-utilities)
|
|
10
|
+
5. [Support My Work](#support-my-work)
|
|
11
|
+
|
|
5
12
|
## Installation
|
|
6
13
|
|
|
7
14
|
To use the serializers in your project, simply include the necessary files in your application directory.
|
|
8
15
|
|
|
16
|
+
```bash
|
|
17
|
+
pip install sqlalchemy-serializer-meta==0.1.0
|
|
18
|
+
```
|
|
19
|
+
|
|
9
20
|
## Usage
|
|
10
21
|
|
|
11
22
|
Can be used to Serialize SQLAlchemy Model or Flask-SQLAlchemy model.
|
|
@@ -368,3 +379,75 @@ if __name__ == '__main__':
|
|
|
368
379
|
- **Focus on Business Logic:** By handling validation and serialization within the serializer, the main view functions can focus on business logic and API response handling, making the codebase cleaner and more maintainable.
|
|
369
380
|
|
|
370
381
|
By adopting this design, you can create scalable, maintainable, and robust APIs that leverage the power of SQLAlchemy and Flask-SQLAlchemy, ensuring that your application remains flexible and adaptable to changing requirements.
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
## Validators & Other Utilities
|
|
385
|
+
|
|
386
|
+
You can add a `read_only` to class meta to specify fields that can only be retrieved but not be updated.
|
|
387
|
+
|
|
388
|
+
Also you can add a `write_only` to a class to specify fields that can be put as inputs but never retrieved
|
|
389
|
+
|
|
390
|
+
By default field `password` is passively added as a `write_only` field.
|
|
391
|
+
|
|
392
|
+
|
|
393
|
+
In your application `validators.py`:
|
|
394
|
+
|
|
395
|
+
```py
|
|
396
|
+
from sqlalchemy_serializer_meta.validator import BaseValidator
|
|
397
|
+
|
|
398
|
+
class CustomValidator(BaseValidator):
|
|
399
|
+
def validate(self, value):
|
|
400
|
+
if not value:
|
|
401
|
+
raise self.raise_exception(ValueError, 'Value cannot be empty')
|
|
402
|
+
|
|
403
|
+
```
|
|
404
|
+
|
|
405
|
+
In your application `serializers.py`:
|
|
406
|
+
|
|
407
|
+
```py
|
|
408
|
+
from sqlalchemy_serializer_meta import Serializer
|
|
409
|
+
from your_application.models import CustomModel
|
|
410
|
+
from your_application.validators import CustomValidator
|
|
411
|
+
from pydantic import BaseModel, EmailStr, ValidationError
|
|
412
|
+
from typing import Union
|
|
413
|
+
|
|
414
|
+
class CustomPydantic(BaseModel):
|
|
415
|
+
id: str
|
|
416
|
+
name: str
|
|
417
|
+
description: EmailStr
|
|
418
|
+
|
|
419
|
+
class CustomModelSerializer(Serializer):
|
|
420
|
+
pydantic_model: BaseModel = CustomPydantic
|
|
421
|
+
|
|
422
|
+
class Meta:
|
|
423
|
+
model = CustomModel
|
|
424
|
+
fields = ['id', 'name', 'description']
|
|
425
|
+
read_only = ['id']
|
|
426
|
+
write_only = ['name']
|
|
427
|
+
|
|
428
|
+
def validate_name(self, value):
|
|
429
|
+
CustomValidator(SerializerError).validate(value)
|
|
430
|
+
return value
|
|
431
|
+
```
|
|
432
|
+
|
|
433
|
+
## Support My Work
|
|
434
|
+
|
|
435
|
+
If you find my free content and projects helpful, consider supporting me to enable more learning resources for the community. Your contributions will go towards creating new content and maintaining existing projects.
|
|
436
|
+
|
|
437
|
+
### How to Contribute
|
|
438
|
+
|
|
439
|
+
You can contribute by making a donation through PayPal:
|
|
440
|
+
|
|
441
|
+
[](https://paypal.me/Waheedkhaled?country.x=EG&locale.x=en_US)
|
|
442
|
+
|
|
443
|
+
You can contribute by making donation through Buy me coffee:
|
|
444
|
+
|
|
445
|
+
[](https://www.buymeacoffee.com/waheedKh)
|
|
446
|
+
|
|
447
|
+
Your generosity is greatly appreciated!
|
|
448
|
+
|
|
449
|
+
### Why Contribute?
|
|
450
|
+
|
|
451
|
+
By supporting this project, you help ensure the continued availability of free educational content and the development of new projects. Your contribution makes a meaningful impact on the community of learners.
|
|
452
|
+
|
|
453
|
+
Thank you for your support!
|
|
@@ -2,7 +2,7 @@ from setuptools import setup, find_packages
|
|
|
2
2
|
|
|
3
3
|
setup(
|
|
4
4
|
name="sqlalchemy_serializer_meta",
|
|
5
|
-
version="0.
|
|
5
|
+
version="0.2.0",
|
|
6
6
|
description="A simple serializer for SQLAlchemy models and Flask Models",
|
|
7
7
|
long_description=open("README.md").read(),
|
|
8
8
|
long_description_content_type="text/markdown",
|
|
@@ -11,11 +11,7 @@ setup(
|
|
|
11
11
|
author_email="waheedkhaled95@gmail.com",
|
|
12
12
|
license="MIT",
|
|
13
13
|
packages=find_packages(),
|
|
14
|
-
install_requires=[
|
|
15
|
-
"sqlalchemy",
|
|
16
|
-
"pydantic==2.7.4",
|
|
17
|
-
"pydantic_core==2.18.4"
|
|
18
|
-
],
|
|
14
|
+
install_requires=["sqlalchemy", "pydantic==2.7.4", "pydantic_core==2.18.4"],
|
|
19
15
|
classifiers=[
|
|
20
16
|
"Development Status :: 4 - Beta",
|
|
21
17
|
"Intended Audience :: Developers",
|
|
@@ -156,7 +156,9 @@ class Serializer(SerializerInterface):
|
|
|
156
156
|
"input": (
|
|
157
157
|
value
|
|
158
158
|
if key != "password"
|
|
159
|
-
else ""
|
|
159
|
+
else ""
|
|
160
|
+
if key in self.write_only
|
|
161
|
+
else ""
|
|
160
162
|
),
|
|
161
163
|
"type": f"{e.args[0].__name__}",
|
|
162
164
|
}
|
|
@@ -169,7 +171,9 @@ class Serializer(SerializerInterface):
|
|
|
169
171
|
"input": (
|
|
170
172
|
value
|
|
171
173
|
if key != "password"
|
|
172
|
-
else ""
|
|
174
|
+
else ""
|
|
175
|
+
if key in self.write_only
|
|
176
|
+
else ""
|
|
173
177
|
),
|
|
174
178
|
"type": f"{type(e).__name__}",
|
|
175
179
|
}
|
|
@@ -363,9 +367,7 @@ class Serializer(SerializerInterface):
|
|
|
363
367
|
return hasattr(instance, "__table__") and isinstance(instance.__table__, Table)
|
|
364
368
|
|
|
365
369
|
def __serialize_relationship(
|
|
366
|
-
self,
|
|
367
|
-
nested_serializer: Optional["Serializer"],
|
|
368
|
-
relationship: object,
|
|
370
|
+
self, nested_serializer: Optional["Serializer"], relationship: object,
|
|
369
371
|
) -> Any:
|
|
370
372
|
"""Helper function to serialize a single nested relationship."""
|
|
371
373
|
if nested_serializer and isinstance(nested_serializer, Serializer):
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Module Contains base validator implementation.
|
|
3
|
+
"""
|
|
4
|
+
from abc import ABC, abstractmethod
|
|
5
|
+
from typing import Self
|
|
6
|
+
from . import SerializerError
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
# Abstracted Validator Class
|
|
10
|
+
class BaseValidator(ABC):
|
|
11
|
+
"""
|
|
12
|
+
Base Validation class that utilizes `SerializerError` from package as it's default raise_exception
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
def __init__(self: Self, raise_exception: SerializerError = SerializerError):
|
|
16
|
+
self.raise_exception: SerializerError = raise_exception
|
|
17
|
+
|
|
18
|
+
@abstractmethod
|
|
19
|
+
def validate(self, data):
|
|
20
|
+
pass
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: sqlalchemy_serializer_meta
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.2.0
|
|
4
4
|
Summary: A simple serializer for SQLAlchemy models and Flask Models
|
|
5
5
|
Home-page: https://github.com/Wehda23/sqlalchemy_serializer_meta
|
|
6
6
|
Author: Waheed Khaled Elhariri
|
|
@@ -24,10 +24,21 @@ Requires-Dist: pydantic_core==2.18.4
|
|
|
24
24
|
|
|
25
25
|
This module provides a set of serializers for your Flask application, allowing you to easily validate and serialize data. The serializers are built with flexibility and extendability in mind, providing features such as nested serialization and integration with Pydantic models.
|
|
26
26
|
|
|
27
|
+
## 📜 Table of Contents
|
|
28
|
+
1. [Installation](#installation)
|
|
29
|
+
2. [Basic Usage](#usage)
|
|
30
|
+
3. [Creating Custom Serializers](#custom-serializers)
|
|
31
|
+
4. [Validators & Other Utilities](#validators--other-utilities)
|
|
32
|
+
5. [Support My Work](#support-my-work)
|
|
33
|
+
|
|
27
34
|
## Installation
|
|
28
35
|
|
|
29
36
|
To use the serializers in your project, simply include the necessary files in your application directory.
|
|
30
37
|
|
|
38
|
+
```bash
|
|
39
|
+
pip install sqlalchemy-serializer-meta==0.1.0
|
|
40
|
+
```
|
|
41
|
+
|
|
31
42
|
## Usage
|
|
32
43
|
|
|
33
44
|
Can be used to Serialize SQLAlchemy Model or Flask-SQLAlchemy model.
|
|
@@ -390,3 +401,75 @@ if __name__ == '__main__':
|
|
|
390
401
|
- **Focus on Business Logic:** By handling validation and serialization within the serializer, the main view functions can focus on business logic and API response handling, making the codebase cleaner and more maintainable.
|
|
391
402
|
|
|
392
403
|
By adopting this design, you can create scalable, maintainable, and robust APIs that leverage the power of SQLAlchemy and Flask-SQLAlchemy, ensuring that your application remains flexible and adaptable to changing requirements.
|
|
404
|
+
|
|
405
|
+
|
|
406
|
+
## Validators & Other Utilities
|
|
407
|
+
|
|
408
|
+
You can add a `read_only` to class meta to specify fields that can only be retrieved but not be updated.
|
|
409
|
+
|
|
410
|
+
Also you can add a `write_only` to a class to specify fields that can be put as inputs but never retrieved
|
|
411
|
+
|
|
412
|
+
By default field `password` is passively added as a `write_only` field.
|
|
413
|
+
|
|
414
|
+
|
|
415
|
+
In your application `validators.py`:
|
|
416
|
+
|
|
417
|
+
```py
|
|
418
|
+
from sqlalchemy_serializer_meta.validator import BaseValidator
|
|
419
|
+
|
|
420
|
+
class CustomValidator(BaseValidator):
|
|
421
|
+
def validate(self, value):
|
|
422
|
+
if not value:
|
|
423
|
+
raise self.raise_exception(ValueError, 'Value cannot be empty')
|
|
424
|
+
|
|
425
|
+
```
|
|
426
|
+
|
|
427
|
+
In your application `serializers.py`:
|
|
428
|
+
|
|
429
|
+
```py
|
|
430
|
+
from sqlalchemy_serializer_meta import Serializer
|
|
431
|
+
from your_application.models import CustomModel
|
|
432
|
+
from your_application.validators import CustomValidator
|
|
433
|
+
from pydantic import BaseModel, EmailStr, ValidationError
|
|
434
|
+
from typing import Union
|
|
435
|
+
|
|
436
|
+
class CustomPydantic(BaseModel):
|
|
437
|
+
id: str
|
|
438
|
+
name: str
|
|
439
|
+
description: EmailStr
|
|
440
|
+
|
|
441
|
+
class CustomModelSerializer(Serializer):
|
|
442
|
+
pydantic_model: BaseModel = CustomPydantic
|
|
443
|
+
|
|
444
|
+
class Meta:
|
|
445
|
+
model = CustomModel
|
|
446
|
+
fields = ['id', 'name', 'description']
|
|
447
|
+
read_only = ['id']
|
|
448
|
+
write_only = ['name']
|
|
449
|
+
|
|
450
|
+
def validate_name(self, value):
|
|
451
|
+
CustomValidator(SerializerError).validate(value)
|
|
452
|
+
return value
|
|
453
|
+
```
|
|
454
|
+
|
|
455
|
+
## Support My Work
|
|
456
|
+
|
|
457
|
+
If you find my free content and projects helpful, consider supporting me to enable more learning resources for the community. Your contributions will go towards creating new content and maintaining existing projects.
|
|
458
|
+
|
|
459
|
+
### How to Contribute
|
|
460
|
+
|
|
461
|
+
You can contribute by making a donation through PayPal:
|
|
462
|
+
|
|
463
|
+
[](https://paypal.me/Waheedkhaled?country.x=EG&locale.x=en_US)
|
|
464
|
+
|
|
465
|
+
You can contribute by making donation through Buy me coffee:
|
|
466
|
+
|
|
467
|
+
[](https://www.buymeacoffee.com/waheedKh)
|
|
468
|
+
|
|
469
|
+
Your generosity is greatly appreciated!
|
|
470
|
+
|
|
471
|
+
### Why Contribute?
|
|
472
|
+
|
|
473
|
+
By supporting this project, you help ensure the continued availability of free educational content and the development of new projects. Your contribution makes a meaningful impact on the community of learners.
|
|
474
|
+
|
|
475
|
+
Thank you for your support!
|
|
@@ -4,6 +4,7 @@ setup.cfg
|
|
|
4
4
|
setup.py
|
|
5
5
|
sqlalchemy_serializer_meta/__init__.py
|
|
6
6
|
sqlalchemy_serializer_meta/serializer.py
|
|
7
|
+
sqlalchemy_serializer_meta/validator.py
|
|
7
8
|
sqlalchemy_serializer_meta.egg-info/PKG-INFO
|
|
8
9
|
sqlalchemy_serializer_meta.egg-info/SOURCES.txt
|
|
9
10
|
sqlalchemy_serializer_meta.egg-info/dependency_links.txt
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|