Skip to content

Instantly share code, notes, and snippets.

@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"
)
@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 / chromecast.sh
Created March 27, 2014 00:48
Using Google Chromecast from Fedora 20
cat /proc/sys/net/ipv4/ip_local_port_range
firewall-cmd --permanent --add-port=32768-61000/udp
firewall-cmd --reload
firewall-cmd --permanent --zone=home --add-port=32768-61000/udp
iptables -A INPUT -s 192.168.0.0/24 -p udp -m udp --dport 32768:61000 -j ACCEPT
@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 / 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
{
"bold_folder_labels": true,
"close_windows_when_empty": false,
"color_scheme": "Packages/Color Scheme - Default/Blackboard.tmTheme",
"fade_fold_buttons": false,
"file_exclude_patterns":
[
"*.xlsx",
"*.pyc",
"*.directory"
@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 / 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 / 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 / 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)