Skip to content

Instantly share code, notes, and snippets.

### Keybase proof
I hereby claim:
* I am jneight on github.
* I am petrillo (https://keybase.io/petrillo) on keybase.
* I have a public key ASCCdy_7casBx81D9nFTjg5T88GU6JwGSQAQLLEarSEWkAo
To claim this, I am signing this object:
@jneight
jneight / check-vpn.py
Last active August 23, 2019 10:06
check ipsec VPN status and get it up if it is down
#!/usr/bin/python3
import re
import os
import subprocess
import logging
from systemd.journal import JournalHandler
logger = logging.getLogger('check_vpn')
@jneight
jneight / check_vpn.py
Created October 21, 2017 15:35
Check ipsec VPN status
import re
import os
import subprocess
def get_vpn_names(path):
with open(path, 'r') as f:
return re.findall(r'conn ([\w]+)', read)
@jneight
jneight / row_count_all_tables
Created August 12, 2013 08:54
Get the rows in each table.
SELECT
nspname AS schemaname,relname,reltuples
FROM pg_class C
LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
WHERE
nspname NOT IN ('pg_catalog', 'information_schema') AND
relkind='r'
ORDER BY reltuples DESC;
@jneight
jneight / parse_uri.py
Last active December 20, 2015 03:09
Given a uri with the form /api/v1/class/1/ get the view and args it will call from registered urls
from django.core.urlresolvers import (
reverse, get_script_prefix, resolve, Resolver404)
prefix = get_script_prefix()
chomped_uri = uri
if prefix and chomped_uri.startswith(prefix):
chomped_uri = chomped_uri[len(prefix) - 1:]
# resolve will split cleaned uri and get PK
view, args, extra_venue = resolve(chomped_uri)
@jneight
jneight / debug_pickle.py
Created July 18, 2013 09:58
Debug unpickled errors.
# from http://stackoverflow.com/questions/569754/how-to-tell-for-which-object-attribute-pickle-fails
"""
Show which fields cannot be pickled
"""
import pickle
def get_pickling_errors(obj,seen=None):
if seen is None:
seen = []
@jneight
jneight / responseloggingmiddleware.py
Last active December 19, 2015 21:58
For tastypie. Middleware to logs all requests. Store the params used in POST and PUT applying a sensitive word substitution.
# coding=utf-8
import datetime
import logging
logger = logging.getLogger('djangoapp')
from django.views.debug import CLEANSED_SUBSTITUTE
from django.conf import settings
from tastypie.utils.mime import determine_format
@jneight
jneight / path_rename.py
Created July 2, 2013 08:13
Get an uploaded image and rename it to model PK
"""
USE:
photo = models.ImageField(
_(u'Rating photo'), upload_to=path_and_rename('ratings/pictures'),
blank=True)
"""
import os
from uuid import uuid4
@jneight
jneight / fields.py
Last active December 18, 2015 01:29
Avoid doing extra database queries when resource uri of related field is built using models PK.
# coding=utf-8
from tastypie.bundle import Bundle
from tastypie import fields
from tastypie.exceptions import ApiFieldError
class OptimizedToOneField(fields.ToOneField):
def dehydrate(self, bundle, **kwargs):
"""
@jneight
jneight / crossdomain_middleware.py
Created May 27, 2013 14:55
Just the same middleware used in lot of sites to allow crossdomain request in django but with credentials allowed
# XHR CrossDomain Middleware by robtotheb
# https://gist.github.com/1164697
from django import http
XS_SHARING_ALLOWED_ORIGINS = '*'
XS_SHARING_ALLOWED_METHODS = 'POST, GET, OPTIONS, PUT, DELETE'
XS_SHARING_ALLOWED_HEADERS = 'Accept, Accept-Charset, Accept-Encoding, Accept-Language, Connection, ' \
'Content-Type, Authorization, Cache-Control, Referer, User-Agent, Origin'
XS_SHARING_ALLOWED_CREDENTIALS = 'true'