Skip to content

Instantly share code, notes, and snippets.

View mjtamlyn's full-sized avatar

Marc Tamlyn mjtamlyn

  • Photocrowd
  • Oxford
View GitHub Profile
@mjtamlyn
mjtamlyn / base.py
Created March 1, 2012 18:17
Alternative dispatch
# Current dispatch method
def dispatch(self, request, *args, **kwargs):
# Try to dispatch to the right method; if a method doesn't exist,
# defer to the error handler. Also defer to the error handler if the
# request method isn't on the approved list.
if request.method.lower() in self.http_method_names:
handler = getattr(self, request.method.lower(), self.http_method_not_allowed)
else:
handler = self.http_method_not_allowed
self.request = request
@mjtamlyn
mjtamlyn / index.html
Created May 25, 2012 11:19
Testing storage options
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
function setCookie(c_name,value,exdays) {
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}
marc ~ $ ls /usr/share/postgresql/timezonesets/
Africa.txt Asia.txt Australia.txt Europe.txt Pacific.txt
America.txt Atlantic.txt Default India
Antarctica.txt Australia Etc.txt Indian.txt
@mjtamlyn
mjtamlyn / gist:2897920
Created June 8, 2012 20:10
App-loading in django

Application loading refactor

At present application loading is handled in different ways through Django. Because of some of the limitations of Python, and some peculiarities of how models work, modules should not be reimported. Consequently Django maintains a global static, called the application cache. Models are imported from the application cache transparently when interacting with the database. But applications listed in INSTALLED_APPS contain more than just models. Some contain template tags, admin.py files, search_indexes.py files from

(docs)marc docs $ rm -rf _build/html/
(docs)marc docs $ make html
sphinx-build -b djangohtml -d _build/doctrees . _build/html
Making output directory...
Running Sphinx v1.1.3
loading pickled environment... done
building [djangohtml]: targets for 243 source files that are out of date
updating environment: 0 added, 1 changed, 0 removed
reading sources... [100%] ref/class-based-views/generic-date-based
looking for now-outdated files... none found
@mjtamlyn
mjtamlyn / multi_contact_form.py
Created June 20, 2012 10:57
Multi-contact form (FeinCMS content type)
from django.db import models
from django.utils.text import ugettext_lazy as _
from .contact_form_amended import ContactForm, ContactFormContent
class MultiContactFormContent(ContactFormContent):
FORM_CLASSES = {'simple-contact': ContactForm}
FORM_CHOICES = (('simple-contact', _('Simple contact form')),)
from django.db import models
class Entry(models.Model):
RATING_CHOICES = zip(range(1, 11), range(1, 11))
user = models.ForeignKey('auth.User')
date = models.DateField()
medication_taken = models.BooleanField()
rating_1 = models.IntegerField(choices=RATING_CHOICES)
rating_2 = models.IntegerField(choices=RATING_CHOICES)
@mjtamlyn
mjtamlyn / auth.py
Created September 7, 2012 14:53
Basic rest_framework app key & access token based auth (stripped down)
class AccessTokenAuthentication(BaseAuthentication):
def authenticate(self, request):
auth_header = request.META.get('HTTP_AUTHORIZATION')
if not auth_header:
return None
token = re.match(r'token;([a-f0-9]{32})', auth_header, re.I)
if not token:
return None
token = token.groups()[0].lower()
try:
@mjtamlyn
mjtamlyn / views.py
Created September 7, 2012 15:17
Authenticate view for rest framework
class GetAccessToken(PermissionsMixin, View):
authentication = [APIKeyAuthentication]
form = UserForm
def post(self, request, *args, **kwargs):
user = authenticate(username=self.CONTENT['username'], password=self.CONTENT['password'])
if not user:
raise ErrorResponse(status.HTTP_400_BAD_REQUEST, {'detail': 'invalid username or password'})
access, created = Access.objects.get_or_create(application=self.user, user=user)
response_data = {
# Extra imports
from rest_framework.authtoken.models import Token
from rest_framework.response import Response
from rest_framework import status
class Login(ObtainAuthToken):
serializer_class = MyAuthTokenSerializerWithExtraChecks
def post(self, request):
"""Override the post method to use custom serializer class."""