Skip to main content

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

Project description

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']

./manage.py commands

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-ckc-0.0.9.tar.gz (13.0 kB view details)

Uploaded Source

Built Distribution

django_ckc-0.0.9-py3-none-any.whl (17.0 kB view details)

Uploaded Python 3

File details

Details for the file django-ckc-0.0.9.tar.gz.

File metadata

  • Download URL: django-ckc-0.0.9.tar.gz
  • Upload date:
  • Size: 13.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.8

File hashes

Hashes for django-ckc-0.0.9.tar.gz
Algorithm Hash digest
SHA256 34b555311f12feb0b7cb361e868b912d0360d4ff3fa42c53f28e303ee9b5e385
MD5 6c8c890b92605223adea6c0c04974ef6
BLAKE2b-256 64a08f3ffbf980a27149b538f8f66dbbcbe62c70a887a155ab9ce6d7cf2ac2df

See more details on using hashes here.

File details

Details for the file django_ckc-0.0.9-py3-none-any.whl.

File metadata

  • Download URL: django_ckc-0.0.9-py3-none-any.whl
  • Upload date:
  • Size: 17.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.8

File hashes

Hashes for django_ckc-0.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 7cc6d1598875a449c57fafcaa3f5add709a925ac7855a492f39bc44d35155be1
MD5 1cb419dbab4ee91f171efdd3dc263bbf
BLAKE2b-256 69ddb36a620fd49fd26bfb9918e2f7765cf2f4e347ecb1ee01f6df7985c3f01a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page