Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home... permanently!

Andrea Grandi andreagrandi

🏠
Working from home... permanently!
View GitHub Profile
@andreagrandi
andreagrandi / substitute_string
Created May 21, 2013
Substitute a placeholder like {{hostname}} that is inside a file, with the output of the hostname command.
View substitute_string
sed -i "s/{{hostname}}/`hostname`/g" /etc/supervisor/conf.d/celeryd.conf
@andreagrandi
andreagrandi / bitly_test.py
Created Jul 19, 2013
An example of Bitly API use.
View bitly_test.py
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 Aug 6, 2013
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).
View site_example
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 Aug 6, 2013
This is an example of an API call (a GET method) using the requests Python library.
View test_api.py
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 Aug 6, 2013
Custom JSONRenderer for DjangoRestFramework
View drf_renderer.py
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 Aug 6, 2013
A more compact version of FbxEncoder for DjangoRestFramework.
View drf_encoder2.py
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 Aug 12, 2013
Bazaar hook to trigger a Jenkins build.
View post_change_branch_tip_hook.py
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 Oct 30, 2019
Read a Google Docs spreadsheet document from Python.
View gdata_read.py
# 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 Aug 20, 2013
Add colored git branch name to your shell prompt
View git_branch_shell.sh
# 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 Sep 13, 2013
Custom Django Rest Framework exception. How to add {"Success": false} to the response.
View custom_drf_ex.py
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