Skip to content

Instantly share code, notes, and snippets.

View luzfcb's full-sized avatar

Fábio C. Barrionuevo da Luz luzfcb

View GitHub Profile
@luzfcb
luzfcb / drf_utils.py
Created April 29, 2022 13:26 — forked from twidi/drf_utils.py
Make Django Rest Framework correctly handle Django ValidationError raised in the save method of a model
"""
Sometimes in your Django model you want to raise a ``ValidationError`` in the ``save`` method, for
some reason.
This exception is not managed by Django Rest Framework because it occurs after its validation
process. So at the end, you'll have a 500.
Correcting this is as simple as overriding the exception handler, by converting the Django
``ValidationError`` to a DRF one.
"""
from django.core.exceptions import ValidationError as DjangoValidationError
@luzfcb
luzfcb / fields.py
Last active September 29, 2021 18:08 — forked from ramsrib/fields.py
A form field to handle validation of image + svg in Django 3
from io import BytesIO
from defusedxml import ElementTree
from django.core.exceptions import ValidationError
from django.core.validators import (
FileExtensionValidator,
get_available_image_extensions,
)
from django.forms import ImageField as DjangoImageField
from PIL import Image
@luzfcb
luzfcb / celery_django_redis_ratelimit.py
Created June 19, 2021 13:27 — forked from Vigrond/celery_django_redis_ratelimit.py
Celery / Django / Redis Rate Limits done "as expected" - Simple SMTP Example
# Rate limiting with Celery + Django + Redis
# Multiple Fixed Windows Algorithm inspired by Figma https://www.figma.com/blog/an-alternative-approach-to-rate-limiting/
# and Celery's sometimes ambiguous, vague, and one-paragraph documentation
#
# Celery's Task is subclassed and the is_rate_okay function is added
# celery.py or however your App is implemented in Django
import os
import math
@luzfcb
luzfcb / callpraphmiddleware.py
Created March 1, 2021 21:48 — forked from hirokiky/callpraphmiddleware.py
Django Middleware to show call graph.
from pycallgraph import PyCallGraph
from pycallgraph import Config
from pycallgraph import GlobbingFilter
from pycallgraph.output import GraphvizOutput
class ProfilerMiddleware(object):
includes = []
def can(self, request):
@luzfcb
luzfcb / black_selection.sh
Last active March 9, 2020 15:45 — forked from jamesbraza/black_selection.sh
Black on selection
#!/usr/bin/env bash
# Original Source: https://blog.godatadriven.com/black-formatting-selection
# This is a modified version of
# https://gist.github.com/JDusub83/ba2848d71644f4c3496def4c1a1d9fc8
#
# changes:
# - Added debugging prints, permissioning/cleanup, and also a bugfix for
# selecting whole file.
# - Now the original file can only be modified from black to run without errors.
# - Added support to black pyproject.toml - https://black.readthedocs.io/en/stable/pyproject_toml.html
@luzfcb
luzfcb / CalibreServerOnLinux.md
Created February 23, 2020 22:54 — forked from plembo/CalibreServerOnLinux.md
Calibre Server on Linux

Calibre Server on Linux

Introduction

Calibre is a powerful cross-platform, open source, ebook manager and editing platform. Its calibre-server component can be used to publish an e-book library on a local network. While you can launch calibre-server as a desktop application, it can also be run as a daemon on a headless Linux server.

This tutorial on setting up calibre-server using Ubuntu 14.04 is very good, but dated.

@luzfcb
luzfcb / models.py
Last active May 22, 2018 00:04 — forked from jacobian/models.py
An example of using many-to-many "through" to augment m2m relationships. See http://www.quora.com/How-do-you-query-with-a-condition-on-a-ManyToMany-model-in-Django for context.
from django.db import models
class Person(models.Model):
name = models.CharField(max_length=200)
groups = models.ManyToManyField('Group', through='GroupMember', through_fields=('person', 'group'), related_name='people')
class Meta:
ordering = ['name']
def __unicode__(self):
@luzfcb
luzfcb / print.js
Created March 17, 2018 16:05 — forked from rg3915/print.js
Print on HTML with JS
<script>
function printDiv(divName) {
var printContents = document.getElementById(divName).innerHTML;
var originalContents = document.body.innerHTML;
document.body.innerHTML = printContents;
window.print();
document.body.innerHTML = originalContents;
import os
import pandas
from dateutil import parser
from django.conf import settings
#from iptu.core.models import Iptu
from core.models import Iptu
import os
from dateutil import parser
import pandas
from django.conf import settings
from iptu.core.models import Iptu
def gera_db_iptu():