Package: django-ckc
django-ckctools, utilities
, etc. we use across projects @ckcinstallingpipinstalldjango-ckc# settings.pyINSTALLED_APPS=("django.contrib.auth","django.contrib.contenttypes","django.contrib.sessions","django.contrib.staticfiles",# ... add ckc"ckc",)tests$dockerbuild-tdjango-ckc.&&dockerrundjango-ckcpytestwhat's in thisSoftDeletableModelMake your models have adeletedbool set when they are deleted instead of actuallying
being deleted. Uses a model managerSoftDeleteModelManagerto keep them hidden.PrimaryKeyWriteSerializerReadFieldA 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 anOrdermodel withLineItemobjects 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:classLineItemUpdateSerializer(serializers.ModelSerializer):order=PrimaryKeyWriteSerializerReadField(queryset=Order.objects.all(),read_serializer=OrderDetailSerializer)classMeta:model=LineItemfields=["id","order","product"]POSTdata for adding product #123 to order #5 would look like// REQUEST{"order":5,"product":123}"}// RESPONSE{"order": {"total_amount": "$1,000.00"}, "product":123}DefaultCreatedByMixinforModelSerializersThis will automatically setYourModel.created_bytorequest.user. To override which
attribute the user is written to, add auser_fieldto your classes Meta informationclassYourModel(models.Model):created_by=models.ForeignKey(User,on_delete=models.CASCADE)classMySerializer(DefaultCreatedByMixin,ModelSerializer):classMeta:model=YourModelDjangoGeoPointProviderHelps generate geo points in Factory Boy factories.# factories.pyclassSomeLocationFactory(DjangoModelFactory):location=factory.Faker('geo_point',country_code='US')classMeta:model=SomeLocation# test_whatever.pyfromdjango.contrib.gis.geosimportPointclassWhateverTest(TestCase):deftest_something(self):SomeLocationFactory()# random locationSomeLocationFactory(location=Point(x=60,y=60))# specified locationSlack loggingGet a Slack webhook URL and setSLACK_WEBHOOK_URLenv var. You can also setDJANGO_SLACK_LOG_LEVELwith info, warning, etc.Modify your Celery settings:# Let our slack logger handle celery stuffCELERY_WORKER_HIJACK_ROOT_LOGGER=FalseExampleLOGGINGconfiguration that turns on Slack logging ifSLACK_WEBHOOK_URLenv 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','')ifSLACK_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']SnackbarErrorA 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.pyfromckc.exceptionsimportSnackbarErrorclassTestExceptionsViewSet(APIView):defget(self,request,*args,**kwargs):# This will return {"snackbar_message": "Something went wrong"}raiseSnackbarError("Something went wrong")./manage.pycommandscommanddescriptionupload_file <source> <destination>usesdjango-storagessettings to upload a file