Skip to content

Instantly share code, notes, and snippets.

View umutcoskun's full-sized avatar
🌴
On vacation

Umut Çağdaş Coşkun umutcoskun

🌴
On vacation
View GitHub Profile
@umutcoskun
umutcoskun / csv_to_customerio.py
Created November 30, 2016 08:22
Import users from CSV to Customer.IO
import csv
from customerio import CustomerIO
site_id = ''
api_key = ''
cio = CustomerIO(site_id, api_key)
with open('data.csv', 'r') as f:
@umutcoskun
umutcoskun / django_filter_phonenumbers.py
Created January 12, 2017 11:26
Django custom filter that formats phone numbers using 'phonenumbers' module.
@register.filter(name='format_phonenumber')
def format_phonenumber(phone, country='TR'):
"""
Usage:
{{ user.phone|format_phonenumber }}
{{ user.phone|format_phonenumber: 'TR' }}
"""
import phonenumbers
return phonenumbers.format_number(
phonenumbers.parse(phone, country),
@umutcoskun
umutcoskun / django_filter_md5.py
Created August 30, 2017 08:00
Django custom template filter to hashing value.
from django import template
register = template.Library()
@register.filter(name='md5')
def md5(value):
"""
Usage:
{{ user.email|md5 }}
@umutcoskun
umutcoskun / django_filter_amp.py
Created August 30, 2017 08:05
Django custom template filter that replaces 'img' tags with 'amp-img'. The img tags must have 'height' and 'width' attributes for validated AMP output.
from django import template
register = template.Library()
@register.filter(name='amp')
def amp(value):
"""
Usage:
{{ article.body|amp }}
from django.shortcuts import render
def home(request):
context = {
'name': 'Umut',
'language': 'Türkçe',
}
return render(request, 'home.html', context)
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.request',
],
},
from django.conf import settings
def constants(request):
return {
'BASE_TITLE': settings.BASE_TITLE,
'BASE_URL': settings.BASE_URL,
}
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
# ...
'blog.context_processors.constants',
],
from blog.models import Tag
def tags(request):
published_tags = Tag.objects\
.filter(is_published=True, posts__is_published=True)\
.exclude(posts=None)\
.order_by('name')\
.distinct()
return {'tags': published_tags}
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
# ...
'blog.context_processors.constants',
'blog.context_processors.tags',