Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This I found years ago, floating around django snippets.
# If anyone knows the proper attribution, please email me
# at steven@agoodclooud.com, and I'll update t!
#
import re
from types import UnicodeType
@victorono
victorono / urls.py
Created February 10, 2014 19:24
Robots in django
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from django.views.generic import TemplateView
urlpatterns += patterns('',
url(r'^robots\.txt$', TemplateView.as_view(template_name='robots.txt', content_type='text/plain')),
)
@victorono
victorono / DoesNotExistTo404.py
Created February 12, 2014 13:33
A Middleware that can be applied to your views to turn ObjectDoesNotExist exceptions into Http404 exceptions. This means people will see a "Page not found" error rather than an "Internal Server Error" when they are request something that does not exist in the database.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from django.core.exceptions import ObjectDoesNotExist
from django.http import Http404
class DoesNotExistTo404Middleware(object):
def process_exception(self, request, exception):
if isinstance(exception,ObjectDoesNotExist):
raise Http404(exception.message)
@victorono
victorono / count_share.py
Created February 14, 2014 16:02
Count share url in twitter and facebook
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
url = "http://urls.api.twitter.com/1/urls/count.json"
params = dict(
url='http://www.google.com'
)
@victorono
victorono / calculate_age.py
Created March 1, 2014 23:43
Age from birthdate in python
from datetime import date
def calculate_age(born):
today = date.today()
try:
birthday = born.replace(year=today.year)
except ValueError: # raised when birth date is February 29 and the current year is not a leap year
birthday = born.replace(year=today.year, day=born.day-1)
if birthday > today:
return today.year - born.year - 1
@victorono
victorono / format_rut.py
Created March 3, 2014 03:44
Formats the RUT form to the common string representation.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from django import template
register = template.Library()
from django.utils.encoding import smart_unicode
def _verifier(rut):
rut = smart_unicode(rut).replace(' ', '').replace('.', '').replace('-', '')
@victorono
victorono / gist:9679158
Created March 21, 2014 03:48
psycopg2 in Mavericks 10.9.2
# Download Command Line Tools (OS X Mavericks) for Xcode
# https://developer.apple.com/downloads/index.action
# You can setting the following environment variables prior compilation (.bash_profile or .bashrc):
export CFLAGS=-Qunused-arguments
export CPPFLAGS=-Qunused-arguments
pip install psycopg2
@victorono
victorono / round.py
Created March 26, 2014 21:23
Function that can round like this: 10 -> 10, 12 -> 10, 13 -> 15, 14 -> 15, 16 -> 15, 18 -> 20
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from django import template
register = template.Library()
@register.filter(name='round_step')
def round_step(x, base=5):
return int(base * round(float(x)/base))
@victorono
victorono / ssh.sh
Last active August 29, 2015 13:58
SSH without password between servers
ssh-keygen -b 4096 -t rsa
ssh-copy-id user@server
@victorono
victorono / forms.py
Created April 9, 2014 21:24
File Mimetype Validator
from validators import MimetypeValidator
class MyForm(forms.Form):
file = forms.FileField(
allow_empty_file=False,
validators=[MimetypeValidator('application/pdf')],
help_text="Upload a PDF file"
)