Skip to content

Instantly share code, notes, and snippets.

View artschwagerb's full-sized avatar

Brian Artschwager artschwagerb

View GitHub Profile
@artschwagerb
artschwagerb / routers.py
Created November 14, 2013 18:38
Django Database Router
import random
class MasterSlaveRouter(object):
def db_for_read(self, model, **hints):
"""
Reads go to a randomly-chosen slave.
"""
return random.choice(['master','slave1', 'slave2'])
def db_for_write(self, model, **hints):
@artschwagerb
artschwagerb / settings.py
Created November 14, 2013 18:43
Database Routers (settings.py)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'anotherdatabase',
'USER': 'anotherusername',
'PASSWORD': 'anotherpassword',
'HOST': '192.168.1.1',
'PORT': '',
},
'master': {
@artschwagerb
artschwagerb / routers.py
Created November 14, 2013 19:20
Django Database Routers Master-Slave
import random
class MasterSlaveRouter(object):
def db_for_read(self, model, **hints):
"""
Reads go to a randomly-chosen slave.
"""
return random.choice(['master','slave1', 'slave2'])
def db_for_write(self, model, **hints):
@artschwagerb
artschwagerb / mailbox.py
Last active December 29, 2015 02:49 — forked from cgoldberg/mailbox.py
#!/usr/bin/env python
"""MailBox class for processing IMAP email.
(To use with Gmail: enable IMAP access in your Google account settings)
usage with GMail:
import mailbox
@artschwagerb
artschwagerb / models.py
Last active December 29, 2015 20:39
Django Signals
from django.db import models
from django.contrib.auth.models import User
class Person(models.Model):
employee_id = models.IntegerField(unique=True,editable=False)
username = models.CharField(max_length=50,unique=True,editable=False)
state_id = models.IntegerField(unique=True,null=True,blank=True,editable=False)
email = models.EmailField(max_length=50,unique=True,editable=False)
firstname = models.CharField(max_length=50,editable=False)
lastname = models.CharField(max_length=50,editable=False)
@artschwagerb
artschwagerb / models.py
Created December 12, 2013 15:40
Django __unicode__ method
def __unicode__(self):
if self.name:
return u'%s' % (self.name)
elif self.assettag:
return u'%s' % (self.assettag)
elif self.mac_wired:
return u'%s' % (self.mac_wired)
elif self.model.chassis:
return u'%s' % (self.model.chassis)
else:
@artschwagerb
artschwagerb / forms.py
Last active August 12, 2021 14:25
Django Forms Example
from django import forms
from inventory.models import Computer
class ComputerForm(forms.ModelForm):
class Meta:
model = Computer
class ChromebookForm(forms.ModelForm):
@artschwagerb
artschwagerb / gist:11123810
Created April 20, 2014 20:03
Django URLs
from django.conf.urls import patterns, include, url
from chordcharts.views import ChordListView #for class based views
from chordcharts import views #for function based views
urlpatterns = patterns('',
url(r'^$', ChordListView.as_view(), name='chord_list'), #for class based views
url(r'^$', 'chordcharts.views.chord_list', name='chord_list'), #for function based views
url(r'^chordcharts/', include('chordcharts.urls')),
)
global
log 127.0.0.1 local0 notice
maxconn 20000
user haproxy
group haproxy
defaults
log global
mode tcp
option dontlognull
@artschwagerb
artschwagerb / gist:a310a23e215a11ce7ad1
Created June 11, 2014 15:06
Django - for loop range
{% for i in '123'|make_list %}
{% if object.property == i|add:"0" %}
<li>(Selected) Property {{ i }}</li>
{% else %}
<li>Property {{ i }}<li>
{% endif %}
{% endfor %}