Skip to content

Instantly share code, notes, and snippets.

View vstoykov's full-sized avatar
🇧🇬

Venelin Stoykov vstoykov

🇧🇬
View GitHub Profile
@vstoykov
vstoykov / force_default_language_middleware.py
Last active March 25, 2025 14:26
Force Django to use settings.LANGUAGE_CODE for default language instead of request.META['HTTP_ACCEPT_LANGUAGE']
try:
from django.utils.deprecation import MiddlewareMixin
except ImportError:
MiddlewareMixin = object
class ForceDefaultLanguageMiddleware(MiddlewareMixin):
"""
Ignore Accept-Language HTTP headers
@vstoykov
vstoykov / SQLPrintingMiddleware.py
Last active November 15, 2024 22:04
Django Middleware to print sql queries in debug console
"""
Originaly code was taken from http://djangosnippets.org/snippets/290/
But I was made some improvements like:
- print URL from what queries was
- don't show queries from static URLs (MEDIA_URL and STATIC_URL, also for /favicon.ico).
- If DEBUG is False tell to django to not use this middleware
- Remove guessing of terminal width (This breaks the rendered SQL)
- Port to Python 3 and newer versions of Django
"""
from django.conf import settings
@vstoykov
vstoykov / dictobject.py
Last active August 30, 2018 14:11
Simple dict sucblass for making it possible to access keys as attributes
from collections import defaultdict
class DictObject(dict):
"""
Simple dict subclass for making it possible to to access keys as attributes
This class can be used as object_hook when deserializing JSON for easy
access to attributes of the JSON objects.
@vstoykov
vstoykov / pdftk
Last active October 19, 2017 13:40
Run pdftk in Docker
#!/usr/bin/env python
import os
import sys
import subprocess
PDFTK_DOCKER_IMAGE = 'agileek/pdftk'
def main(*argv):
file_args = {'stamp', 'output'}
@vstoykov
vstoykov / CSVParser.py
Last active August 9, 2017 11:41
Parsing CSV files
"""
Easy parsing of CSV files.
Every row is a named tuple with column name defined by the header (first row).
By default header is not returned (only rows containing the data)
"""
import csv
from collections import namedtuple
@vstoykov
vstoykov / pfx2pem.sh
Last active July 25, 2017 13:29
Convert pfx files to key and pem files suitable for Nginx
#!/bin/bash
# Usage:
# ./pfx2pem.sh /path/to/domain.pfx
#
# Creates domain.pem and domain.key in the current directory
#
# Based on https://gist.github.com/ericharth/8334664#gistcomment-1942267
pfxpath="$1"
if [ ! -f "$pfxpath" ];
// Place your settings in this file to overwrite the default settings
{
//-------- Editor configuration --------
// Controls the font family.
"editor.fontFamily": "Ubuntu Mono",
// Controls the font size.
"editor.fontSize": 14,
@vstoykov
vstoykov / maxmind_update.sh
Created March 2, 2012 09:56
Download MaxMind free database and place it in default location for pygeoip
#!/bin/bash
if [ `whoami` != 'root' ]; then
echo "You must to be root"
exit
fi
DOWNLOAD_URL="http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz"
TEMPFILE="/tmp/GeoIPCity.dat"
TEMPFILEGZ="$TEMPFILE.gz"
GEOIP_DIR="/usr/local/share/GeoIP"
try:
import json
except ImportError:
from django.utils import simplejson as json
from django.http import HttpResponse
from django.conf import settings
from django.core.serializers.json import DjangoJSONEncoder
JSON_CONTENT_TYPE = 'application/json; charset=%s' % (settings.DEFAULT_CHARSET, )
@vstoykov
vstoykov / seo_parser.py
Last active December 19, 2015 22:39
Simple parser to get all meta tags
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Fetch SEO data from given URL
"""
from HTMLParser import HTMLParser
from urllib2 import build_opener