Skip to content

Instantly share code, notes, and snippets.

@treyhunner
treyhunner / models.py
Created December 10, 2010 06:16
Encrypt and decrypt Django model primary key values (useful for publicly viewable unique identifiers)
# This code is under the MIT license.
# Inspired by this StackOverflow question:
http://stackoverflow.com/questions/3295405/creating-django-objects-with-a-random-primary-key
import struct
from Crypto.Cipher import DES
from django.db import models
def base36encode(number):
@dokterbob
dokterbob / validators.py
Created August 31, 2011 15:03
Validator for files, checking the size, extension and mimetype.
from os.path import splitext
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _
from django.template.defaultfilters import filesizeformat
class FileValidator(object):
"""
Validator for files, checking the size, extension and mimetype.
@elidickinson
elidickinson / admin.py
Created November 20, 2011 01:16
Using CKEditor with Flatpages
from django.contrib import admin
from django.contrib.flatpages.models import FlatPage
# Note: we are renaming the original Admin and Form as we import them!
from django.contrib.flatpages.admin import FlatPageAdmin as FlatPageAdminOld
from django.contrib.flatpages.admin import FlatpageForm as FlatpageFormOld
from django import forms
from ckeditor.widgets import CKEditorWidget
@girasquid
girasquid / gist:1453265
Created December 9, 2011 20:59
How to save a file to a FileField in Django without writing the entire row.
instance.file_field.save(the_filename, file_content, save=False)
MyModel.objects.filter(id=instance.id).update(file_field=instance.file_field)
@adam-p
adam-p / harvestbot.py
Created December 15, 2011 06:22
A tool to automatically fill in Harvest timesheets. For when you're just expected to put 8s in a single task across all five days anyway. Note that it doesn't really do the job, since Harvest doesn't let you submit your entries for approval via the API.
#!python
# Note that this whole effort is fundamentally flawed. The Harvest API does not
# provide the ability to submit the timesheet for approval. So, even though this
# code can fill in your timesheet, you still have to go to the web page to submit
# it. Which makes this a waste of time.
# See the feature request thread here:
# http://forum.getharvest.com/forums/api-and-developer-chat/topics/is-there-a-timesheet-api
# HARVEST_URL, PROJECT_NAME, and TASK_NAME need to be set to the desired values.
@grapo
grapo / gist:2319638
Created April 6, 2012 13:09
Customizable serialization

Introduction:

If we want to serialize objects we must consider two questions:

  1. What to serialize? Which fields of objects, how deep to serialize related objects.
  2. How output should looks like? Which format (xml, json, yaml), fields renaming, some format specific options (like attributes in xml - nothing similar in json), order of fields, some fields in other place in structure tree than others.

This questions lead as to two phases of serialization:

  1. First phase "dehydration": Change class instances (generally Python class, particular Django Model class) to dictionary contains data(python native types) of all interesting us fields. In this stage we are not interested in specific format.
@Nitron
Nitron / hosts
Created July 16, 2012 23:26
Django Subdomain Middleware with CNAME support
[...]
# Fake DNS entries for testing the subdomain middleware
127.0.0.1 test1.django.local
127.0.0.1 test2.django.local
127.0.0.1 djangotest.christopher-williams.net
127.0.0.1 django.local
@perrygeo
perrygeo / django_model_graph.sh
Created April 13, 2013 21:44
Generate UML diagram of django app models
apt-get install python-pygraphviz
pip install django-extensions
# add 'django_extensions' to INSTALLED_APPS in settings.py
python manage.py graph_models trees -o test.png
def not_in_student_group(user):
"""Use with a ``user_passes_test`` decorator to restrict access to
authenticated users who are not in the "Student" group."""
return user.is_authenticated() and not user.groups.filter(name='Student').exists()
# Use the above with:
@user_passes_test(not_in_student_group, login_url='/elsewhere/')
def some_view(request):
# ...
@phybros
phybros / update-route53.sh
Last active February 12, 2024 00:07
BASH Script to keep Route53 updated with your current external IP address
#!/bin/bash
# (optional) You might need to set your PATH variable at the top here
# depending on how you run this script
#PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
# Hosted Zone ID e.g. BJBK35SKMM9OE
ZONEID="enter zone id here"
# The CNAME you want to update e.g. hello.example.com