Skip to content

Instantly share code, notes, and snippets.

View guillaumepiot's full-sized avatar

Guillaume Piot guillaumepiot

View GitHub Profile
@guillaumepiot
guillaumepiot / show-hide-fields.js
Created January 29, 2018 17:09
Show / hide fields based on field value
var required_financial_fields = {{LOAN_REQUIRED_FINANCIAL_FIELDS|safe}};
var fields = []
for(var key in required_financial_fields) {
fields = fields.concat(required_financial_fields[key])
}
function hideAllFields() {
for(var i = 0; i < fields.length; i++) {
document.querySelector('[name='+fields[i]+']').parentNode.parentNode.style.display = "none";
@guillaumepiot
guillaumepiot / parallax.js
Last active August 14, 2017 13:58
parallax.js
// For a given HTML snippet with the class "parallax"
//
// <div class="parallax" style="height: 400px; overflow: hidden;">
// <div style="background-image:url('http://via.placeholder.com/1600x780');"></div>
// </div>
(function(){
var speedRatio = 4
var minYOffset = -200
@guillaumepiot
guillaumepiot / checkAll.js
Created August 9, 2017 14:07
Javascript: Check all / Check none
function toggleCheckboxes(){
var checkboxes = document.querySelectorAll("[name=accommodation]")
var allChecked = Array.prototype.every.call(checkboxes, function(item){ return item.checked })
if (allChecked) {
checkboxes.forEach(function(item){ item.checked = false })
} else {
checkboxes.forEach(function(item){ item.checked = true })
}
}
@guillaumepiot
guillaumepiot / format_field_to_currency.js
Last active May 12, 2017 08:28
Augment text input to format currency - Duplicate origin field, format to currency display
// Author: Guillaume Piot, Alex Russell
// Copyright 2017
// MIT License
var currency_fields = document.getElementsByClassName('form__text--currency')
function formatCurrencyField(elm){
// Duplicate the element to create:
// - a hidden version with the integer value tp submitted to the form
// - a new text input to display the formatted value
@guillaumepiot
guillaumepiot / test_image_upload.py
Last active January 22, 2024 19:19
Django Rest Framework - Image/File upload test
import os
import io
from PIL import Image
from django.core.urlresolvers import reverse
from django.conf import settings
from rest_framework import status
from rest_framework.test import APITestCase
# Example integration of a background-image uploader
# Author: Guillaume Piot
# Email: guillaume@cotidia.com
# Company: Cotidia Ltd
# Licence: MIT
#
# The div holder is absolute positioned within the parent div
#
# <div class="[ article__image ] [ article-image ] [ editable ] [ parallax ]" data-name="article_image">
# <div
@guillaumepiot
guillaumepiot / weeks_in_month.js
Last active August 10, 2018 15:38
Weeks in month using Moment.js
var get_calendar;
var calendar, endDay, firstDay, firstWeekDay, headerRow, i, j, lastWeekDay, len, len1, month, monthRange, row, startDate, week, weekRange, weeks, year,
indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
get_calendar = function(year, month) {
startDate = moment([year, month]);
firstDay = moment(startDate).startOf('month');
endDay = moment(startDate).endOf('month');
monthRange = moment.range(firstDay, endDay);
weeks = [];
@guillaumepiot
guillaumepiot / weeks_in_month.coffee
Last active March 8, 2023 07:34
Get all the weeks in a given month and year using Moment.js
# year and month are variables
year = 2015
month = 7 # August (0 indexed)
startDate = moment([year, month])
# Get the first and last day of the month
firstDay = moment(startDate).startOf('month')
endDay = moment(startDate).endOf('month')
# Create a range for the month we can iterate through
"""
Two things are wrong with Django's default `SECRET_KEY` system:
1. It is not random but pseudo-random
2. It saves and displays the SECRET_KEY in `settings.py`
This snippet
1. uses `SystemRandom()` instead to generate a random key
2. saves a local `secret.txt`
@guillaumepiot
guillaumepiot / mysite.conf
Last active December 10, 2015 12:05
WSGI - Upstart file for Django 1.9
description "uWSGI server for <domain-name>"
start on runlevel 2
stop on runlevel [016]
respawn
exec /<path-to-myvenv>/bin/uwsgi --chdir=<code_dir> \
--module=<app-name>.wsgi:application \
--env DJANGO_SETTINGS_MODULE=<app-name>.settings.production \
--master --pidfile=/tmp/<mysitename>-master.pid \
--socket=127.0.0.1:<wsgi_port> \
--processes=3 \