Skip to content

Instantly share code, notes, and snippets.

Augusto Destrero baxeico

Block or report user

Report or block baxeico

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@baxeico
baxeico / excel_models.py
Last active Nov 9, 2017
Django custom command to write model fields on an Excel file (e.g. for documentation purposes)
View excel_models.py
# coding=utf-8
from __future__ import unicode_literals
import logging
from django.core.management.base import BaseCommand, CommandError
from django.apps import apps
from xlsxwriter.workbook import Workbook
logger = logging.getLogger(__name__)
@baxeico
baxeico / smtp_server.py
Created May 10, 2017
A simple Python email gateway
View smtp_server.py
import smtpd
import asyncore
import logging
import email
from email.header import decode_header
import requests
logger = logging.getLogger(__name__)
class CustomSMTPServer(smtpd.SMTPServer):
@baxeico
baxeico / allow_cors_mixin.py
Last active Oct 7, 2018
Simple mixin to add CORS headers in a Django View
View allow_cors_mixin.py
from django.http import HttpResponse
class AllowCORSMixin(object):
def add_access_control_headers(self, response):
response["Access-Control-Allow-Origin"] = "*"
response["Access-Control-Allow-Methods"] = "GET, OPTIONS"
response["Access-Control-Max-Age"] = "1000"
response["Access-Control-Allow-Headers"] = "X-Requested-With, Content-Type"
@baxeico
baxeico / queryset_iterator.py
Last active Feb 9, 2018
queryset_iterator, a function to iterate over huge Django querysets without using too much memory
View queryset_iterator.py
import gc
def queryset_iterator(qs, batchsize = 500, gc_collect = True):
iterator = qs.values_list('pk', flat=True).order_by('pk').distinct().iterator()
eof = False
while not eof:
primary_key_buffer = []
try:
while len(primary_key_buffer) < batchsize:
primary_key_buffer.append(iterator.next())
View update_migration.sh
#!/bin/bash
if [ -z $1 ]; then
echo "Usage: $0 app_label"
exit 1
fi
app=$1 # this is the name of the app where we want to update the last migration
# get the list of known migrations for the app
View canvas_sharing_example.js
angular.module('canvas_share', ['ionic', 'ngCordova'])
.controller('CanvasShareCtrl', function($scope, $cordovaSocialSharing, $q) {
var buildImage = function() {
var deferred = $q.defer();
// create the canvas
var canvas = document.createElement('canvas');
canvas.width = 400;
canvas.height = 120;
var context = canvas.getContext('2d');
// draw a rectangular white frame for our content
@baxeico
baxeico / Code.gs
Created Feb 9, 2015
Google App Script to geocode a list of addresses in a Spreadsheet
View Code.gs
function myFunction() {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getDataRange();
var cells = range.getValues();
var latitudes = [];
var longitudes = [];
for (var i = 0; i < cells.length; i++) {
@baxeico
baxeico / views.py
Created Sep 30, 2014
Fixing a memleak in a Django view
View views.py
from django.http import HttpResponse
from .models import FirstModel
def my_view(request):
# this queryset contains about 100k records
# each of them has many ForeignKeys to other models
huge_queryset = FirstModel.objects.all().iterator()
f = open('dumb.dump', 'w')
@baxeico
baxeico / views.py
Last active Aug 29, 2015
A huge "memleak" in a simple Django view
View views.py
from django.http import HttpResponse
from .models import FirstModel
# this view will make crazy use of the RAM ;)
def my_view(request):
# this queryset contains about 100k records
# each of them has many ForeignKeys to other models
huge_queryset = FirstModel.objects.all()
@baxeico
baxeico / views.py
Last active Aug 29, 2015
Find and fix a django "memleak"
View views.py
import objgraph
import sys
import logging
from django.http import HttpResponse
from .models import FirstModel
logger = logging.getLogger(__name__)
You can’t perform that action at this time.