Latest 100 public snipts »
TomL's
snipts
showing 1-6 of 6 snipts
-
∞ Re-check pass form
class PasswordCheckForm(forms.Form): password = forms.CharField(widget=forms.PasswordInput) def __init__(self, user, *args, **kwargs): self.user = user super(PasswordCheckForm, self).__init__(*args, **kwargs) def clean(self): password = self.cleaned_data.get('password') if self.user.check_password(password): raise forms.ValidationError(_('Password is incorrect.')) return self.cleaned_data def recheck_pass(request, template_name='registration/recheck_pass.html', redirect_field_name=REDIRECT_FIELD_NAME, authentication_form=PasswordCheckForm): redirect_to = request.REQUEST.get(redirect_field_name, '') if request.method == 'POST': form = authentication_form(request.user, request.POST) if form.is_valid(): if not redirect_to or ' ' in redirect_to: redirect_to = settings.LOGIN_REDIRECT_URL if '//' in redirect_to and re.match(r'[^\?]*//', redirect_to): redirect_to = settings.LOGIN_REDIRECT_URL else: form = authentication_form(request.user) return render_to_response(template_name, { 'form': form, }, RequestContext(request))
-
∞ Randomise selected elements
$(document).ready(function() { $("#choices").randomize("p"); }); // I think i'm in love with stackoverflow // Thanks to Russ Cam for this snippet to randomise selected elements // http://stackoverflow.com/questions/1533910/randomize-a-sequence-of-div-elements-with-jquery (function($) { $.fn.randomize = function(childElem) { return this.each(function() { var $this = $(this); var elems = $this.children(childElem); elems.sort(function() { return (Math.round(Math.random())-0.5); }); $this.remove(childElem); for(var i=0; i < elems.length; i++) $this.append(elems[i]); }); } })(jQuery);
-
∞ Get a list of links from html doc
def get_links(text): try: from BeautifulSoup import BeautifulSoup except ImportError: from beautifulsoup import BeautifulSoup soup = BeautifulSoup(text) link_list = [] for link in soup.findAll('a'): link_list.append(link['href']) return link_list
-
∞ Paste to paste2.org
""" Posts a file to paste2.org. Can post multiple files at once. paste2.py [options] [files] -h, --help : Print this help -l, --lang : Specify the language for the paste -p, --parent : Sprecify parent paste """ import urllib import urllib2 import sys import getopt DEFAULT_LANG = 'python' PASTE2_URL = 'http://paste2.org/new-paste' def post_to_paste2(code, lang=DEFAULT_LANG, parent=0): """ Post to paste2, return url of new paste """ url = PASTE2_URL values = { 'lang': lang, 'description': '', 'code': code, 'parent': 0, } data = urllib.urlencode(values) req = urllib2.Request(url, data) paste_url = urllib2.urlopen(req).geturl() return paste_url def usage(): print "paste2.py [options] [files]" print " -h, --help : Print this help" print " -l, --lang : Specify the language for the paste" print " -p, --parent : Sprecify parent paste" def main(): try: opts, args = getopt.gnu_getopt(sys.argv[1:], "hl:p:v", ["help", "lang=", "parent="]) except getopt.GetoptError, err: # print help information and exit: print str(err) # will print something like "option -a not recognized" usage() sys.exit(2) lang = DEFAULT_LANG parent = None for o, a in opts: if o in ("-l", "--lang"): language = a elif o in ("-p", "--parent"): parent = a elif o in ("-h", "--help"): usage() sys.exit() else: assert False, "unhandled option" #loop through all files passed for file_name in args: file = open(file_name, 'r') paste_url = post_to_paste2(file.read(), lang=lang, parent=parent) print "%s -> %s" % (file_name, paste_url) if __name__ == "__main__": main()
-
∞ Revoke all mysql priveleges
REVOKE ALL PRIVILEGES, GRANT OPTION ON *.* FROM jimmy@localhost; REVOKE ALL PRIVILEGES, GRANT OPTION ON *.* FROM jimmy@localhost.localdomain; FLUSH PRIVILEGES;
-
∞ Validate email address (based on example from django source)
import re if re.match(r"(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*"r'|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-011\013\014\016-\177])*"'r')@(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,6}\.?$', email_address, re.IGNORECASE): print "valid email" else: print "invalid email"


