Skip to content

Instantly share code, notes, and snippets.

@aaugustin
aaugustin / djpatch.py
Created August 25, 2011 10:30 — forked from jezdez/djpatch.py
A helper script to apply patches from Django's trac
#!/usr/bin/env python
"""
Obviously this is only useful if you have to deal with Django's
Trac a lot.
Mostly stolen from Jacob Kaplan-Moss, but improved by Jannis Leidel
and Aymeric Augustin.
Reads a config file at ~/.djpatchrc, e.g.:
@aaugustin
aaugustin / admin.py
Last active August 7, 2022 19:39
Read-only ModelAdmin for Django
from django.contrib import admin
class ReadOnlyModelAdmin(admin.ModelAdmin):
"""
ModelAdmin class that prevents modifications through the admin.
The changelist and the detail view work, but a 403 is returned
if one actually tries to edit an object.
@aaugustin
aaugustin / i18n.py
Created December 13, 2011 15:05
Cache Django's javascript_catalog
import datetime
import hashlib
from django.views.decorators.cache import cache_page
from django.views.decorators.http import condition
from django.views.i18n import javascript_catalog
def get_version():
# Write your own! That depends on your deployment strategy.
# This example won't work if you release more than once a day.
@aaugustin
aaugustin / gist:2351479
Created April 10, 2012 13:44
Django: log all database queries in the console
import logging
logger = logging.getLogger('django.db.backends')
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.StreamHandler())
@aaugustin
aaugustin / convert_to_utc.py
Created June 22, 2012 08:52
Convert datetimes in the database when switching USE_TZ from False to True.
"""Convert datetimes in the database when switching USE_TZ from False to True.
tl;dr RUNNING THIS SCRIPT CAN RESULT IN DATA CORRUPTION, DATA LOSS AND EVEN
SERVER CRASHES. USE IT AT YOUR OWN RISK. NO WARRANTY WHATSOEVER.
This is a management command. Put it in the management.commands package of
one of your applications, then run: ./manage.py convert_to_utc <app_name> ...
This script assumes that no write operations take place while it's running.
It will rewrite every single record in your database; that's its whole point.
@aaugustin
aaugustin / gist:3012318
Created June 28, 2012 16:29
Quick'n'dirty faster test runner for Django (not tested)
from optparse import make_option
from django.test.simple import DjangoTestSuiteRunner
def fake_create_test_db(self, verbosity=1, autoclobber=False):
"""Simplified version of BaseDatabaseCreation.create_test_db."""
test_database_name = self._get_test_db_name()
if verbosity >= 1:
@aaugustin
aaugustin / gist:3164559
Created July 23, 2012 16:25
Migrate from CMS_REDIRECTS=True to django.contrib.redirects
from cms.models import Title
from django.contrib.redirects.models import Redirect
titles_with_redirect = Title.objects.exclude(redirect__isnull=True).exclude(redirect=u'')
for title in titles_with_redirect.select_related('page__site'):
Redirect.objects.create(
site=title.page.site,
old_path=u'/%s/' % title.path,
new_path=title.redirect,
@aaugustin
aaugustin / build_svn_to_git_mapping.py
Created July 26, 2012 08:52
Script used to create the svn_to_git.py file for code.djangoproject.com
"""Build a mapping between svn commits and git changesets.
Usage: python build_svn_to_git_mapping.py > svn_to_git.py
This script should be run in a clone of the git repository,
with a checkout of https://code.djangoproject.com/svn/django in ../django-svn.
"""
import os
import pprint
@aaugustin
aaugustin / HOWTO.md
Last active December 3, 2019 20:18
Connecting a Django application to a Microsoft SQL Server database from Debian GNU/Linux
  1. Install and register the FreeTDS driver for unixODBC.

     apt-get install tdsodbc
     odbcinst -i -d -f /usr/share/tdsodbc/odbcinst.ini
    
  2. (Optional) Test the DSN-less connection with pyodbc.

     apt-get install python-pyodbc
    

>>> import pyodbc

@aaugustin
aaugustin / kerberos.md
Last active April 10, 2023 01:06
Kerberos

Kerberos setup

This guide explains how to set up Kerberos authentication for:

  • SSH access to a server,
  • HTTP access to a service.

It assumes you're running Active Directory and Debian servers.