Skip to content

Instantly share code, notes, and snippets.

View mattmcc's full-sized avatar

Matt McClanahan mattmcc

View GitHub Profile
@mattmcc
mattmcc / pdfresponse.py
Last active October 10, 2016 09:13
Django PDFResponse using weasyprint
from django.template.response import TemplateResponse
from django.views.generic import DetailView
from weasyprint import HTML
class PDFResponse(TemplateResponse):
@property
def rendered_content(self):
template = self.resolve_template(self.template_name)
context = self.resolve_context(self.context_data)
{% load sniplates %}
{% load_widgets core="widgets.html" _soft=True %}
{% block content %}
{% include "included_widget.html" %}
{% endblock %}
class ChoiceList(list):
def __init__(self, *args, **kwargs):
self.choices = kwargs.pop('choices')
super(ChoiceList, self).__init__(*args, **kwargs)
def items(self):
return [(k, v) for k, v in self.choices if k in self]
class ChoiceArrayField(ArrayField):
def __init__(self, *args, **kwargs):
@mattmcc
mattmcc / models.py
Created March 27, 2013 00:24
Quick & dirty "read-only" model for using SQL views
class ViewManager(models.Manager):
def bulk_create(self, *args, **kwargs):
raise NotImplementedError
def create(self, *args, **kwargs):
raise NotImplementedError
def get_or_create(self, *args, **kwargs):
raise NotImplementedError
@mattmcc
mattmcc / datastructures.py
Created January 1, 2013 01:49
Preliminary ducktyping of ManyToManyField using django-orm-extensions' ArrayField and storing a list of PKs.
class QueryList(list):
"""
A list of primary keys for a specified model which can act as a QuerySet
Takes a ``model`` keyword argument indicating which model the PKs are
associated with.
"""
def __init__(self, *args, **kwargs):
self.model = kwargs.pop('model')
super(QueryList, self).__init__(*args, **kwargs)