Skip to content

Instantly share code, notes, and snippets.

View mattkahl's full-sized avatar

Matt Kahl mattkahl

  • Waymark
  • Detroit, MI
View GitHub Profile
@mattkahl
mattkahl / admin.py
Last active March 1, 2022 17:03
An improvement to the Django admin queryset count approximation workaround proposed by @adamchainz at http://adamj.eu/tech/2014/07/16/extending-djangos-queryset-to-return-approximate-counts/. Note: This is intended for use with Postgres.
class BaseModelAdmin(Admin):
def queryset(self, request):
qs = super(BaseModelAdmin, self).queryset(request)
# Use the approximate queryset to avoid any non-performant
# 'COUNT(*)' queries
# Mixin the ApproximateCountQuerySetMixin to add the approximate count
# to any custom querysets
class AugmentedApproximateCountQueryset(ApproximateCountQuerySetMixin, qs.__class__):
pass
@mattkahl
mattkahl / twig.async.js
Last active March 16, 2017 08:23
An asynchronous implementation of twigjs that returns a Promise on instantiation if the template is asynchronous. Works just like twig(), but when instantiated, twigAsync returns a Promise. The twig template is passed as the only parameter when resolving the Deferred object.
/**
* twig.async.js
*
* An asynchronous implementation of twigjs that returns a Promise on instantiation if the template is asynchronous.
* Works just like twig(), but when instantiated, twigAsync returns a Promise. The twig template
* is passed as the only parameter when resolving the Deferred object.
*
* e.g.
* tpl = twigAsync({href:'/templates/profile/profile-welcomeNewDesign.twig'});
* $.when(tpl).done(function(twigTemplate){
@mattkahl
mattkahl / twig.spaceless.js
Last active December 13, 2015 19:29
Add Twig's spaceless token to twig.js (e.g. `{% spaceless %} ... {% endspaceless %}`)
Twig.extend(function(Twig) {
// Add the {% spaceless %} token
Twig.logic.extend({
type: 'spaceless',
regex: /^spaceless$/,
next: [
'endspaceless'
],
open: true,