Skip to content

Instantly share code, notes, and snippets.

@pricco
pricco / admin.py
Last active May 18, 2021 04:42
Django Admin: Request for list_display
# any_app/admin.py
from django.contrib import admin
from core.admin_mixins import ModelAdminMixin
@admin.register(AnyModel)
class AnyModelAdmin(ModelAdminMixin, admin.ModelAdminMixin):
list_display = ['id', 'especial_display_with_request']
@pricco
pricco / merge_calendars.gs
Last active December 19, 2017 17:00
Merge multiple google calendars (copy events from many sources into one calendar)
// Reference: https://developers.google.com/apps-script/reference/calendar/calendar-app
function sync(sources, target) {
var start = new Date((new Date()).getTime() - 10 * 3600000 * 24);
var end = new Date((new Date()).getTime() + 120 * 3600000 * 24);
var targetCalendar = CalendarApp.getCalendarById(target);
if (!targetCalendar) {
return;
}
var targetEvents = targetCalendar.getEvents(start, end);
@pricco
pricco / myproject-settings-test.py
Created September 2, 2015 17:36
Avoid all migrations for test in django.
class IgnoreAllMigrations(dict):
def __getitem__(self, key):
return 'django.migrations_not_used'
def __contains__(self, key):
return True
MIGRATION_MODULES = IgnoreAllMigrations()
@pricco
pricco / merge_migrations.md
Last active August 29, 2015 14:08
Merge Django 1.7 Migrations

Merge Django 1.7 Migrations

  • Remove migrations for each app
  • Run makemigrations for each app
  • Remove each migration > 0001
  • Edit each 0001_initial migration
  • Remove operations distinct of migrations.CreateModel
  • Remove fields of type ForeignKeyField or ManyToManyField
  • Clear dependencies = []
  • Run makemigrations again for each app
  • Edit each 0002 migration and change each dependency for 0001_initial
@pricco
pricco / loaders.py
Created May 19, 2014 19:31
Wrapper for loading templates from "templates" directories in the specified apps.
"""
Wrapper for loading templates from "templates" directories in the specified
apps.
>>> {% extends 'grappelli,admin:admin/change_list.html' %}
"""
import os
import sys
@pricco
pricco / history.sh
Last active August 1, 2022 02:20
List commits of all subdirectories for the specified date: ./history.sh 2013-12-28
exec 2> /dev/null
original=`pwd`
function history {
d=$1
author=$2
for i in $(find . -type d -print -maxdepth 3); do
cd "${i}"
if [ -d ".git" ]; then
h=`git rev-list --after "${d}T00:00:00" --before "${d}T23:59:59" --tags --branches --remotes --author=${author} --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' | sed '/^commit/ d'`
from os.path import splitext
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _
from django.template.defaultfilters import filesizeformat
class FileValidator(object):
"""
Validator for files, checking the size, extension and mimetype.
@pricco
pricco / common.py
Created April 2, 2013 18:32
Remove accents django filter.
import unicodedata
from django import template
register = template.Library()
@register.filter
def remove_accents(value):
nkfd_form = unicodedata.normalize('NFKD', unicode(value))
return u"".join([c for c in nkfd_form if not unicodedata.combining(c)])
@pricco
pricco / qurl.py
Last active July 22, 2016 22:01
Django Template Tag: Url query string modifier
"""qurl is a tag to append, remove or replace query string parameters from an url (preserve order)"""
import re
from django.template import Library, Node, TemplateSyntaxError
from urlparse import urlparse, parse_qsl, urlunparse
from django.utils.encoding import smart_str
from urllib import urlencode
register = Library()
@pricco
pricco / gist:3440266
Created August 23, 2012 18:53 — forked from caseman/gist:3428752
Ctor: Lightweight Javascript Constructors with Inheritance

Ctor: Lightweight Javascript Constructors with Inheritance

Author: Casey Duncan @iamnotcasey

This Javascript constructor with inheritance pattern is designed as a lightweight alternative to other methods I've seen while still providing a nice abstraction as a 13 line function that can be easily inlined into your code.