Skip to content

Instantly share code, notes, and snippets.

@renyi
renyi / managers.py
Created August 18, 2012 07:34
Simple Geo model for Django
from geopy import units, distance
from mezzanine.core.managers import CurrentSiteManager
class GeoManager(CurrentSiteManager):
def near(self, latitude=None, longitude=None, distance_range=30):
queryset = super(GeoManager, self).get_query_set()
if not (latitude and longitude and distance_range):
return queryset.none()
@renyi
renyi / django-wkhtmltopdf-static-url-hack.py
Created May 21, 2016 10:38
django-wkhtmltopdf STATIC_URL hack to make static files work on both local and remote hosting.
def render_to_response(self, context, **response_kwargs):
from django.conf import settings
STATIC_URL = settings.STATIC_URL
if 'http' not in STATIC_URL:
# wkhtmltopdf requires full uri to load css
from urlparse import urlparse
parsed = urlparse(self.request.META.get('HTTP_REFERER'))
parsed = '{uri.scheme}://{uri.netloc}'.format(uri=parsed)
context["STATIC_URL"] = "{}{}".format(parsed, settings.STATIC_URL)
@renyi
renyi / gist:877176
Created March 19, 2011 03:05
CSS Font family
/* sans-serif */
font-family: Arial, "Helvetica Neue", Helvetica, sans-serif;
font-family: Helvetica, "Helvetica Neue", Arial, sans-serif;
font-family: Verdana, Geneva, Tahoma, sans-serif;
font-family: Trebuchet, Tahoma, Arial, sans-serif;
font-family: GillSans, Calibri, Trebuchet, sans-serif;
font-family: "DejaVu Sans", "Bitstream Vera Sans", "Segoe UI", "Lucida Grande", Verdana, Tahoma, Arial, sans-serif;
font-family: Geneva, "Lucida Sans", "Lucida Grande", "Lucida Sans Unicode", Verdana, sans-serif;
font-family: Geneva, Verdana, "Lucida Sans", "Lucida Grande", "Lucida Sans Unicode", sans-serif;
font-family: "HelveticaNeue-Roman", "Helvetica 55 Roman", Helvetica, Arial, sans-serif;
@renyi
renyi / gist:1558477
Created January 4, 2012 04:23
python urlencode/urldecode
import urllib2
def urlencode(s):
return urllib2.quote(s)
def urldecode(s):
return urllib2.unquote(s).decode('utf8')
@renyi
renyi / gist:7019854
Created October 17, 2013 06:16
Django, download and store image in ImageField
image_url = og.get('image')
if image_url:
import urllib2
from django.core.files import File
from django.core.files.temp import NamedTemporaryFile
img_temp = NamedTemporaryFile(delete=False)
img_temp.write(urllib2.urlopen(image_url).read())
img_temp.flush()
import os
@renyi
renyi / utils.py
Created October 20, 2011 04:11
My common python string utils
import array
def ilist_to_str(list):
'''Integer list to string.'''
return array.array('B', list).tostring()
def str_to_ilist(string):
'''String to integer list'''
return array.array('b', string).tolist()
@renyi
renyi / gist:4522029
Last active May 31, 2017 14:42
Queryset sorting
# http://stackoverflow.com/questions/431628/how-to-combine-2-or-more-querysets-in-a-django-view
from itertools import chain
from operator import attrgetter
def join_list(*args, sort_key=None):
if sort_key:
return sorted(chain(args), key=attrgetter(sort_key))
return list(chain(args))
@renyi
renyi / gist:7300109
Created November 4, 2013 09:21
Replaces {% url something %} with {% url "something" %}
find . -name "*.html" -print0 | xargs -0 sed -i "" 's/ url \([^" >][^ >]*\)/ url "\1"/g'
#!/usr/bin/env pypy
import time
n = 13
# n = 1000000
if __name__ == "__main__":
<!-- new_base.html -->
<header>
{% block header %}
{% endblock %}
</header>
<main id="content" class="group" role="main">
{% block main %}
<div id="main-container" class="main-container container">