A library for handling common stuff in Django
Project description
DjKit
Installation
pip install djkit
Examples
Obfuscation
from djkit.utils import Obfuscator
Obfuscator.email("abcdqweqwe@test.com") # abcdq****@test.com'
Obfuscator.obfuscate('my_super_confidential_secret') # 'my_super_confidential_s*****'
Django rest framework related examples
# models.py
from django.db import models
class Human(models.Model):
class Level(models.IntegerChoices):
BEGINNER = 0
INTERMEDIATE = 1
ADVANCED = 2
class MilitaryStatus(models.TextChoices):
EXEMPTED = "exempted", "Exempted"
SERVED = "served", "Served"
POSTPONED = "postponed", "Postponed"
name = models.CharField(max_length=128)
level = models.IntegerField(choices=Level.choices)
military_status = models.CharField(
choices=MilitaryStatus.choices,
max_length=128
)
# serializers.py
from rest_framework import serializers
from djkit.rest_framework.serializers import EnumSerializer
from example.core import models
class HumanSerializer(serializers.ModelSerializer):
level = EnumSerializer(enum=models.Human.Level)
military_status = EnumSerializer(enum=models.Human.MilitaryStatus)
class Meta:
model = models.Human
fields = [
'id',
'level',
'military_status',
]
Example serialization
# wherever.py
from example.core.serializers import HumanSerializer
from example.core.models import Human
human = Human.objects.find(id=1)
serializer = HumanSerializer(human) # level = BEGINNER, military_status = EXEMPTED
Example deserialization
# wherever.py
from example.core.serializers import HumanSerializer
data = {
"level": "BEGINNER",
"MILITARY_STATUS": "EXEMPTED",
}
instance = HumanSerializer(data=data)
instance.is_valid()
instance.save()
Got some endpoints that accept files in table formats? got you
- Assuming Pandas
import pandas as pd
from djkit.rest_framework.pandas import TableUploadField # or PandasTableUploadField
from rest_framework import serializers
class MySerializer(serializers.ModelSerializer):
file = TableUploadField()
def create(self, validated_data):
file: pd.DataFrame = validated_data['file']
# do logic, don't do row based validation here
return validated_data
# optional method
def validate_file_row(self, row, index, table_df):
if row.iloc[0] == "X":
row.iloc[0] = "Y"
# return new row, or None and changes won't be reflected
return row
- Assuming Pola.rs
import polars as pl
from djkit.rest_framework.polars import TableUploadField # or PolarsTableUploadField
from rest_framework import serializers
class MySerializer(serializers.ModelSerializer):
file = TableUploadField()
def create(self, validated_data):
file: pl.DataFrame = validated_data['file']
# do logic, don't do row based validation here
return validated_data
# optional method
def validate_file_row(self, row, index, table_df):
# do polars logic, return new row or None
return row
TableUploadFieldiswrite_only- TableUploadField is easily extended, use your own library if you want
from djkit.rest_framework.serializers import TableUploadField as BaseTableUploadField
def read_csv(source):
return ...
def read_excel(source):
return ...
class TableUploadField(BaseTableUploadField):
handlers = {
"csv": read_csv,
"xlsx": read_excel,
"xls": read_excel,
"xlsm": read_excel,
"xlsb": read_excel,
"odf": read_excel,
"ods": read_excel,
"odt": read_excel,
}
def update_row(self, table_object: "YourLibraryDataFrame", index, new_row):
"""how your library updates the row"""
Supported formats are the keys in handlers
can_parse_csv = "csv" in field.allowed_upload_formats
Or
can_parse_csv = field.is_allowed_format('obj') # False
Custom kwargs for handlers in TableFieldUpload
from djkit.rest_framework.pandas import PandasTableUploadField
import pandas as pd
class Serializer(...):
pandas_file = PandasTableUploadField(handler_kwargs={
# by format
"xlsx": {
# args for read_excel would be here
"engine": "openpyxl"
},
# or by reference
pd.read_csv: {
"delimiter": "\t"
}
})
-
The same logic applies to all
TableUploadFieldsubclasses. -
djkit provides easily methods for overriding most of the logic.
-
If you want a field that does aggregation or something, override
process_tableonTableUploadField
Obfuscation
from djkit.rest_framework.serializers import ObfuscatedCharField, ObfuscatedEmailField, ObfuscatedFieldMixin
class MySerializer(...):
email = ObfuscatedEmailField() # in the API, it's obfuscated
name = ObfuscatedCharField() # same applies here
class MyCustomObfuscatedApiKeyField(ObfuscatedFieldMixin, MyApiKeyField):
pass
Better error handling
REST_FRAMEWORK = {
"DEFAULT_RENDERER_CLASSES": [
"djkit.rest_framework.renderers.JSONRenderer"
]
}
This will change you response schema in case of errors to
response = {
"field_errors": [],
"non_field_errors": []
}
Contributions
To run the project locally
pip install poetrypoetry installpoetry shellpre-commit install
To build the docs
make html
To run the tests
pytest .
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file djkit-1.0.9.tar.gz.
File metadata
- Download URL: djkit-1.0.9.tar.gz
- Upload date:
- Size: 13.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0ead19c44a77c8fcc6a05ba092fd75c78fb4ea82afa84c318f38ff4d08dc2bc2
|
|
| MD5 |
0add178b4f9a36041f8f1775a2e101b1
|
|
| BLAKE2b-256 |
98c15ff5937fab54f3e5060ee8f46511780dc1c0696590354fa719d05c813089
|
File details
Details for the file djkit-1.0.9-py3-none-any.whl.
File metadata
- Download URL: djkit-1.0.9-py3-none-any.whl
- Upload date:
- Size: 15.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
43fbf3e2e0f421ba60d9a03db3ab448daffcb059a9f3fddeaf232ee15c648de7
|
|
| MD5 |
c624519cf6f35c8a70185df2dc5ca311
|
|
| BLAKE2b-256 |
1e4ac5f89645136ed5980ccfb71a0d1cc9878f42f219fb86d3f595ecf254af92
|