Skip to content

Instantly share code, notes, and snippets.

@magopian
magopian / tests.py
Created November 16, 2012 10:55
Unit test to validate django templates
class TemplatesTest(TestCase):
def test_templates(self):
"""Templates can compile properly and there's no mismatched tags"""
# get app template dirs
template_dirs = []
apps = [app for app in settings.INSTALLED_APPS
if app.startswith('rh2')]
for app in apps:
mod = import_module(app)
@magopian
magopian / gist:4078269
Created November 15, 2012 11:53
Reuse the Django admin's javascript (example with prepopulated_fields.js)
<script src="{% static "admin/js/jquery.js" %}"></script>
<script src="{% static "admin/js/jquery.init.js" %}"></script>
<script src="{% static "admin/js/urlify.js" %}"></script>
<script src="{% static "admin/js/prepopulate.js" %}"></script>
<script>
// adaptation from django/contrib/admin/templates/admin/prepopulated_fields_js.html
(function($) {
var field = {
id: '#{{ form.###DESTINATION FIELD NAME###.auto_id }}',
dependency_ids: ['#{{ form.###SOURCE FIELD NAME###.auto_id }}'],
@magopian
magopian / Makefile
Created November 15, 2012 11:02
Sample Makefile for Django (use with project templates)
.PHONY: all help translate test clean update compass collect rebuild
SETTINGS={{ project_name }}.settings
TEST_SETTINGS={{ project_name }}.test_settings
# target: all - Default target. Does nothing.
all:
@echo "Hello $(LOGNAME), nothing to do by default"
@echo "Try 'make help'"
@magopian
magopian / pre-commit
Created November 16, 2012 10:40
git pre-commit hook for sphinx documentation
#!/bin/sh
#
# This pre-commit hook tests that the documentation builds correctly.
# It can be disabled by using the "-n" option with "git commit".
#
# See http://git-scm.com/book/en/Customizing-Git-Git-Hooks for details.
#
# This pre-commit hook requires that you have the "###PROJECT###" project on your python
# path, in order for the DJANGO_SETTINGS_MODULE=###PROJECT###.settings to work.
# This is needed to build the apidoc.
@magopian
magopian / gist:297d20e95b49daf3ad934e36b1855e82
Created November 28, 2018 16:56
elm decoder very helpful to debug decoders used in event handlers (thanks @JoelQ on the elm slack)
module Main exposing (main)
import Html exposing (Html, text)
import Json.Decode as Decode exposing (Decoder)
loggingDecoder : Decoder a -> Decoder a
loggingDecoder realDecoder =
Decode.value
|> Decode.andThen
@magopian
magopian / fix_permissions.py
Created November 19, 2013 11:00
Django admin command to "fix permissions" (create them properly for proxy models). This is needed because of the following bug in Django (not fixed as of 1.6): https://code.djangoproject.com/ticket/11154
# -*- coding: utf-8 -*-
"""Add permissions for proxy model.
This is needed because of the bug https://code.djangoproject.com/ticket/11154
in Django (as of 1.6, it's not fixed).
When a permission is created for a proxy model, it actually creates if for it's
base model app_label (eg: for "article" instead of "about", for the About proxy
model).
@magopian
magopian / admin_list_editable_autosubmit.js
Last active September 10, 2021 15:51
Small js script that automatically submits the changelist form on field changes. This is convenient when used with https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_editable, and avoids having to remember to submit the form when done (the form on the changelist page doesn't look like a form after all, …
/*
* Only useful in changelist pages when the ModelAdmin displayed has
* "list_editable" (https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_editable)
* configured.
*
* When one of the input/select/textarea value is changed, automatically submit
* the form using ajax.
*
* Only form fields relevant to the "list_editable" will trigger a form submit.
*
@magopian
magopian / aiohttp + aiopg.py
Last active October 4, 2019 08:48
experiments with python asyncio (all three are roughly the same speed as the werkzeug+pg version, as it's CPU bound, not IO bound: asyncio doesn't help in such cases)
"""start with:
gunicorn utilery.views_aiohttp_aiopg:app -b 0.0.0.0:3579 -w 4 -k aiohttp.worker.GunicornWebWorker"""
import asyncio
import json
import math
import time
import psycopg2
import psycopg2.extras
@magopian
magopian / gist:6128849
Last active August 27, 2019 04:32
Local git mirror of github used by private Jenkins
So I've installed a dead simple backup, in /home/USER/git_backup/:
git clone --mirror git@github.com:USER/PROJECT.git
update.sh (launched every 5 minutes by cron):
#/bin/sh
cd /home/USER/git_backup/PROJECT.git
LINES=`git remote update 2>&1 | wc -l`
@magopian
magopian / conftest.py
Last active November 1, 2018 17:10
Some pytest(-django) fixtures that I find useful. There's also an "app" fixture to take advantage of django-webtest when used with py.test.
# -*- coding: utf-8 -*-
from __future__ import unicode_literals, absolute_import, division
import pytest
from django_webtest import DjangoTestApp, WebTestMixin
class Stub(object):
"""Stub methods, keep track of calls."""