Skip to content

Instantly share code, notes, and snippets.

View guillaumepiot's full-sized avatar

Guillaume Piot guillaumepiot

View GitHub Profile
@guillaumepiot
guillaumepiot / gist:3939452
Created October 23, 2012 15:28
ANGULARJS - Django CSRF Token header setup
var myApp = angular.module('myApp').config(function($httpProvider) {
$httpProvider.defaults.headers.post['X-CSRFToken'] = $('input[name=csrfmiddlewaretoken]').val();
});
@guillaumepiot
guillaumepiot / PYTHON - Convert URL in text to anchor tags (links)
Created January 15, 2013 16:42
PYTHON - Convert URL in text to anchor tags (links)
@guillaumepiot
guillaumepiot / DJANGO - Smart truncate chars
Last active December 11, 2015 21:58
DJANGO - Template tag for smart truncate chars. Limit the input value by a number of character, but it will not remove the last word if it's cut.
from django import template
register = template.Library()
# Truncate chars but leaving last word complete
@register.filter("smart_truncate_chars")
def smart_truncate_chars(value, max_length):
if len(value) > max_length:
# Limits the number of characters in value tp max_length (blunt cut)
truncd_val = value[:max_length]
# Check if the next upcoming character after the limit is not a space, in which case it might be a word continuing
@guillaumepiot
guillaumepiot / gist:4949486
Created February 13, 2013 23:50
Python model instance copy
def copy_model_instance(obj):
"""
Create a copy of a model instance.
M2M relationships are currently not handled, i.e. they are not copied. (Fortunately, you don't have any in this case)
See also Django #4027. From http://blog.elsdoerfer.name/2008/09/09/making-a-copy-of-a-model-instance/
"""
initial = dict([(f.name, getattr(obj, f.name)) for f in obj._meta.fields if not isinstance(f, AutoField) and not f in obj._meta.parents.values()])
return obj.__class__(**initial)
class MyAdmin(admin.ModelAdmin):
list_filter = ['market']
# Duplicate list_filter to assign on a pre request basis
_list_filter = list_filter
# Change list filter on the view depending on user
def changelist_view(self, request, extra_context=None):
if not request.user.is_superuser:
self.list_filter = None
@guillaumepiot
guillaumepiot / gist:5338169
Last active December 15, 2015 23:09
DJANGO - Default context processor for Cotidia CMS
import os
from django.contrib.sites.shortcuts import get_current_site
from django.conf import settings
def website_settings(request):
site = get_current_site(request)
data = {
'SITE_NAME': site.name,
@guillaumepiot
guillaumepiot / gist:5363386
Last active December 16, 2015 02:29
Grunt default config file
module.exports = function(grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
copy: {
main: {
files: [
{expand: true, cwd: 'components/bootstrap/', src: ['less/*.less', 'img/*'], dest: 'src/', filter: 'isFile'}, // includes files in path
{expand: true, cwd: 'components/bootstrap/js/', src: ['*.js'], dest: 'src/js/bootstrap/', filter: 'isFile'}, // includes files in path
{expand: true, cwd: 'components/jquery/', src: ['jquery.js'], dest: 'src/js/', filter: 'isFile'}
//{src: ['path/**'], dest: 'dest/'}, // includes files in path and its subdirs
@guillaumepiot
guillaumepiot / gist:5363418
Last active December 16, 2015 02:29
Grunt default package.json
{
"name": "deploy",
"version": "0.0.1",
"devDependencies": {
"grunt": "latest",
"grunt-contrib-uglify": "latest",
"grunt-contrib-jshint": "latest",
"grunt-contrib-watch": "latest",
"grunt-contrib-concat": "latest",
"grunt-contrib-copy": "latest"
@guillaumepiot
guillaumepiot / dostuff.py
Created April 12, 2013 09:24
DJANGO - Default command
from django.core.management.base import BaseCommand, CommandError
class Command(BaseCommand):
args = '<arg1 arg2 ...>'
help = 'Command to do ...'
def handle(self, *args, **options):
@guillaumepiot
guillaumepiot / gist:5391705
Last active December 16, 2015 06:29
DJANGO - ADMIN TOOLS - Menu default class
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _
from admin_tools.menu import items, Menu
# to activate your custom menu add the following to your settings.py:
#
# ADMIN_TOOLS_MENU = 'my_project.menu.CustomMenu'
class CustomMenu(Menu):
"""