Skip to content

Instantly share code, notes, and snippets.

@yprez
yprez / gist:4061556
Last active October 12, 2015 17:27
Enable all logging
import logging
logging.basicConfig()
logger = logging.getLogger('')
logger.setLevel(logging.DEBUG)
@yprez
yprez / gist:4127960
Created November 21, 2012 21:37
Installing pip + virtualenv on Ubuntu

Install pip and virtualenv:

sudo apt-get install python_setuptools
sudo easy_install pip
sudo pip install virtualenv virtualenvwrapper

Set the following in ~/.bashrc:

WORKON_HOME=/home/yuri/virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
@yprez
yprez / 5.1
Created December 1, 2012 08:11
MySQL set unicode as default
[mysqld]
default-character-set=utf8
default-collation=utf8_general_ci
character-set-server=utf8
collation-server=utf8_general_ci
init-connect='SET NAMES utf8'
[client]
default-character-set=utf8
@yprez
yprez / email_list_field.py
Created October 6, 2013 15:00
Email list field for Django Rest Framework
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _
from rest_framework import serializers
class EmailListField(serializers.EmailField):
default_error_messages = {
'invalid': _('Invalid E-mail address.'),
}
@yprez
yprez / fields.py
Last active February 19, 2023 12:08
Django rest framework - Base64 image field
import base64
from django.core.files.base import ContentFile
from rest_framework import serializers
class Base64ImageField(serializers.ImageField):
def from_native(self, data):
if isinstance(data, basestring) and data.startswith('data:image'):
# base64 encoded image - decode
@yprez
yprez / managers.py
Created January 13, 2016 12:27
Django - EstimateCountManager (faster, but innacurate counts for PostgreSQL)
from django.db import connection
from django.db import models
class EstimateCountManager(models.Manager):
def estimate_count(self):
"""
Postgres really sucks at full table counts, this is a faster version
see: http://wiki.postgresql.org/wiki/Slow_Counting
"""
@yprez
yprez / nginx.conf
Created March 27, 2017 18:39 — forked from AysadKozanoglu/jail.conf
fail2ban nginx 404 400 403 444 filter /etc/fail2ban/filter.d/nginx-4xx.conf
# nano /etc/fail2ban/filter.d/nginx-4xx.conf
#
[Definition]
failregex = ^<HOST>.*"(GET|POST).*" (404|444|403|400) .*$
ignoreregex =