Skip to content

Instantly share code, notes, and snippets.

View guillaumepiot's full-sized avatar

Guillaume Piot guillaumepiot

View GitHub Profile
@guillaumepiot
guillaumepiot / array-uniquify.coffee
Last active August 29, 2015 14:19
COFFEESCRIPT - Uniquify Array
usernames = ['@guillaumepiot','@lemans','@guillaumepiot']
isUnique = (value, index, array)->
array.indexOf(value) == index
console.log usernames.filter(isUnique)
@guillaumepiot
guillaumepiot / abbreviate.coffee
Last active August 29, 2015 14:16
COFFEESCRIPT - Abbreviate string
window.abbreviate = (str, max, suffix) ->
#
# Trim leading and trailing white space
#
str = str.replace /^\s+|\s+$/g, ""
#
# If the string is less than max, return it
#
@guillaumepiot
guillaumepiot / gist:56191c0256f9ae9bf8e1
Created December 16, 2014 14:08
JAVASCRIPT - Set cursor positon in input and textarea
setSelectionRange: (input, selectionStart, selectionEnd)->
if input.setSelectionRange
input.focus()
input.setSelectionRange(selectionStart, selectionEnd)
else if input.createTextRange
range = input.createTextRange()
range.collapse(true)
range.moveEnd('character', selectionEnd)
@guillaumepiot
guillaumepiot / gist:4e1028076b115ab93439
Created November 25, 2014 12:40
JAVASCRIPT - Count number format
# Format count numbers depending on how they are:
# 1-999: 999
# 1000+: 1K
# 1100+: 1.1K
# 10000+: 10K
# 100000+: 100K
# 1000000+: 1M
# 1100000+: 1.1M
window.format_count = (value)->
@guillaumepiot
guillaumepiot / gist:a61094256997ddd8cfe6
Created October 20, 2014 14:03
JQUERY - Check if element is in view
# * Add jquery support for checking if an element is in view
$.fn.inView = () ->
$win = $(window)
viewport = {
top: $win.scrollTop()
left: $win.scrollLeft()
}
@guillaumepiot
guillaumepiot / gist:d8985bc8aa4f1d660174
Last active August 29, 2015 14:07
DJANGO - Minimum required formset
from django import forms
# how to use:
# Formset = inlineformset_factory(ParentModel, Model, ModelForm, \
# formset=MinimumRequiredFormSet, extra=1, can_delete=True)
# instance = Formset(request.POST or None, instance=obj, prefix="prefix_form", \
# minimum_forms=2, minimum_forms_message="At least two items are required.",)
class MinimumRequiredFormSet(forms.models.BaseInlineFormSet):
"""
@guillaumepiot
guillaumepiot / gist:edea914f06558581737a
Created October 6, 2014 12:45
Python - Time difference formatting
from dateutil.relativedelta import relativedelta
# Calculate the relative timedelta
relative_delta = relativedelta(now_gmt, posted_time_gmt)
relative_time_format = {
'future' : 'in %s',
'past' : '%s ago',
's' : 'a few seconds',
'm' : 'a minute',
@guillaumepiot
guillaumepiot / array-deep-copy.coffee
Last active August 29, 2015 14:07
Javascript - Array deep copy with jQuery $.extend
# Deep copy the *items* list of objects
items_list = []
for item in items
items_list.push($.extend({}, item))
@guillaumepiot
guillaumepiot / forms.py
Last active March 28, 2019 11:53
DJANGO CookBook - File type validation in form
# Tested in Django 1.6
class ImportForm(forms.Form):
csv_file = forms.FileField(label=_('Select CSV file'))
def clean_csv_file(self):
f = self.cleaned_data['csv_file']
if not f.content_type in ['text/csv',]:
raise forms.ValidationError(_("The file type is not accepted."))
@guillaumepiot
guillaumepiot / admin.py
Last active November 16, 2016 19:39
DJANGO CookBook - Custom list filter
# Tested in Django 1.6
# Import default list filter
from django.contrib.admin import SimpleListFilter
# Create the filter
class InvoicePaidFilter(SimpleListFilter):
title = _('Paid')
parameter_name = 'paid'