Skip to content

Instantly share code, notes, and snippets.

@carymrobbins
carymrobbins / patch_django_1_4_osx_libgeos.sh
Last active December 27, 2015 18:49
Patch Django 1.4 to support libgeos on OSX. Source this while in your virtualenv.
pip install git+https://github.com/carymrobbins/django.git@fix_libgeos_for_mac#egg=django --upgrade
@carymrobbins
carymrobbins / activerecord_pg_upsert.rb
Last active December 29, 2015 17:09
Extend ActiveRecord::Base with an efficient upsert class method for PostgreSQL. This will perform updates, inserts, and/or deletes with batch data in a single query.
class ActiveRecord::Base
# Performs an insert, update, and/or delete in a single SQL query. Note that
# this will only work with PostgreSQL.
# Params:
# - data: Array of Hash to be upserted.
# - translation: Hash to map keys in data elements to fields in the database.
# - constants: Hash of constants to merge into every data row.
# - field_names: Array of field names to use from data for the upsert. These
# should match your database, not your upsert data. By default
# this will be determined by the translated keys of your first
# found this from Armin R. on Twitter, what a beautiful gem ;)
import ctypes
from types import DictProxyType, MethodType
def main():
import datetime
class _(monkey(int)):
import Data.List
type Dict = [(String, String)]
views :: Dict
views = [ ("foo", "select bar")
, ("bar", "select shoe")
, ("shoe", "select none")
, ("cat", "select foo")
, ("dog", "select mouse")
@carymrobbins
carymrobbins / cabal-freeze.hs
Last active December 31, 2015 09:29
Output dependency versions for cabal sandbox.
#!/usr/bin/env runghc
import Data.Tuple
import System.Process
main = do
out <- getBash "ls .cabal-sandbox/lib/*"
let packages = lines out
let packageVersions = map (tjoin "==" . splitVersion) packages
putStrLn . unlines $ packageVersions
@carymrobbins
carymrobbins / redmine-workflow-auto-check.js
Last active January 3, 2016 07:09
Redmine workflow - automatically check the boxes!
// Check box numbers, can be found in input class.
// e.g. <input class="new-status-21" />
[21,22,41,55,56,42,54].each(function(n) {
$$('.new-status-' + n).each(function(el) {
el.checked = true;
});
});
@carymrobbins
carymrobbins / django-javascript-variable-interpolation.py
Last active June 29, 2022 10:50
Inject JavaScript variables into a Django {% url %} template tag.
from django.template import Context, Template
print Template("""<script type="text/javascript">
var foo = 1,
bar = 2,
url = '{% url view_name obj "'+foo+'" "'+bar+'" %}';
</script>""").render(Context(dict(obj='something')))
# Output:
<script type="text/javascript">
@carymrobbins
carymrobbins / raw_as_qs.py
Last active March 25, 2022 12:21
Django - convert RawQuerySet to QuerySet
from django.db import connection, models
class MyManager(Manager):
def raw_as_qs(self, raw_query, params=()):
"""Execute a raw query and return a QuerySet. The first column in the
result set must be the id field for the model.
:type raw_query: str | unicode
:type params: tuple[T] | dict[str | unicode, T]
:rtype: django.db.models.query.QuerySet
"""
@carymrobbins
carymrobbins / override_model_field.py
Last active August 3, 2022 12:02
Override a model field in Django!
from django.db.models.fields.related import ReverseSingleRelatedObjectDescriptor
def override_model_field(model, name, column, field):
"""Force override a field in a Django Model.
Usage: override_model_field(
MyModel, models.ForeignKey(OtherModel), 'other', 'other_id')
:type model: django.db.models.base.ModelBase
:type name: basestring
:type column: basestring
:type field: django.db.models.fields.Field
@carymrobbins
carymrobbins / roundup.py
Last active August 29, 2015 13:55
Round up in Python to specified digits.
import math
def roundup(x, digits=0):
n = 10**-digits
return round(math.ceil(x / n) * n, digits)