Skip to content

Instantly share code, notes, and snippets.

View turtlemonvh's full-sized avatar

Timothy turtlemonvh

View GitHub Profile
@turtlemonvh
turtlemonvh / Run Django Tastypie Endpoint Tests
Last active December 19, 2015 14:09
Embracing duck typing
import api
# Create test client
cl = Client()
# Hit all endpoints defined in api file
for _, obj in inspect.getmembers(api):
try:
test_url = '/api/v1/%s/' % obj.Meta.resource_name
response = cl.get(test_url, {'format': 'json'})
@turtlemonvh
turtlemonvh / Django nose settings file
Created July 25, 2013 20:39
A nose specific settings file. Specify this for running tests using the nose test framework. Allows you to run tests using the normal Django test library the usual way. Requires the `nose` and `django-nose` python packages. This version also creates xml coverage and test reports in cobertura and xunit format, respectively.
"""
Specify this settings file to run nose tests format instead of the standard Django test runner.
Useful for getting coverage reports and for getting xunit compaible reports for use with a CI server.
To run all tests:
> python manage.py test --settings=settings_folder.nose_settings apps
To run tests only for a specific app:
> python manage.py test --settings=settings_folder.nose_settings apps.myapp.tests
@turtlemonvh
turtlemonvh / JS Scoping
Last active December 20, 2015 18:49
Javascript variable scoping; before and after
for (var i; i<10; i++) {
var x = $("$a-thing");
// ...more work here
}
@turtlemonvh
turtlemonvh / For loop scope test
Created August 7, 2013 21:00
For loop scope test python
for i in range(10):
x = 4
print x
@turtlemonvh
turtlemonvh / For loop scope test in javascript
Created August 7, 2013 21:08
For loop scope test in javascript
for(var i=1; i<10; i++){var x = 3;};
x
@turtlemonvh
turtlemonvh / fix_bad_mysql_restart.py
Created February 21, 2014 17:21
Register this as a crontab to run every hour to check if mysql had a hard time restarting. If so, it may be because this was brought down quickly. Use `sudo crontab -e` when creating the crontab because it needs to run as root to be able to move the pid files and restart the service.
#!/usr/bin/env python
import os
import subprocess
child = subprocess.Popen('service mysqld status', shell=True, stdout=subprocess.PIPE)
# Capture output string
outstr = ''
while True:
@turtlemonvh
turtlemonvh / main.js
Last active January 3, 2021 16:37
Angular Messaging
var MyApp = angular.module('MyApp');
MyApp.factory('msgBus', ['$rootScope', function($rootScope) {
var msgBus = {};
msgBus.emitMsg = function(msg, data) {
data = data || {};
$rootScope.$emit(msg, data);
};
msgBus.onMsg = function(msg, func, scope) {
var unbind = $rootScope.$on(msg, func);
if (scope) {
@turtlemonvh
turtlemonvh / myapp.conf
Created June 25, 2014 20:05
Apache conf for multiple djangos at subdomains
# Some tips:
# - tips on multiple versions: http://stackoverflow.com/questions/1553165/multiple-django-sites-with-apache-mod-wsgi
# - using the wsgi daemon mode allows better performance with multi-threading
WSGISocketPrefix /var/run/wsgi
Alias /err/ "/var/www/error/"
ErrorDocument 404 /err/HTTP_NOT_FOUND.html.var
# The default virtual host
@turtlemonvh
turtlemonvh / myapp.conf
Created July 1, 2014 03:51
Apache conf for multiple djangos at different ports, with the main version served over https
WSGISocketPrefix /var/run/wsgi
WSGIDaemonProcess site-1 user=myapp group=myapp threads=20
WSGIDaemonProcess site-alt user=myapp group=myapp threads=5
LoadModule ssl_module modules/mod_ssl.so
# Get apache listening on another port
# The "Listen 80" directive is in httpd.conf
Listen 8080
@turtlemonvh
turtlemonvh / multiPort.py
Created July 30, 2014 13:16
Middleware for running django on multiple ports
from django.conf import settings
class MultiPortMiddleware(object):
"""
Middleware changes the SESSION_COOKIE_NAME to use the current port in the name
"""
def process_request(self, request):
settings.SESSION_COOKIE_NAME = 'sessionid' + request.META['SERVER_PORT']