Skip to content

Instantly share code, notes, and snippets.

View vstoykov's full-sized avatar
🇧🇬

Venelin Stoykov vstoykov

🇧🇬
View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / 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 <[email protected]>
#
# 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