Skip to content

Instantly share code, notes, and snippets.

View baxeico's full-sized avatar

Augusto Destrero baxeico

View GitHub Profile
@baxeico
baxeico / allow_cors_mixin.py
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
baxeico / smtp_server.py
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
baxeico / excel_models.py
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 django.core.management.base import BaseCommand, CommandError
from django.apps import apps
from xlsxwriter.workbook import Workbook
logger = logging.getLogger(__name__)
@baxeico
baxeico / locked_command.py
Created April 9, 2021 14:50
Django command with lockfile
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 / dumpnested.py
Created April 8, 2024 20:08 — forked from alexhayes/dumpnested.py
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 django.core.management import BaseCommand
from django.db import router