This file contains 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 MyModelAdminForm(forms.ModelForm): | |
''' | |
To add the option 'Save as New', by default does not work for reverse | |
ManyToMany/ForeignKey fields. In other words, does not copy over the | |
reverse attributes. A simple workaround to make this happen is: | |
update the cleaned_data and returning it in the context. | |
Something like this: | |
''' | |
def clean(self, *args, **kwargs): | |
cd = self.cleaned_data |
This file contains 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
#models.py | |
CAPRICORN = 'Capricorn' | |
AQUARIUS = 'Aquarius' | |
PISCES = 'Pisces' | |
ARIES = 'Aries' | |
TAURUS = 'Taurus' | |
GEMINI = 'Gemini' | |
CANCER = 'Cancer' | |
LEO = 'Leo' |
This file contains 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
# -*- coding: utf-8 -*- | |
import datetime | |
from south.db import db | |
from south.v2 import DataMigration | |
from django.db import models | |
class Migration(DataMigration): | |
def forwards(self, orm): | |
db.start_transaction() |
This file contains 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
''' | |
This is an example of functools.partials | |
Typical usage is when you have a function that needs multiple parameters, | |
of which one is a constant, you can use a partial. | |
Example: | |
Return only the values in an iterable which match the regex 'c.t' where . is a single character. | |
Input: | |
['cat', 'cut', 'abc', 'def'] |
This file contains 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
def secondsToStr(t): | |
''' | |
This snippet converts the time elapsed in | |
seconds to HH:MM:SS.mmm | |
Example: | |
>>> secondsToStr(123456) | |
'34:17:36.000' | |
''' |
This file contains 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.views.debug import technical_500_response | |
from django.views.defaults import server_error | |
def server_error_handler(request): | |
ip_address = request.META.get("HTTP_X_FORWARDED_FOR", request.META.get("REMOTE_ADDR")) | |
if ip_address in settings.ALLOWED_IPS_FOR_DEBUG_SCREEN: #or request.user.is_staff | |
return technical_500_response(request, *sys.exc_info()) | |
return server_error(request) |
This file contains 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
;(function () { | |
// console.log statements cause issues in Internet Explorer <= 9. | |
// This piece of code prevents the application to fail | |
// Source for console: https://developer.mozilla.org/en-US/docs/Web/API/Console | |
var fns = [ | |
"assert", | |
"count", | |
"debug", | |
"dir", | |
"dirxml", |
This file contains 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 fractions | |
def smallest_multiple( n): | |
return reduce(lambda x, y: x*y/fractions.gcd(x, y), range(2, n+1)) |
This file contains 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 itertools | |
def yieldSubset(s): | |
return sorted(list(set([''.join(c) for i in range(len(s)+1) for c in itertools.combinations(s, i) if c]))) |
This file contains 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
(function($) { | |
$.fn.regexFinder = function(text) { | |
/* | |
Phone number formats | |
XXX-XXX-XXXX | |
XXX.XXX.XXXX | |
XXX XXX XXXX | |
*/ | |
var cc_re = /(?:4[0-9]{12}(?:[0-9]{3})?|[25][1-7][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})/g; | |
var date_re = /\d{2}\/\d{4}/g; |
OlderNewer