Skip to content

Instantly share code, notes, and snippets.

View guillaumepiot's full-sized avatar

Guillaume Piot guillaumepiot

View GitHub Profile
@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
@guillaumepiot
guillaumepiot / custom_modelchoicefield.py
Last active July 5, 2023 14:01
Django Form - Custom ModelChoiceField label
from django import forms
from django.utils.safestring import mark_safe
class CustomModelChoiceField(forms.ModelChoiceField):
def label_from_instance(self, obj):
return mark_safe("My Object custom label <strong>%i</strong>" % obj.id)
class MyForm(forms.ModelForm):
my_field = CustomModelChoiceField(label=_('The form label'), queryset=MyModel.objects.filter(), widget=forms.RadioSelect, empty_label=None)
@guillaumepiot
guillaumepiot / PYTHON - Convert URL in text to anchor tags (links)
Created January 15, 2013 16:42
PYTHON - Convert URL in text to anchor tags (links)
@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
@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)
@guillaumepiot
guillaumepiot / gist:5583149
Created May 15, 2013 10:54
DJANGO - Admin custom list filter
class CategoryListFilter(SimpleListFilter):
# USAGE
# In your admin class, pass trhe filter class as tuple for the list_filter attribute:
#
# list_filter = (CategoryListFilter,)
# Human-readable title which will be displayed in the
# right admin sidebar just above the filter options.
@guillaumepiot
guillaumepiot / gist:3939452
Created October 23, 2012 15:28
ANGULARJS - Django CSRF Token header setup
var myApp = angular.module('myApp').config(function($httpProvider) {
$httpProvider.defaults.headers.post['X-CSRFToken'] = $('input[name=csrfmiddlewaretoken]').val();
});
@guillaumepiot
guillaumepiot / thumbnail-carousel.bxslider.html
Last active July 11, 2019 18:43
bxSlider with thumbnails carousel
@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."))
# 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