Skip to main content

django-ckc CKC

tools, utilities , etc. we use across projects @ ckc

installing

pip install django-ckc
# settings.py
INSTALLED_APPS = (
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.staticfiles",

    # ... add ckc
    "ckc",
)

tests

$ docker build -t django-ckc . && docker run django-ckc pytest

what's in this

SoftDeletableModel

Make your models have a deleted bool set when they are deleted instead of actuallying being deleted. Uses a model manager SoftDeleteModelManager to keep them hidden.

PrimaryKeyWriteSerializerReadField

A DRF field for writing via PK and reading via a serializer. Useful for when you want to connect 2 models together and immediately display to the user some useful information.

For example, if you had an Order model with LineItem objects pointing to it, it may be useful to create a new line item via order PK and return back the complete order with new totals and other calculations:

class LineItemUpdateSerializer(serializers.ModelSerializer):
    order = PrimaryKeyWriteSerializerReadField(
        queryset=Order.objects.all(),
        read_serializer=OrderDetailSerializer
    )
    
    class Meta:
        model = LineItem
        fields = ["id", "order", "product"]

POST data for adding product #123 to order #5 would look like

// REQUEST
{"order": 5, "product": 123}"}

// RESPONSE
{"order": {"total_amount": "$1,000.00"}, "product": 123}

DefaultCreatedByMixin for ModelSerializers

This will automatically set YourModel.created_by to request.user. To override which attribute the user is written to, add a user_field to your classes Meta information

class YourModel(models.Model):
    created_by = models.ForeignKey(User, on_delete=models.CASCADE)
        

class MySerializer(DefaultCreatedByMixin, ModelSerializer):
    class Meta:
        model = YourModel

DjangoGeoPointProvider

Helps generate geo points in Factory Boy factories.

# factories.py
class SomeLocationFactory(DjangoModelFactory):
    location = factory.Faker('geo_point', country_code='US')

    class Meta:
        model = SomeLocation

# test_whatever.py
from django.contrib.gis.geos import Point


class WhateverTest(TestCase):
    def test_something(self):
        SomeLocationFactory()  # random location
        SomeLocationFactory(location=Point(x=60, y=60))  # specified location

Slack logging

Get a Slack webhook URL and set SLACK_WEBHOOK_URL env var. You can also set DJANGO_SLACK_LOG_LEVEL with info, warning, etc.

Modify your Celery settings:

# Let our slack logger handle celery stuff
CELERY_WORKER_HIJACK_ROOT_LOGGER = False

Example LOGGING configuration that turns on Slack logging if SLACK_WEBHOOK_URL env var is found:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'colored': {
            '()': 'colorlog.ColoredFormatter',
            'format': "%(log_color)s%(levelname)-8s%(reset)s %(white)s%(message)s",
        }
    },
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'formatter': 'colored',
        },
    },
    'loggers': {
        '': {
            'handlers': ['console'],
            'level': os.getenv('DJANGO_LOG_LEVEL', 'INFO'),
        },
        'django': {
            'handlers': ['console'],
            'level': os.getenv('DJANGO_LOG_LEVEL', 'INFO'),
            'propagate': False,
        }
    },
}

SLACK_WEBHOOK_URL = os.getenv('SLACK_WEBHOOK_URL', '')
if SLACK_WEBHOOK_URL:
    LOGGING['handlers']['slack'] = {
        'class': 'ckc.logging.CkcSlackHandler',
        'level': os.getenv('DJANGO_SLACK_LOG_LEVEL', 'ERROR'),
    }

    LOGGING['loggers']['django']['handlers'] = ['console', 'slack']
    LOGGING['loggers']['']['handlers'] = ['console', 'slack']

SnackbarError

A little helper that when raised returns a JSON response to the user with a snackbar message. This message is automatically rendered as a snackbar by Skeletor.

# views.py
from ckc.exceptions import SnackbarError


class TestExceptionsViewSet(APIView):
    def get(self, request, *args, **kwargs):
        # This will return {"snackbar_message": "Something went wrong"}
        raise SnackbarError("Something went wrong")

./manage.py commands

command description
upload_file <source> <destination> uses django-storages settings to upload a file

Metadata

Release files for django-ckc 0.0.10

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

Source distribution (sdist)

Source distribution for django-ckc 0.0.10
File Size Uploaded
django-ckc-0.0.10.tar.gz 13.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-ckc 0.0.10
File Interpreter ABI Platform
django_ckc-0.0.10-py3-none-any.whl Python 3 none any Details

Total release size: 32.3 kB

Release files / django-ckc-0.0.10.tar.gz

Download URL django-ckc-0.0.10.tar.gz
Size 13.9 kB
Tags Source
SHA-256 checksum
How to use checksums
7eb95844dcdf09320799de0bdc591bfe72c0216fc2e32e57ee75e00504052430
BLAKE2b-256 checksum
How to use checksums
1da5c6480d08fc8e0d02965db263e77c66f126c7ace2271ff7e5ee720fdbd412
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.3

Release files / django_ckc-0.0.10-py3-none-any.whl

Download URL django_ckc-0.0.10-py3-none-any.whl
Size 18.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
88a5e08e2c2f8243ad5093bf54de03943d3b53cb8d8fb5259bc3a502191f720a
BLAKE2b-256 checksum
How to use checksums
161e39653f43fc481d5e07dbc7536dfd4f7dbac19fafd041f8036fb3b7d610bf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.3

Release history Release notifications | RSS feed

This release

0.0.10 This release

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.5

1 release file

0.0.4

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.1

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