Skip to content

Instantly share code, notes, and snippets.

@bennylope
bennylope / list_filter.py
Created November 16, 2012 16:33
Simplified filtering without validation
class MyListView(ListView):
def get(self, request, *args, **kwargs):
self.object_list = self.get_queryset()
search_query = request.GET.get('search_query')
if search_query:
self.object_list = self.object_list.filter(name=search_query)
context = self.get_context_data(object_list=self.object_list)
return self.render_to_response(context)
@bennylope
bennylope / commandments.md
Last active December 14, 2015 02:38
Commandments for Collaborative Programming: Coding and VCS workflow.
  1. Thou shalt make diffs meaningful
  2. Thou shalt meaningfully order merged commits
  3. Thou shalt not include superfluous merge commits
  4. Thou shalt make meaningful, well formatted commit messages
  5. Thou shalt only branch off of the canonical branch
  6. Thou shalt run tests before merging or issuing a pull request
  7. Thou shalt add tests for new code before merging or issuing a pull request
  8. Thou shalt not hard code settings, URL schemes, or app-domains
  9. Thou shalt follow a consistent project coding style
@bennylope
bennylope / README.md
Created February 27, 2013 19:21
Vagrant file setup (basic)

File structure

  • Vagrantfile - primary configuration
  • vagrantconfig.yaml - default configuration
  • vagrantconfig_local.yaml - local configuration, this file never gets checked into source control

Setup

The primary Vagrantfile is set up to use Ubuntu 12.04 LTS (Precise) and Puppet configuration has been commented out.

import re
import simplejson
from django.http import HttpResponse
from django.conf import settings
class JSONResponse(HttpResponse):
def __init__(self, request, data):
indent = 2 if settings.DEBUG else None
@bennylope
bennylope / wpversions.py
Created March 25, 2013 17:09
Queries a list of WordPress installations for the version as found in the <meta name='generator'> tag.
import csv
import requests
from bs4 import BeautifulSoup
def is_generator(tag):
return True if tag.attrs.get('name', '') == 'generator' else False
@bennylope
bennylope / simple_scraper.py
Created May 9, 2013 20:13
Super simple scraper that uses `requests` to fetch a single page and then uses BeautifulSoup to parse the meta description.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
import requests
from bs4 import BeautifulSoup
url = "http://www.wellfireinteractive.com"
@bennylope
bennylope / generic_csv_exporter_basic.py
Last active December 22, 2015 14:29
A simple way of avoiding duplication in CSV exports. This example takes advantage of Python's `attrgetter` function to get chained attributes using strings like "attr.subattr.subattr".
import csv
from operator import attrgetter
def generic_csv(csvfile, objects, fields):
writer = csv.writer(csvfile, quoting=csv.QUOTE_ALL)
row_headers = fields.keys()
writer.writerow(row_headers)
for obj in objects:
@bennylope
bennylope / managers.py
Created September 18, 2013 21:41
Manager class example for working with generic keys. Keep content type lookups inside the manager.
from django.db import models
from django.contrib.contenttypes.models import ContentType
class BookmarkManager(models.Manager):
def _lookup_kwargs(self, **kwargs):
owner = kwargs.pop('owner', None)
if owner:
owner_type = ContentType.objects.get_for_model(owner)
---
# ^^^ YAML documents must begin with the document separator "---"
#
#### Example docblock, I like to put a descriptive comment at the top of my
#### playbooks.
#
# Overview: Playbook to bootstrap a new host for configuration management.
# Applies to: production
# Description:
# Ensures that a host is configured for management with Ansible.
@bennylope
bennylope / gist:7662274
Created November 26, 2013 17:19
Convert shapefiles to SQL and import into an existing, named PostgreSQL database with PostGIS extention already installed.
for SHAPEFILE in $(find . -name "*.shp")
do
BASENAME=$(basename $SHAPEFILE .shp)
DIRNAME=$(dirname $SHAPEFILE)
SQLFILE="${BASENAME}.sql"
shp2pgsql -W UTF-8 $SHAPEFILE $BASENAME > "${DIRNAME}/${BASENAME}.sql"
psql -d richmond -f "${DIRNAME}/${BASENAME}.sql"
done