Skip to content

Instantly share code, notes, and snippets.

View AndrewIngram's full-sized avatar
🦊
Being foxy

Andy Ingram AndrewIngram

🦊
Being foxy
View GitHub Profile
var bootmaps = bootmaps || {};
bootmaps.InfoWindow = function(opts) {
opts = opts || {};
google.maps.OverlayView.apply(this, arguments);
this.map_ = opts.map || "";
this.content_ = opts.content || "";
this.disableAutoPan_ = opts.disableAutoPan || false;
this.title_ = opts.title || "";
this.position_ = opts.position || new google.maps.LatLng(0, 0);
@AndrewIngram
AndrewIngram / gist:4595457
Last active December 11, 2015 11:39
Possible EditInPlaceMixin for Django's class-based views. Useful for AJAX requests where on successful POST you render a detail template rather than doing a redirect. Unsuccessful POSTs return the form template as normal but with a 400 response code. Should work with Django's existing FormView and ModelFormView, as well as all the formset-relate…
from django.core.exceptions import ImproperlyConfigured
class EditInPlaceMixin(object):
detail_template_name = None
def get_detail_template_names(self):
"""
Returns a list of template names to be used for the request. Must return
a list. May not be called if render_to_detail_response is overridden.
@AndrewIngram
AndrewIngram / gist:5012426
Created February 22, 2013 10:30
Tastypie Collection for Backbone.js
var TastypieCollection = Backbone.Collection.extend({
initialize: function(options) {
this.paginator = new models.Paginator({}, this);
},
parse: function(response) {
this.paginator.set(response.meta);
return response.objects || response;
},
@AndrewIngram
AndrewIngram / gist:5362941
Last active December 16, 2015 02:29
prefetch_related makes me sad
In : from onefinestay.geo.models import Place
In : p = Place.objects.prefetch_related('categories').get(id=1460) # Only prefetching categories
In : p.categories.all()
Out: [<Category: Miscellaneous>, <Category: For kids>]
In : p.regions.all()
Out: [<Region: Belgravia>, <Region: Bloomsbury>, <Region: Clerkenwell>, <Region: City of London>]
In : p.regions.clear()
#!/usr/bin/env python
from scapy.all import *
ap_list = []
def PacketHandler(pkt) :
if pkt.haslayer(Dot11) and pkt.type == 0 and pkt.subtype == 8 and pkt.addr2 not in ap_list:
ap_list.append(pkt.addr2)
#!/usr/bin/env python
import socket
rawSocket = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(0x0003))
rawSocket.bind(("mon0", 0x0003))
ap_list = set()
while True:
pkt = rawSocket.recvfrom(2048)[0]
if pkt[26] == "\x80":
if pkt[36:42] not in ap_list and ord(pkt[63]) > 0:
ap_list.add(pkt[36:42])
@AndrewIngram
AndrewIngram / gist:5468285
Created April 26, 2013 15:50
Almost everything about this is wrong
Javascript gives us an option to do things a bit differently. Rather than wait around for a function to finish by returning a value, we can use callbacks to do it asynchronously. This is useful for things that take a while to finish, like making an AJAX request, because we aren’t holding up the browser. We can keep on doing other things while waiting for the callback to be called. In fact, very often we are required (or, rather, strongly encouraged) to do things asynchronously in Javascript.
@AndrewIngram
AndrewIngram / gist:5636041
Last active December 17, 2015 16:00
Annotations, aggregates and prefetching
Two problems:
1) Individual annotations can't be filtered
2) `prefetch_related` doesn't allow for filtered relations
The `annotate()` method is currently used for calculating
aggregates for each result in a queryset, but it could be
used more generally as an API for attaching the result of
some calculation to each model in a queryset - whether it
be a single value (an aggregate calculation) or multiple
@AndrewIngram
AndrewIngram / storages.py
Created June 18, 2013 09:22
Hybrid Storage Backend for Django
from django.conf import settings
from django.core.files.storage import Storage
from django.utils import importlib
def load_class(class_string):
class_module, class_name = class_string.rsplit('.', 1)
class_module = importlib.import_module(class_module)
return getattr(class_module, class_name)
@AndrewIngram
AndrewIngram / gist:6019684
Last active December 19, 2015 21:19
Migrate from old style tree with parent id to django-mptt or Treebeard's nested sets
from itertools import count
def migrate_children(tree_id, node, counter):
for index, child in enumerate(Region.objects.filter(parent=node).order_by('name')):
child.tree_id = tree_id
child.lft = counter.next()
migrate_children(tree_id, child, counter)
child.rgt = counter.next()
child.save()