Skip to content

Instantly share code, notes, and snippets.

View supermanzer's full-sized avatar
🐒
Code Monkey

Ryan Manzer supermanzer

🐒
Code Monkey
View GitHub Profile
@supermanzer
supermanzer / cbv_mixins.py
Created June 17, 2021 20:24
Message Mixin for Django Class Based Views
"""
cbv_mixins.py - Provide useful mixins for Django's already useful Class Based Views.
* MessageMixin: Automates assigning of success, error messages to POST method
"""
class MessageMixin(object):
"""Automate addition of success/error messages if defined in the view."""
def get_success_message(self):
@supermanzer
supermanzer / model_admin_classes.py
Last active July 17, 2021 16:55
Django ModelAdmin Mixins - User Save Admin & Inlines
"""
Subclass Django admin classes to automate saving created_by, modified_by fields.
Requires models being registered have the necesssary fields.
"""
from django.contrib import admin
from django.contrib.contenttypes.admin import GenericTabularInline
class UserSaveModelAdmin(admin.ModelAdmin):
"""
@supermanzer
supermanzer / pagination.py
Created March 7, 2021 21:48
A simple pagination class for Django REST Framework that returns the number of pages (most client side pagination needs this)
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response
class PageCountPaginator(PageNumberPagination):
"""Implements all behaviors from `PageNumberPagination` class but results includet `page_count` value."""
def get_paginated_response(self, data):
return Response({
'count': self.page.paginator.count,
@supermanzer
supermanzer / viewset_mixin.py
Last active March 7, 2021 21:49
DRF List & Detail Viewset Mixin
class ListDetailMixin:
"""
Encapsulate the selection of separate serializers
based on the detail boolean of the View `self.detail`.
Requires View (or Viewset) have the following properties:
* list_serializer - For returning lists of records
* detail_serializer - For returning a single record (usually more fields)
"""
# For details regarding viewset attrbiutes please refer to the DRF documentation
@supermanzer
supermanzer / run_celery.sh
Created January 15, 2020 18:02
A simple bash script to spin up a Celery worker (often used in Docker container).
#!/usr/bin/env bash
# wait for RabbitMQ server to start
sleep 10
# If we have a stale PID file we need to remove it
if [[ -f w1.pid ]]; then
rm -f ./w1.pid
fi
@supermanzer
supermanzer / .env-template
Created December 23, 2019 16:32
Docker .ENV File Template for a Django - PostgreSQL - RabbitMQ - Celery app.
################################################################################
# DOCKER ENVIRONMENT VARIABLES TEMPLATE #
# Many of these settings are automatically used by Docker images when first #
# being created in an application. In other situations (like custom Django #
# settings) thees values are accessed by the server code through custom calls #
# like os.environ.get() #
################################################################################
# Django App Container Variables
SETTINGS_FILE= # <- SETTING FILE PATH GOES HERE
@supermanzer
supermanzer / template_mixin.py
Created February 15, 2019 19:27
Django JSON Template Mixin
from django.http import JsonResponse
from django.template.loader import render_to_string
class JsonTemplateMixin:
"""
A mixnin for returning a rendered template as a JSON response,
along with a value to indicate to a calling function whether the
rendering was successful or not.
"""
@supermanzer
supermanzer / context_processor.py
Created January 7, 2019 21:40
Useful Django Context Processor
"""
project.context_processors - Custom designed context processors.
These context processors handle setting additional context variables
in the Django request/response cycle.
Processors:
debug_var - This sends the DEBUG setting value to the template context as the `testing_env` variable.
"""