Skip to content

Instantly share code, notes, and snippets.

@robhudson
robhudson / gist:3848832
Last active December 28, 2024 16:13
Quick way to set CORS headers on django-tastypie resources
class CORSResource(object):
"""
Adds CORS headers to resources that subclass this.
"""
def create_response(self, *args, **kwargs):
response = super(CORSResource, self).create_response(*args, **kwargs)
response['Access-Control-Allow-Origin'] = '*'
response['Access-Control-Allow-Headers'] = 'Content-Type'
return response
@robhudson
robhudson / lsCache.js
Last active December 14, 2015 16:09
caching and localStorage
var lsCache = function() {
var TIMEOUT_DEFAULT = 60;
var self = {
set: function(key, val, timeout) {
var timeout = parseInt(timeout, 10) || TIMEOUT_DEFAULT;
var now = Math.round(new Date().getTime() / 1000);
localStorage.setItem(key, val);
localStorage.setItem(key + '.timeout', now * timeout);
},
@robhudson
robhudson / gist:5296851
Created April 2, 2013 22:38
.git/hooks/pre-commit and flake8
#!/usr/bin/env python
import sys
from flake8.run import git_hook
ignore = [
'E121',
'E123',
'E124',
'E125',
'E126',
@robhudson
robhudson / middleware.py
Created September 25, 2013 16:12
CNAME handling in Django middleware example
import threading
from django.conf import settings
from django.http import HttpResponseRedirect
# We used our own model to associate users with sites, but could use django.contrib.sites here.
from .models import Network
# List of allowed URLs if user isn't in the network
ALLOWED = [
u'/accounts/register/',
@robhudson
robhudson / top_books.py
Created October 14, 2013 22:27
An example of doing a "Top 10" query in elasticsearch using a term_stats facet.
import datetime
import random
from collections import defaultdict
from elasticsearch import Elasticsearch
INDEX = 'test-top-books'
TYPE = 'book-sales'
today = datetime.date.today()
@robhudson
robhudson / feed.py
Created July 1, 2014 22:53
Feed experiment
import datetime
from elasticsearch import Elasticsearch
INDEX = 'feed'
today = datetime.date.today()
es = Elasticsearch()
mysql> select a.app_slug, ai.value from addons_installs as ai join addons as a on a.id=ai.addon_id where region=0 order by value desc limit 20;
+---------------------+--------+
| app_slug | value |
+---------------------+--------+
| connecta2 | 254311 |
| openwapp | 184414 |
| facebook-1 | 153012 |
| loqui | 115818 |
| whatsapp-firefox-os | 70816 |
| line | 43606 |
(env)vagrant@precise32:~/src$ pip install --no-deps -U -t vendor/src/elasticsearch-dsl-py -e git+https://github.com/elasticsearch/elasticsearch-dsl-py.git@ea37a0dcefb0f3738a9796a5095f3d93796df259#egg=elasticsearch-dsl-py
You are using pip version 6.0.6, however version 6.0.7 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Obtaining elasticsearch-dsl-py from git+https://github.com/elasticsearch/elasticsearch-dsl-py.git@ea37a0dcefb0f3738a9796a5095f3d93796df259#egg=elasticsearch-dsl-py
Updating /home/vagrant/env/src/elasticsearch-dsl-py clone (to ea37a0dcefb0f3738a9796a5095f3d93796df259)
Could not find a tag or branch 'ea37a0dcefb0f3738a9796a5095f3d93796df259', assuming commit.
/home/vagrant/env/lib/python2.6/site-packages/setuptools/dist.py:284: UserWarning: The version specified requires normalization, consider using '0.0.4.dev0' instead of '0.0.4.dev'.
self.metadata.version,
/home/vagrant/env/lib/python2.6/site-packages/pkg_resources/__init__.py:2263:
{
"highlight": {
"fields": {
"content": {},
"summary": {}
},
"order": "score"
},
"facets": {
"marketplace": {
Traceback (most recent call last):
File "/home/vagrant/src/kuma/search/tests/test_views.py", line 147, in test_tokenize_camelcase_titles
response = self.client.get('/en-US/search?q=' + q)
File "/home/vagrant/src/vendor/src/django/django/test/client.py", line 439, in get
response = super(Client, self).get(path, data=data, **extra)
File "/home/vagrant/src/vendor/src/django/django/test/client.py", line 244, in get
return self.request(**r)
File "/home/vagrant/src/vendor/src/django/django/test/client.py", line 381, in request
response = self.handler(environ)
File "/home/vagrant/src/vendor/src/django/django/test/client.py", line 84, in __call__