Skip to content

Instantly share code, notes, and snippets.

Avatar

Augusto Destrero baxeico

View GitHub Profile
@baxeico
baxeico / locked_command.py
Created Apr 9, 2021
Django command with lockfile
View locked_command.py
import logging
from os.path import exists
from os import remove, getpid
from django.core.management.base import BaseCommand
logger = logging.getLogger(__name__)
class LockedCommand(BaseCommand):
"""
@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 Aug 6, 2020
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()