Skip to content

Instantly share code, notes, and snippets.

View baxeico's full-sized avatar

Augusto Destrero baxeico

View GitHub Profile
baxeico /
Created April 8, 2024 20:08 — forked from alexhayes/
Django management command to dump objects to JSON with all nested relations included.
Dump out objects and all nested associations.
import sys
from django.apps import apps
from django.contrib.admin.utils import NestedObjects
from django.core import serializers
from import BaseCommand
from django.db import router
baxeico /
Created April 9, 2021 14:50
Django command with lockfile
import logging
from os.path import exists
from os import remove, getpid
from import BaseCommand
logger = logging.getLogger(__name__)
class LockedCommand(BaseCommand):
baxeico /
Last active November 9, 2017 18:11
Django custom command to write model fields on an Excel file (e.g. for documentation purposes)
# coding=utf-8
from __future__ import unicode_literals
import logging
from import BaseCommand, CommandError
from django.apps import apps
from xlsxwriter.workbook import Workbook
logger = logging.getLogger(__name__)
baxeico /
Created May 10, 2017 13:18
A simple Python email gateway
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 /
Last active August 6, 2020 13:40
Simple mixin to add CORS headers in a Django View
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 /
Last active February 9, 2018 09:45
queryset_iterator, a function to iterate over huge Django querysets without using too much memory
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 = []
while len(primary_key_buffer) < batchsize:
if [ -z $1 ]; then
echo "Usage: $0 app_label"
exit 1
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
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 /
Created February 9, 2015 15:07
Google App Script to geocode a list of addresses in a Spreadsheet
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 /
Created September 30, 2014 16:17
Fixing a memleak in a Django view
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')