Skip to content

Instantly share code, notes, and snippets.

View thclark's full-sized avatar

Tom Clark thclark

View GitHub Profile
@thclark
thclark / n_plus_one_in_django.py
Last active February 15, 2018 11:20
An example of the N+1 database insertion problem in django
from django.db.models import CharField, Model, OneToOneField, AutoField, EmailField
class User(Model):
id = AutoField(primary_key=True) # The default Id field from django
username = CharField(max_length=30)
profile = OneToOneField(Profile, primary_key=True, related_name='user')
class Profile(Model):
@thclark
thclark / gist:13794cf4ba5cea3c472dcad9f4039262
Created January 11, 2019 08:33
Jijnja2 Block for rendering a table with django-datatables-view
{% block view_scripts %}
<script type="text/javascript">
var MANIFEST_TABLE_URL = '{{ reverse('manifests:manifest-files-table', kwargs={'manifest_id': object.manifest.id if object.manifest else 'none'}) }}';
</script>
<script>
var table2 = $('#manifestfilestable').DataTable({
language: dt_language, // global variable defined in html
@thclark
thclark / views
Created January 11, 2019 08:39
Django view for providing DataTables
from app.models import Manifest, ManifestFileRecord
from django_datatables_view.base_datatable_view import BaseDatatableView
logger = logging.getLogger(__name__)
class ManifestDatatableView(BaseDatatableView):
model = Manifest
columns = ['id', 'name', 'created', 'updated', 'tags', 'automan_params']
@thclark
thclark / LoadingPage.js
Last active February 21, 2019 22:29
Loading spinner, centred on a page, in React. Designed for use with creative-tim material kit pro, but could be adapted.
import classNames from 'classnames'
import PropTypes from 'prop-types'
import React, { Component } from 'react'
import withStyles from '@material-ui/core/styles/withStyles'
import CircularProgress from '@material-ui/core/CircularProgress'
import FullArea from 'components/FullArea'
import GridContainer from 'components/Grid/GridContainer'
import GridItem from 'components/Grid/GridItem'
@thclark
thclark / FullArea.js
Created February 21, 2019 22:34
React HOC that places children in a div the size of the remaining window space. Updates on window resize. More info at https://stackoverflow.com/a/54112855/3556110
import React, { Component } from 'react'
import PropTypes from 'prop-types'
class FullArea extends Component {
constructor(props) {
super(props)
this.state = {
width: 0,
height: 0,
@thclark
thclark / blocks.py
Created May 23, 2019 19:18
Wagtail serializers for streamfield - example of customizing per-block
class HeroBlock(StructBlock):
content = StreamBlock([
('button', StructBlock([
('text', CharBlock(required=False, max_length=80, label='Label')),
('url', URLBlock(required=False, label='URL')),
], label='Call to action', help_text='A "call-to-action" button, like "Sign Up Now!"')),
('video', EmbedBlock(label='Video')),
('quote', StructBlock([
('text', TextBlock()),
@thclark
thclark / dataProvider.js
Created October 18, 2019 08:11
A django dataProvider for react-admin v3.x
import { stringify } from 'query-string'
import { fetchUtils } from 'react-admin'
import Cookies from 'universal-cookie'
import { store } from 'index'
const camelCaseKeys = require('camelcase-keys')
const snakeCaseKeys = require('snakecase-keys')
@thclark
thclark / # osgeo-gdal - 2019-12-04_16-57-56.txt
Created December 4, 2019 17:02
osgeo-gdal (osgeo/osgeo4mac/osgeo-gdal) on macOS 10.14.6 - Homebrew build logs
Homebrew build logs for osgeo/osgeo4mac/osgeo-gdal on macOS 10.14.6
Build date: 2019-12-04 16:57:56
@thclark
thclark / heroku_local.sh
Created December 6, 2019 10:28
Running celery, flower and heroku locally for a django app
#!/usr/bin/env bash
export PYTHONUNBUFFERED=1
export DJANGO_READ_DOT_ENV_FILE=1
export DJANGO_SETTINGS_MODULE=backend.settings.local
heroku local -p 8000
@thclark
thclark / gtest.rb
Last active March 11, 2020 10:00 — forked from Kronuz/gtest.rb
Homebrew Formula for Google Test
# Homebrew Formula for Google Test
# Usage: brew install --HEAD https://gist.githubusercontent.com/Kronuz/96ac10fbd8472eb1e7566d740c4034f8/raw/gtest.rb
require 'formula'
class Gtest < Formula
desc "Google Test"
homepage "https://github.com/google/googletest"
head "git://github.com/google/googletest.git", :using => :git