Skip to content

Instantly share code, notes, and snippets.

@ckinsey
ckinsey / gist:3452253
Created August 24, 2012 15:57
custom admin view
class FooAdmin(admin.ModelAdmin):
def get_urls(self):
urls = super(ContentProjectAdmin, self).get_urls()
extra_urls = patterns('',
url(r'^dashboard/$', self.admin_site.admin_view(self.dashboard), name="cms_dashboard"),
)
return extra_urls + urls
@ckinsey
ckinsey / middleware.py
Created September 21, 2012 03:24
Mobile middleware success!
from django.conf import settings
class MobileTemplateMiddleware:
def process_request(self, request):
host = request.META.get('HTTP_HOST', '')
host_s = host.split('.')
if len(host_s) > 1:
subdomain = host_s[0]
@ckinsey
ckinsey / gist:4136999
Created November 23, 2012 19:40
Formset magic
class DashboardManageUsersFormset(BaseFormSet):
def __init__(self, *args, **kwargs):
self.assignment_perms = kwargs.pop('assignment_perms', None)
super(DashboardManageUsersFormset, self).__init__(*args, **kwargs)
def _construct_forms(self):
self.forms = []
for i in xrange(self.total_form_count()):
self.forms.append(self._construct_form(i, assignment_perms=self.assignment_perms))
@property
def request(self):
"""
just for convenience
"""
return self
@ckinsey
ckinsey / gist:7250117
Created October 31, 2013 13:51
Python flow control as determined by the text of a button set via JavaScript.
if self.request.POST.get('action').lower().find('book') > -1:
# if everything on the other side was all set to go and the user making changes,
# the button they are pushing contains "book" on it and we can redirect to booking this session.
return HttpResponseRedirect(reverse('setup_payment_for_session', kwargs={'pk': obj.pk}))
STATUS_CANCELLED = 'cancelled'
STATUS_NEW = 'new'
STATUS_PENDING = 'pending'
STATUS_PAYMENT_DUE = 'payment-due'
STATUS_PAYMENT_PENDING = 'payment-pending'
STATUS_PAID = 'paid'
NEGOTIABLE_STATUS_CHOICES = (
(STATUS_CANCELLED, 'Cancelled'), # Cancelled, duh
(STATUS_NEW, 'New'), # Default state of request
@ckinsey
ckinsey / decorators.py
Created March 22, 2014 16:34
Require SSL for Django views via a decorator. You'll need some setup in your web server to support the request.is_secure() method--see the Django docs.
from functools import wraps
from django.conf import settings
from django.http import HttpResponseRedirect
def require_ssl(view):
"""
Decorator that requires an SSL connection. If the current connection is not SSL, we redirect to the SSL version of
the page.
@ckinsey
ckinsey / etl_regstry.py
Created October 13, 2015 02:49
An example implementation of an ETL registry
class ETLRegistry(object):
"""
The ETL registry stores the relationships between Extract, Transform
and Load classes.
An entry tracks both a string representation (for passing through message queue) and
a reference for the class (for invocation inside the task)
"""
_regsitry = []
@ckinsey
ckinsey / bot.py
Created September 6, 2016 14:08
Bot.py Basic
import markovify
import time
from slackclient import SlackClient
BOT_TOKEN = "insert bot token here"
GROUP_TOKEN = "insert slack group token here"
@ckinsey
ckinsey / chain.py
Last active August 19, 2018 04:05
Chain.py Builds the Markov Models from Slack
import json
import markovify
import re
import time
from slackclient import SlackClient
BOT_TOKEN = "insert bot token here"