Skip to content

Instantly share code, notes, and snippets.

View andreagrandi's full-sized avatar
🏠
Working from home... permanently!

Andrea Grandi andreagrandi

🏠
Working from home... permanently!
View GitHub Profile
@andreagrandi
andreagrandi / substitute_string
Created May 21, 2013 10:25
Substitute a placeholder like {{hostname}} that is inside a file, with the output of the hostname command.
sed -i "s/{{hostname}}/`hostname`/g" /etc/supervisor/conf.d/celeryd.conf
@andreagrandi
andreagrandi / bitly_test.py
Created July 19, 2013 11:10
An example of Bitly API use.
import requests
from urllib import urlencode
token = '**************************************************'
url_base = 'https://api-ssl.bitly.com/v3/'
url = 'http://www.repubblica.it'
parameters = [('access_token', token), ('longUrl', url)]
request = requests.get(url_base + 'shorten/', params = urlencode(parameters))
@andreagrandi
andreagrandi / site_example
Created August 6, 2013 09:49
This nginx rule redirects all the traffic that arrive on port 80 of the web server to the internal port 8000 (for example where a Django application is running).
server {
listen 80;
server_name mywebsite.com;
access_log /var/log/www/mywebsite.com/log/nginx.access.log;
error_log /var/log/www/mywebsite.com/log/nginx_error.log debug;
#set your default location
location / {
proxy_pass http://127.0.0.1:8000/;
proxy_redirect off;
@andreagrandi
andreagrandi / test_api.py
Created August 6, 2013 09:54
This is an example of an API call (a GET method) using the requests Python library.
import requests
from urllib import urlencode
api_base_url = 'http://localhost:8001/api/1/'
api_token = '******************************************'
auth_header = {'Authorization': 'Token ' + api_token}
parameters = [('type', 'adcountry'), ('limit', 1000)]
request = requests.get(api_base_url + 'facebook/search/', params = urlencode(parameters), headers = auth_header)
@andreagrandi
andreagrandi / drf_renderer.py
Created August 6, 2013 11:29
Custom JSONRenderer for DjangoRestFramework
from __future__ import unicode_literals
from django.utils.functional import Promise
from rest_framework.compat import timezone, force_text
from django.utils.encoding import smart_unicode
from rest_framework import renderers
import datetime
import decimal
import types
import json
from bson import ObjectId
@andreagrandi
andreagrandi / drf_encoder2.py
Created August 6, 2013 11:45
A more compact version of FbxEncoder for DjangoRestFramework.
from rest_framework import renderers
from rest_framework.utils.encoders import JSONEncoder
from bson import ObjectId
class FbxJSONEncoder(JSONEncoder):
"""
FbxJSONEncoder subclass that knows how to encode date/time/timedelta,
decimal types, generators and Mongo ObjectId.
"""
def default(self, o):
@andreagrandi
andreagrandi / post_change_branch_tip_hook.py
Created August 12, 2013 18:26
Bazaar hook to trigger a Jenkins build.
from bzrlib import branch
import urllib
JENKINS_URL = 'http://.....'
def post_change_branch_tip_hook(push_result):
urllib.openurl(JENKINS_URL)
branch.Branch.hooks.install_named_hook('post_change_branch_tip',
post_change_branch_tip_hook, 'Jenkins build hook')
@andreagrandi
andreagrandi / gdata_read.py
Last active October 30, 2019 22:30
Read a Google Docs spreadsheet document from Python.
# Copyright (c) 2013, Andrea Grandi and individual contributors.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# 1) Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2) Redistributions in binary form must reproduce the above copyright notice,
@andreagrandi
andreagrandi / git_branch_shell.sh
Created August 20, 2013 20:29
Add colored git branch name to your shell prompt
# Credits: http://qugstart.com/blog/git-and-svn/add-colored-git-branch-name-to-your-shell-prompt/
function parse_git_branch_and_add_brackets {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\ \[\1\]/'
}
PS1="\h:\W \u\[\033[0;32m\]\$(parse_git_branch_and_add_brackets) \[\033[0m\]\$ "
@andreagrandi
andreagrandi / custom_drf_ex.py
Created September 13, 2013 14:41
Custom Django Rest Framework exception. How to add {"Success": false} to the response.
from rest_framework.views import exception_handler
def custom_exception_handler(exc):
# Call REST framework's default exception handler first,
# to get the standard error response.
response = exception_handler(exc)
# Now add the HTTP status code to the response.
if response is not None:
response.data['Success'] = False