This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| split_char = ',' | |
| '.'.join('{:split_char}'.format(100000037683678456).split(',')) | |
| # or more succinctly | |
| '{:,}'.format(1000000).replace(',', '.') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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')) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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!) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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" | |
| }, | |
| }, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| lsof | grep "your_file.log" | awk '{printf "%s ",$2}' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |