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
def write_csv_from_queryset(queryset, fields, output=None):
"""
>>> from django.contrib.auth.models import User
>>> qs = User.objects.all()[:5]
>>> write_csv_from_queryset(qs, 'username')
>>> write_csv_from_queryset(qs, ['username', 'is_superuser'])
"""
import csv
import datetime
import csv
from io import TextIOWrapper
from collections import namedtuple
class InvalidCSVFileException(Exception):
pass
class MissingCSVHeadersException(Exception):
import csv
import datetime
import logging
from pathlib import Path
logger = logging.getLogger(__name__)
def write_csv_from_queryset(queryset, fields, output=None):
output_filename = Path(str(output) if isinstance(output, (Path, str)) else f'{queryset.model._meta.model_name}_{datetime.datetime.now():%Y-%m-%d__%H_%M_%S}.csv')
from django.contrib.auth.models import AbstractUser
from django.db.models import CharField, Field, Model
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
class AliasField(Field):
# based on https://djangosnippets.org/snippets/10440/
def __init__(self, alias_from, editable=True):
@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):
.DEFAULT_GOAL := help
define PRINT_HELP_PYSCRIPT
import re, sys
output = []
for line in sys.stdin:
match = re.match(r'^([a-zA-Z_-]+):.*?## (.*)$$', line)
if match:
target, help = match.groups()
brew install imagemagick
convert image.png output.pdf
@luzfcb
luzfcb / wkhtmltopdf_send_email_django.py
Last active July 1, 2020 15:58
I did this example quickly without executing the code. I don't know if it works or if it's minimally correct.
import io
from django.core.mail import EmailMessage
from wkhtmltopdf.views import PDFTemplateView
class MyPDFTemplateView(PDFTemplateView):
def render_to_response(self, context, **response_kwargs):
response_instance = super(MyPDFTemplateView, self).render_to_response(context, **response_kwargs)
pdf_content = io.BytesIO(response_instance.rendered_content)
pdf_content.seek(0)
@luzfcb
luzfcb / bookdown_tinytex_on_docker.log
Created April 14, 2020 15:35
fmtutil [ERROR]: not building lualatex due to missing engine: luahbtex
Step 7/10 : RUN echo -e "\n\nInstalling Bookdown...\n\n" && Rscript -e "install.packages('bookdown', repository='http://mirror.utexas.edu/ctan/')" && echo -e "\n\nInstalling Tinytex...\n\n" && Rscript -e "install.packages('tinytex', repository='http://mirror.utexas.edu/ctan/')" && echo -e "\n\nInstalling Latex packages via Tinytex...\n\n" && Rscript -e "tinytex::install_tinytex(repository='http://mirror.utexas.edu/ctan/', extra_packages=c('luahbtex', 'lm-math', 'unicode-math'))" && echo -e "\n\nReinstalling Latex packages via Tinytex...\n\n" && Rscript -e "tinytex::reinstall_tinytex()"
---> Running in 06a01042e395
-e
Installing Bookdown...
Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
also installing the dependencies ‘glue’, ‘magrittr’, ‘stringi’, ‘digest’, ‘Rcpp’, ‘rlang’, ‘evaluate’, ‘highr’, ‘markdown’, ‘stringr’, ‘yaml’, ‘base64enc’, ‘jsonlite’, ‘mime’, ‘htmltools’, ‘knitr’, ‘rmarkdown’, ‘xfun’, ‘tinytex’
@luzfcb
luzfcb / install_problems.md
Last active April 13, 2020 13:55
bookdown tinytex install errors 2020-04-12

The install command:

RUN Rscript -e "install.packages('bookdown', repository='http://mirror.utexas.edu/ctan/')" \
    && Rscript -e "install.packages('tinytex', repository='http://mirror.utexas.edu/ctan/')" \
    && Rscript -e "tinytex::install_tinytex(repository='http://mirror.utexas.edu/ctan/')" \
    && Rscript -e "tinytex::tlmgr_install(c('lm-math', 'unicode-math'))"