Skip to content

Instantly share code, notes, and snippets.

View stephane's full-sized avatar
🎯
Focusing

Stéphane Raimbault stephane

🎯
Focusing
View GitHub Profile
@stephane
stephane / gist:5450978
Created April 24, 2013 09:39
Avoid multiple clicks on same DOM object and reactivates it after 5 seconds.
function disable_a_moment(o) {
o.disabled = true;
o.value = o.value + "…";
setTimeout(function() {
o.disabled = false;
o.value = o.value.substr(0, o.value.length - 1);
}, 5000);
}
@stephane
stephane / Gruntfile.js
Created May 14, 2014 14:20
Grunt task to extract and compile PO files
/* global module */
module.exports = function(grunt) {
'use strict';
grunt.loadNpmTasks('grunt-angular-gettext');
grunt.initConfig({
nggettext_extract: {
ops: {
@stephane
stephane / Makefile
Created May 14, 2014 14:22
Makefile target to generate PO file from POT (angular-gettext)
makemessagesjs:
grunt nggettext_extract; \
export PO_FILE=locale/fr/LC_MESSAGES/angular.po; \
export POT_FILE=$${PO_FILE}t; \
[ ! -f $$PO_FILE ] && msginit -i $$POT_FILE -o $$PO_FILE || msgmerge --update $$PO_FILE $$POT_FILE; \
rm $$POT_FILE;
@stephane
stephane / libsoup and Django
Last active August 29, 2015 14:15
Interface Django with C code with libsoup
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <glib.h>
#include <libsoup/soup.h>
#include <libsoup/soup-auth.h>
static char* django_get_csrftoken_from_body(SoupMessage *msg)
{
@stephane
stephane / gist:5e614da08649e59ef3c6
Created February 25, 2015 17:40
Duplicate lines
(defun duplicate-current-line-or-region (arg)
"Duplicates the current line or region ARG times.
If there's no region, the current line will be duplicated. However, if
there's a region, all lines that region covers will be duplicated."
(interactive "p")
(let (beg end (origin (point)))
(if (and mark-active (> (point) (mark)))
(exchange-point-and-mark))
(setq beg (line-beginning-position))
(if mark-active
@stephane
stephane / gist:19fddde21391ea2d42f3
Created February 25, 2015 17:41
To rename and delete buffer/file
(defun rename-this-buffer-and-file ()
"Renames current buffer and file it is visiting."
(interactive)
(let ((name (buffer-name))
(filename (buffer-file-name)))
(if (not (and filename (file-exists-p filename)))
(error "Buffer '%s' is not visiting a file!" name)
(let ((new-name (read-file-name "New name: " filename)))
(cond ((get-buffer new-name)
(error "A buffer named '%s' already exists!" new-name))
@stephane
stephane / models.py
Last active August 26, 2015 20:05
Django - Many to many with fixed list of choices
class Issue(models.Model):
title = models.CharField(max_length=256)
class Label(models.Model):
issue = models.ForeignKey(Issue, related_name='labels')
NAME_CHOICES = (
('bug', "Bug"),
('feature', "Feature"),
('blocker', "Release blocker"),
)
@stephane
stephane / models.py
Last active August 26, 2015 20:23
ArrayField in Django 1.8
class Issue(models.Model):
LABEL_CHOICES = (
('bug', "Bug"),
('feature', "Feature"),
('blocker', "Release blocker"),
)
title = models.CharField(max_length=256)
labels = ArrayField(
models.CharField(max_length=32, choices=LABEL_CHOICES),
default=[], blank=True)
@stephane
stephane / forms.py
Last active April 8, 2021 08:10
Widget to render ArrayField in Django. Written by Brad Montgomery.
from django.utils.datastructures import MultiValueDict
class ArrayFieldSelectMultiple(forms.SelectMultiple):
"""This is a Form Widget for use with a Postgres ArrayField. It implements
a multi-select interface that can be given a set of `choices`.
You can provide a `delimiter` keyword argument to specify the delimeter used.
"""
def __init__(self, *args, **kwargs):
# Accept a `delimiter` argument, and grab it (defaulting to a comma)
@stephane
stephane / admin.py
Created August 26, 2015 20:22
How to override the widget of a field in the administration of Django
class IssueAdminForm(forms.ModelForm):
class Meta:
model = app_models.Issue
fields = ['title', 'labels']
widgets = {
'labels': app_forms.ArrayFieldSelectMultiple(
choices=app_models.Issue.LABEL_CHOICES),
}
class IssueAdmin(admin.ModelAdmin):