Skip to content

Instantly share code, notes, and snippets.

View juanmhidalgo's full-sized avatar

Juan M. Hidalgo juanmhidalgo

  • Mar del Plata, Buenos Aires, Argentina
View GitHub Profile
@juanmhidalgo
juanmhidalgo / pas.py
Created March 9, 2016 14:15
Password Generator
import os
import string
def random_password(length=10):
chars = string.ascii_uppercase + string.digits + string.ascii_lowercase
password = ''
for i in range(length):
password += chars[ord(os.urandom(1)) % len(chars)]
return password
@juanmhidalgo
juanmhidalgo / utils.py
Created March 2, 2016 16:41
[django] Model to dict
from django.db.models.fields.related import ManyToManyField
def to_dict(instance):
opts = instance._meta
data = {}
for f in opts.concrete_fields + opts.many_to_many:
if isinstance(f, ManyToManyField):
if instance.pk is None:
data[f.name] = []
else:
@juanmhidalgo
juanmhidalgo / views.py
Last active February 11, 2016 14:39
[django] ClassBasedView with csrf_exempt
from django.views.generic.base import View
from django.views.decorators.csrf import csrf_exempt
from django.utils.decorators import method_decorator
class ExampleView(View):
@method_decorator(csrf_exempt)
def dispatch(self, request, *args, **kwargs):
return super(ExampleView, self).dispatch(request, *args, **kwargs)
@juanmhidalgo
juanmhidalgo / urls.py
Last active February 11, 2016 14:39
[django] Serve static files
from django.conf.urls.static import static
from django.conf import settings
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
@juanmhidalgo
juanmhidalgo / gist:8708918
Last active February 11, 2016 13:57 — forked from robhudson/gist:3848832
Django Cors
class CORSResource(object):
"""
Adds CORS headers to resources that subclass this.
"""
def create_response(self, *args, **kwargs):
response = super(CORSResource, self).create_response(*args, **kwargs)
response['Access-Control-Allow-Origin'] = '*'
response['Access-Control-Allow-Headers'] = 'Content-Type'
return response
//
// Regular Expression for URL validation
//
// Author: Diego Perini
// Updated: 2010/12/05
// License: MIT
//
// Copyright (c) 2010-2013 Diego Perini (http://www.iport.it)
//
// Permission is hereby granted, free of charge, to any person
@juanmhidalgo
juanmhidalgo / gist:3190024
Created July 27, 2012 19:27 — forked from remy/gist:350433
Storage polyfill
if (typeof window.localStorage == 'undefined' || typeof window.sessionStorage == 'undefined') (function () {
var Storage = function (type) {
function createCookie(name, value, days) {
var date, expires;
if (days) {
date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
expires = "; expires="+date.toGMTString();

tmux shortcuts & cheatsheet

start new:

tmux

start new with session name:

tmux new -s myname
class FilterMixin(object):
"""
View mixin which provides filtering for ListView.
"""
filter_url_kwarg = 'filter'
default_filter_param = None
def get_default_filter_param(self):
if self.default_filter_param is None:
raise ImproperlyConfigured(