Skip to content

Instantly share code, notes, and snippets.

View guillaumepiot's full-sized avatar

Guillaume Piot guillaumepiot

View GitHub Profile
@guillaumepiot
guillaumepiot / gist:6421261
Last active December 22, 2015 04:59
DJANGO - Simple contact form template tag
from django import forms
from django.core.mail import EmailMultiAlternatives
from django.conf import settings
from django import template
register = template.Library()
class ContactForm(forms.Form):
name = forms.CharField(error_messages={ 'required':'Please enter your name'})
email = forms.EmailField(error_messages={'invalid': 'This email is not valid', 'required':'Please enter your email'})
@guillaumepiot
guillaumepiot / gist:6766868
Last active December 24, 2015 07:49
CMS Base - Default settings
"""
Django settings for {{myproject}} project.
Generated by 'django-admin startproject' using Django 1.8.3.
For more information on this file, see
https://docs.djangoproject.com/en/1.8/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.8/ref/settings/
@guillaumepiot
guillaumepiot / gist:6974524
Created October 14, 2013 11:58
JS - Info message (one off discardable messages)
// Namespace to handle disclaimer display depending on the user's cookies
// Written by Guillaume Piot gpiot.com
// Inspiration from W3 Schools http://www.w3schools.com/js/js_cookies.asp
// Example use for a cookie disclaimer
// Cookies disclaimer
// var cookie_info = new InfoMessage($('.alert-cookies'), 'cookies_policy');
// $('.close-disclaimer').click(function(e){
// e.preventDefault();
// cookie_info.close();
@guillaumepiot
guillaumepiot / gist:6976698
Last active December 25, 2015 12:29
JS - AJAX Form Submit Handling
<script>
head.js('/static/js/jquery-1.7.1.min.js', '/static/js/jquery.form.js', function(){
// bind 'myForm' and provide a simple callback function
var formdiv = '#form-id'
var options = {
beforeSubmit: function(){
$('#form-edit-basecamp #submit').attr('value','{% trans "Saving..." %}');
},
success: function(data) {
// Remove prior error messages
@guillaumepiot
guillaumepiot / gist:7120407
Created October 23, 2013 14:59
JS - Placeholder fall back
/* This code is licensed under Creative Commons Attribution 3.0 *
* You may share and remix the script so long as you attribute the *
* original author, Andrew January. *
* http://creativecommons.org/licenses/by/3.0/ */
$(document).ready(function() {
// Check to see if the browser already supports placeholder text (introduced in HTML5). If it does,
// then we don't need to do anything.
var i = document.createElement('input');
if ('placeholder' in i) {
@guillaumepiot
guillaumepiot / gist:7213040
Created October 29, 2013 11:32
JS - Sort array by key
function sortByKey(array, key) {
return array.sort(function(a, b) {
var x = a[key]; var y = b[key];
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
});
}
// Example use
// Will update the `my_data` array
//sortByKey(my_data, 'order_id');
@guillaumepiot
guillaumepiot / gist:7693860
Created November 28, 2013 15:40
PYTHON - Clean GET var ready for DB query
import re
identifier = re.sub('\W+', ' ', identifier).strip()
@guillaumepiot
guillaumepiot / gist:7805726
Created December 5, 2013 14:12
UNSLIDER JS - Thumbnails navigation control and trailer
// Extension for unslider.js, add a thumbnail trailer
// HOW TO USE
// Include this script in your page first, after unslider.js
// Then output the thumbnails as follows:
// <div class="slide-control">
// <a href="#prev" class="unslider-arrow prev"></a>
// <a href="#next" class="unslider-arrow next"></a>
// <div class="thumbnail_slider" style="width: 368px;">
@guillaumepiot
guillaumepiot / gist:7942710
Last active December 31, 2015 05:39
GOOGLE MAP - Load map from Lat/Lng coordinates
// This script will load a Google map in #map_canvas (div)
//
// You must include the Google map library in the head of your document:
// <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
//
// The #map_canvas must have a minimim height to show.
// eg: #map_canvas { height:300px; }
//
// Then, at the bottom of the page, or after <div id="map_canvas"></div>
// Include the following JS:
@guillaumepiot
guillaumepiot / gist:8473955
Last active December 16, 2022 15:31
PYTHON / DJANGO - CSV Export view
def export_profile(request):
import csv, StringIO, datetime
date = datetime.datetime.now()
# Create the HttpResponse object with the appropriate CSV header.
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="profiles-%s.csv"' % (date.strftime('%d-%m-%Y'))
writer = csv.writer(response)