Skip to content

Instantly share code, notes, and snippets.

View dstegelman's full-sized avatar

Derek Stegelman dstegelman

  • Colorado State University @csu-chhs
  • Northern Colorado, USA
View GitHub Profile
@dstegelman
dstegelman / cutomqueryset.py
Last active October 1, 2015 20:38
Chainable Custom Model Manager
from django.db.models.query import QuerySet
from django.db.models import Manager
from datetime import date
class EventQuerySet(QuerySet):
def published(self):
return self.filter(published=True)
def available_for_adding(self):
@dstegelman
dstegelman / grouptemplatetag.py
Last active October 7, 2015 11:08
Check for group affiliation in Django tempalte
from django import template
from django.template import resolve_variable
from django.contrib.auth.models import Group
register = template.Library()
@register.tag()
def ifusergroup(parser, token):
""" Check to see if the currently logged in user belongs to a specific
group. Requires the Django authentication contrib app and middleware.
@dstegelman
dstegelman / gist:3991212
Created November 1, 2012 02:14
Django/Python Custom Logging config
LOG_LOCATION = './logs'
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'verbose': {
'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
},
'json': {
@dstegelman
dstegelman / request_in_form.py
Created March 20, 2013 21:22
Access request object inside of a form
#Form
class MyForm(forms.Form):
def __init__(self, *args, **kwargs):
self.request = kwargs.pop('request', None)
super(MyForm, self).__init__(*args, **kwargs)
def clean(self):
#... access the request object via self.request ...
@dstegelman
dstegelman / admin.py
Last active April 29, 2020 18:26
Adding Lookups to Django Generic Foreign Keys with Tabular Inlines
from itertools import chain
from django.utils.safestring import mark_safe
from django import forms
from django.contrib.contenttypes.models import ContentType
from django.forms import ModelForm
from django.contrib.admin.widgets import ForeignKeyRawIdWidget
from django.db.models.fields.related import ManyToOneRel
from django.contrib.admin.sites import site
@dstegelman
dstegelman / Py Web Dev Environment.md
Last active July 4, 2017 14:55
Python Web Development Environment

This short list outlines necessary packages for Python/Django development on a factory Mac. It is assumed that you would also follow most of theese instructions for a Vagrant setup as well. In that case you can commit installing homebrew, libjpeg, etc.

Once again this is my personal preference for development, so IDEs, VCS, etc will vary.

OS Level Packages and Software

  • XCode - Make sure to open XCode -> Preferences -> Downloads -> Install Command Line Tools (xcode-select --install)
  • Homebrew
  • Install wget via homebrew
  • Install git via homebrew
from tastypie.serializers import Serializer
import datetime
from django.utils import feedgenerator
class RSSSerializer(Serializer):
formats = ['json', 'jsonp', 'xml', 'yaml', 'html', 'plist', 'rss']
content_types = {
'json': 'application/json',
'jsonp': 'text/javascript',
[program:derek_stegelman_com]
command=/srv/www/virtualenvs/derek-stegelman-com/bin/python /srv/www/projects/derek-stegelman-com/manage.py run_gunicorn -c=/srv/www/projects/derek-stegelman-com/conf/gunicorn.conf.py --settings=settings.production
directory=/srv/www/projects/derek-stegelman-com
user=dstegelman
autostart=true
autorestart=true
redirect_stderr=True
@dstegelman
dstegelman / Vagrantfile.ru
Created June 27, 2014 19:08
Base Vagrant File
Vagrant.configure("2") do |config|
## Choose your base box
config.vm.box = "precise64"
config.vm.box_url = "http://files.vagrantup.com/precise64.box"
config.vm.synced_folder "", "/home/vagrant/test"
config.vm.network "forwarded_port", guest: 8080, host: 9090
config.ssh.forward_agent = true
@dstegelman
dstegelman / fixgit.sh
Created February 1, 2015 01:26
Check out all branche
#!/bin/bash
for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master `; do
git branch --track ${branch#remotes/origin/} $branch
done