Skip to content

Instantly share code, notes, and snippets.

View dustinfarris's full-sized avatar
💭
🇺🇸

Dustin Farris dustinfarris

💭
🇺🇸
View GitHub Profile
@dustinfarris
dustinfarris / session_enabled_test_case.py
Last active April 18, 2024 22:17
Setting session variables whilst testing with Django's TestCase
"""
Attempting to set session variables directly from TestCases can
be error prone. Use this super-class to enable session modifications
from within your tests.
Usage
-----
class MyTest(SessionEnabledTestCase):
@dustinfarris
dustinfarris / pre-populating_children.coffee
Last active December 19, 2015 20:49
Pre-populate children and grandchildren when a parent is created. The app has a set of bridge—the card game—Exercises, each Exercise consisting of 8 Hands, each Hand having an Answer for each Row within the Exercise's associated Situation.
App.ExercisesNewController = App.ObjectController.extend
create: ->
situation_id = $('#id_situation').val()
situation = App.Situation.find(situation_id)
@content.set 'situation', situation
rows = situation.get('rows')
@content.save().then =>
for i in [1..8]
hand = App.Hand.createRecord
@dustinfarris
dustinfarris / Sharing Django Users and Sessions Across Projects
Created July 26, 2013 00:38
This is a dated document and may be improved on.
# Sharing Django Users and Sessions Across Projects
By Dustin Farris on 22 Feb 2012
This document describes how to share users created using Django's auth system with other
Django projects. It is not a hack; it simply makes use of the database router and
middleware system that Django comes with out of the box.
## Introduction
@dustinfarris
dustinfarris / django_cms_nav.html
Last active December 20, 2015 07:39
Traverse a tree in Django CMS using familiar "Next/Previous" buttons. (not sure if this will work in 3.0 when it is released) This works up to 3 levels. For more levels, append more "next" logic to the bottom. e.g. current_page.parent.parent.parent...
{% load cms_tags %}
<ul class="pager">
{% if current_page.get_previous_sibling %}
<li><a href="{% page_url current_page.get_previous_sibling %}" role="prev">&larr; Previous</a></li>
{% else %}
<li><a href="{% page_url current_page.parent %}" role="prev">&larr; Previous</a></li>
{% endif %}
{% if current_page.children.exists %}
<li><a href="{% page_url current_page.children.all|first %}" role="next">Next &rarr;</a></li>
@dustinfarris
dustinfarris / gist:6566739
Created September 14, 2013 23:47
dealing with reverse generic relations
import ast
from django.contrib.contenttypes.models import ContentType
from rest_framework import serializers
from addresses.serializers import AddressSerializer
from users import models
class AddressesField(serializers.RelatedField):
@dustinfarris
dustinfarris / .gvimrc
Last active December 23, 2015 14:19
VI settings
scriptencoding utf-8
" Powerline setup
set guifont=Droid\ Sans\ Mono\ for\ Powerline:h15
set laststatus=2
" MVim options
set guioptions-=T " Removes top toolbar
set guioptions-=r " Removes right hand scroll bar
set go-=L " Removes left hand scroll bar
color codeschool
set guifont=Monaco:h12
let g:NERDTreeWinPos = "right"
set guioptions-=T " Removes top toolbar
set guioptions-=r " Removes right hand scroll bar
set go-=L " Removes left hand scroll bar
autocmd User Rails let b:surround_{char2nr('-')} = "<% \r %>" " displays <% %> correctly
:set cpoptions+=$ " puts a $ marker for the end of words/lines in cw/c$ commands
let g:syntastic_python_checkers=['pylint']
# Put user (homebrew) installations ahead of system installations
export PATH="/usr/local/bin:$PATH"
# Use local node binaries before global binaries
export PATH="./node_modules/.bin:$PATH"
# Use colors for terminal output
export CLICOLOR=1
# Initialize virtualenvwrapper
# Add RVM to PATH for scripting
PATH=$PATH:$HOME/.rvm/bin
alias run='./manage.py runserver 0.0.0.0:8000'
alias mkenv='mkvirtualenv `basename $PWD` && setvirtualenvproject && add2virtualenv app'
alias pycclean='find . -name "*.pyc" -exec rm {} \;'
alias dsh='./manage.py shell'
alias sm='./manage.py schemamigration'
@dustinfarris
dustinfarris / email_username.py
Last active December 28, 2015 08:29
Django + DRF, Using an email address as a username
class User(AbstractUser):
pass
class UserSerializer(serializers.ModelSerializer):
username = serializers.EmailField()
class Meta:
model = User