This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
usernames = ['@guillaumepiot','@lemans','@guillaumepiot'] | |
isUnique = (value, index, array)-> | |
array.indexOf(value) == index | |
console.log usernames.filter(isUnique) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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)-> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# * Add jquery support for checking if an element is in view | |
$.fn.inView = () -> | |
$win = $(window) | |
viewport = { | |
top: $win.scrollTop() | |
left: $win.scrollLeft() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): | |
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Deep copy the *items* list of objects | |
items_list = [] | |
for item in items | |
items_list.push($.extend({}, item)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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.")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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' | |