Skip to content

Instantly share code, notes, and snippets.

@igniteflow
igniteflow / split_char.py
Created April 18, 2012 11:48
Format a long integer with a specified char rather than commas
split_char = ','
'.'.join('{:split_char}'.format(100000037683678456).split(','))
# or more succinctly
'{:,}'.format(1000000).replace(',', '.')
@igniteflow
igniteflow / improved-handle.py
Created May 18, 2012 10:18
Django management command with modified handle to dynamically call functions by name
class DynamicCommand(BaseCommand):
"""
When an arg is given after the management command this modified handle will automatically look for
and call a method with that name. Therefore, you can extend your management command Command() class
from this class instead of Command and simple write functions that will be called by
./manage.py your_file_name your_function_name
"""
def handle(self, *args, **kwargs):
argument = args[0]
@igniteflow
igniteflow / django-redirect-detailview.py
Created June 1, 2012 11:28
Django: Redirect DetailView on DoesNotExist
class MyDetailView(DetailView):
def get(self, request, **kwargs):
try:
self.model.objects.get(pk=kwargs['pk'])
return super(MyDetailView, self).get(request, **kwargs)
except self.model.DoesNotExist:
return redirect(reverse('your-target'))
@igniteflow
igniteflow / gist:2882341
Created June 6, 2012 14:50
Django: Specify user post-submission for ModelForm using CreateView
"""
Enforce a model's User FK post-submission
From https://github.com/AndrewIngram
"""
class FeedbackForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user')
super(FeedbackForm, self).__init__(*args, **kwargs)
@igniteflow
igniteflow / gist:2889573
Created June 7, 2012 15:47
Displaying HTML in Django admin
from django.utils.safestring import SafeUnicode
"""
When overriding Django admin templates |safe and autoescape off don't work, so do this instead...
"""
# for a foreign key field in the change form, if you want to override the unicode method, use a proxy
class UserProxy(User):
"""
Using a proxy to present the required formatting: username, email, full name
@igniteflow
igniteflow / gist:2987776
Created June 25, 2012 10:13
Fix for installing Citrix on Ubuntu 11.10
# taken from http://forums.citrix.com/thread.jspa?threadID=306353&tstart=0
It seems to be an error in the post install script as it's only looking for "i[0-9]86" as the machine architecture.
Using "uname -m", my machine (64-bit Ubuntu 12.04) returns x86_64 and this causes the script to fail since it doesn't seem to recognise this string.
Here's what I did to get it to install:
1. Install the .deb and let it fail
2. Edit /var/lib/dpkg/info/icaclient.postinst
3. Replace the line that says echo $Arch|grep "i[0-9]86" >/dev/null with *echo $Arch|grep -E "i[0-9]86|x86_64" >/dev/null (note the -E after grep!)
@igniteflow
igniteflow / gist:2988428
Created June 25, 2012 12:56
Customising Python with a start up file
1. Create your start up file, for instance ~/.pythonrc. Example contents (from http://valueerror.wordpress.com/2009/11/03/python-shell-history-autocompletion-and-rc-file/):
import atexit
import os
import re
import readline
import rlcompleter
import socket
import _socket
import sys
@igniteflow
igniteflow / gist:2995970
Created June 26, 2012 14:05
Logging in Django 1.3
# add this to your settings
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'standard': {
'format' : "[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s",
'datefmt' : "%d/%b/%Y %H:%M:%S"
},
},
@igniteflow
igniteflow / gist:3032552
Created July 2, 2012 10:34
Gell all PIDs of an open file
lsof | grep "your_file.log" | awk '{printf "%s ",$2}'
@igniteflow
igniteflow / export.py
Created July 9, 2012 17:03
Django management command to export a model's objects as a CSV file
import csv
from tools import DynamicCommand # see https://gist.github.com/2724472
class Command(DynamicCommand):
"""
Easily export a model's objects in csv format. In this example the csv can be generated by executing:
./manage.py export your_model