Skip to main content

CommonKit

Installation

  • pip install commonkit

Examples

Obfuscation

from commonkit.utils import Obfuscator

Obfuscator.email("abcdqweqwe@test.com") # abcdq****@test.com'
Obfuscator.obfuscate('my_super_confidential_secret')  # 'my_super_confidential_s*****'
# 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 commonkit.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

  1. Assuming Pandas
import pandas as pd
from commonkit.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
  1. Assuming Pola.rs
import polars as pl
from commonkit.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
  • TableUploadField is write_only
  • TableUploadField is easily extended, use your own library if you want
from commonkit.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 commonkit.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 TableUploadField subclasses.

  • commonkit provides easily methods for overriding most of the logic.

  • If you want a field that does aggregation or something, override process_table on TableUploadField

Obfuscation

from commonkit.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": [
        "commonkit.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 poetry
  • poetry install
  • poetry shell
  • pre-commit install

To build the docs

  • make html

To run the tests

  • pytest .

Metadata

Release files for commonkit 1.0.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for commonkit 1.0.8
File Size Uploaded
commonkit-1.0.8.tar.gz 12.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for commonkit 1.0.8
File Interpreter ABI Platform
commonkit-1.0.8-py3-none-any.whl Python 3 none any Details

Total release size: 27.0 kB

Release files / commonkit-1.0.8.tar.gz

Download URL commonkit-1.0.8.tar.gz
Size 12.5 kB
Tags Source
SHA-256 checksum
How to use checksums
128186ac99295d4035d771f1a0a43d81d05433c99948f85961894b6c01289bfe
BLAKE2b-256 checksum
How to use checksums
30c7d0c64bc118b96f2a47a82891d4c4cb0978a9a291a324cfaa5a2487d393b3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.18

Release files / commonkit-1.0.8-py3-none-any.whl

Download URL commonkit-1.0.8-py3-none-any.whl
Size 14.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
956f1d51488900cfe3422609b7ff8a32bc7384474be0a5da3f85f9468fceae45
BLAKE2b-256 checksum
How to use checksums
a9a4f2fafe62f1935c7393ab564fdc71efa9d14e291355e7e0e5a23b8eda318d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.18

Release history Release notifications | RSS feed

This release

1.0.8 This release

2 release files

1.0.7

2 release files

1.0.6

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page