Skip to content

Instantly share code, notes, and snippets.

View vstoykov's full-sized avatar
🇧🇬

Venelin Stoykov vstoykov

🇧🇬
View GitHub Profile
@vstoykov
vstoykov / egn_checker.py
Created August 10, 2011 15:08
Method for checking validity of bulgarian EGN codes.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# egn_checker.py
#
# Copyright 2011 Venelin Stoykov <venelin@magicbg.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
@vstoykov
vstoykov / dump_database.py
Created September 29, 2011 15:13
Django management command for dumping database
from django.core.management.base import BaseCommand
from django.conf import settings
from optparse import make_option
import os
# default dump commands, you can overwrite these in your settings.
MYSQLDUMP_CMD = getattr(settings, 'MYSQLDUMP_CMD', 'mysqldump --host=%(host)s --port=%(port)s --opt --complete-insert --compact --skip-add-locks -u"%(user)s" -p"%(password)s" %(database)s > %(filename)s')
SQLITE3DUMP_CMD = getattr(settings, 'SQLITE3DUMP_CMD', 'echo ".dump" | sqlite3 %(database)s > %(filename)s')
class Command(BaseCommand):
@vstoykov
vstoykov / django_geany_snippets.conf
Created October 21, 2011 08:36
Add some Python and Django snippets to default Python snippets for Geany
# Geany's snippets configuration file
# use \n or %newline% for a new line (it will be replaced by the used EOL char(s) - LF, CR/LF, CR)
# use \t ot %ws% for an indentation step, if using only spaces for indentation only spaces will be used
# use \s to force whitespace at beginning or end of a value ('key= value' won't work, use 'key=\svalue')
# use %cursor% to define where the cursor should be placed after completion
# use %key% for all keys defined in the [Special] section
# you can define a section for each supported filetype to overwrite default settings, the section
# name must match exactly the internal filetype name, run 'geany --ft-names' for a full list
#
# Additionally, you can use most of the template wildcards like {developer} or {date} in the snippets.
@vstoykov
vstoykov / force_default_language_middleware.py
Last active June 6, 2024 10:51
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 December 2, 2022 07:18
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)
"""
from django.core.exceptions import MiddlewareNotUsed
from django.conf import settings
@vstoykov
vstoykov / StaticServeMiddleware.py
Created November 25, 2011 08:28
Django Middleware to serve media files on developer django server
from django.conf import settings
from django.views.static import serve, Http404
class StaticServeMiddleware(object):
"""
Middleware to serve media files on developer server
You must put this at top of all middlewares for speedups
(Whe dont need sessions, request.user or other stuff)
"""
@vstoykov
vstoykov / croppedthumbnail.py
Created December 1, 2011 14:04
Function that make cropped thumbnail from given Image
def croppedthumbnail(image, width, height, method=None):
'''
Function that make a thumbnail with given size
but crop image to fit in aspect ratio
image must be a instance of PIL.Image
'''
dst_width = int(width)
dst_height = int(height)
src_width, src_height = image.size
@vstoykov
vstoykov / django-site.sh
Last active September 29, 2015 15:17
Run fast CGI server for django project
#!/bin/bash
#################################################################
# Startup script for Django server, that will be placed
# in /usr/local/bin for example. It may be symlinked
# in project dir with name "run" like:
@vstoykov
vstoykov / ActionForObjectAdmin.py
Created January 23, 2012 12:27
Django Admin class that add some urls wich can call predefined action for given object.
from django.contrib import admin
from django.conf.urls.defaults import patterns, url
from django.shortcuts import redirect, Http404
class ActionForObjectAdmin(admin.ModelAdmin):
"""
Admin that add some urls wich can call an action for given object.
Example:
if url is /admin/myapp/mymodel/object_id/
@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"