Skip to content

Instantly share code, notes, and snippets.

View dkarchmer's full-sized avatar

David Karchmer dkarchmer

View GitHub Profile
@dkarchmer
dkarchmer / aws-lambda-picture-resize
Created May 20, 2016 04:12
Sample JavaScript code for resizing pictures on AWS Lambda
/**
* This is based on: http://docs.aws.amazon.com/lambda/latest/dg/walkthrough-s3-events-adminuser.html
*/
// dependencies
var async = require('async');
var AWS = require('aws-sdk');
var gm = require('gm').subClass({ imageMagick: true }); // Enable ImageMagick integration.
var util = require('util');
var MAX_WIDTH;
@dkarchmer
dkarchmer / new-mac.md
Last active November 14, 2019 22:33
Setting up a new Mac for Django and Docker development

before

  • Enable Firewall
  • Enable FileVault
  • Disable all sharing

Installation script

0.- Install XCode Compiler

@dkarchmer
dkarchmer / django-filter-sample.py
Created May 2, 2016 00:26
How to use django-filter to add a DRF filter using dates and slugs
class SampleFilter(filters.FilterSet):
start_date = django_filters.DateFilter(name="date", lookup_type='gte')
end_date = django_filters.DateFilter(name="date", lookup_type='lte')
# How to filter by a foreign key that uses slug as a lookup
foo = django_filters.ModelMultipleChoiceFilter(
queryset=MyModel.objects.all(),
to_field_name='slug',
conjoined=True,
)
class Meta:
@dkarchmer
dkarchmer / supervisord.conf
Created April 26, 2016 17:01
Sample supervisord conf for running a python script in the background
; Assumes dockerfile with:
; ENTRYPOINT ["/usr/bin/supervisord", "-c", "/var/app/supervisord.conf"]
[supervisord]
;logfile=/var/app/logs/ ; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=5 ; (num of main logfile rotation backups;default 10)
loglevel=debug ; (log level;default info; others: debug,warn,trace)
pidfile=/tmp/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
nodaemon=true ; (start in foreground if true;default false)
@dkarchmer
dkarchmer / view_api_with_pagination.py
Created April 24, 2016 01:45
How to paginate a custom detail_route get on djangorestframework
# ViewSets define the view behavior.
class FooViewSet(viewsets.ModelViewSet):
lookup_field = 'slug'
queryset = Foo.objects.all()
serializer_class = FooSerializer
def get_queryset(self):
"""
This view should return a list of all records
@dkarchmer
dkarchmer / docker-machine-aws-create.sh
Created April 22, 2016 16:55
How to create a docker-machine for AWS
#!/bin/bash
echo "Creating docker machine on AWS: $1"
source .env
docker-machine create -d amazonec2 \
--amazonec2-access-key=$AWS_ACCESS_KEY_ID \
--amazonec2-secret-key=$AWS_SECRET_ACCESS_KEY \
--amazonec2-vpc-id=$AWS_VPC_ID \
--amazonec2-instance-type=c4.4xlarge \
@dkarchmer
dkarchmer / process_sqs_messages.py
Created April 14, 2016 20:09
Using Boto3 to process SQS messages
import boto3
# Get the service resource
sqs = boto3.resource('sqs')
# Get the queue. This returns an SQS.Queue instance
queue = sqs.get_queue_by_name(QueueName='my-queue')
# You can now access identifiers and attributes
logger.info(queue.url)
@dkarchmer
dkarchmer / django_request_factory_test.py
Last active September 20, 2023 06:19
Sample code for using RequestFactory to do Django Unit Testing - Get and Post
from django.test import TestCase, RequestFactory
from django.utils.importlib import import_module
from django.contrib.auth import get_user_model
from django.core.urlresolvers import reverse
from django.contrib.sessions.middleware import SessionMiddleware
from django.contrib.messages.middleware import MessageMiddleware
from rest_framework import status
from .models import *
@dkarchmer
dkarchmer / docker-compose-v2.yml
Created February 25, 2016 01:38
This shows a V2 Docker Compose example with a Django server/worker, redis, postgres, elasticcache and local dynamodb (for development)
version: '2'
services:
# Data
dbdata:
image: busybox
command: "true"
volumes:
- /var/lib/postgresql/data
- /data
@dkarchmer
dkarchmer / djangorestframework-client.py
Last active August 29, 2017 03:11
Semi-generic Python Rest Client, assuming a DjangoRestFramework back-end (and using TokenAuthentication)
__author__ = 'dkarchmer'
"""
See https://gist.github.com/dkarchmer/d85e55f9ed5450ba58cb
This API generically supports DjangoRestFramework based APIs
It is based on https://github.com/samgiles/slumber, but customized for
Django Rest Frameworks, and the use of TokenAuthentication.
Usage:
# Assuming
# v1_api_router.register(r'some_model', SomeModelViewSet)