Skip to content

Instantly share code, notes, and snippets.

View un33k's full-sized avatar
🎯
Focusing

Val Neekman un33k

🎯
Focusing
View GitHub Profile
@un33k
un33k / pip-install-howto.txt
Created November 10, 2011 01:44
pip install info
The requirements file format
============================
The requirements file is a way to get pip to install specific packages
to make up an *environment*. This document describes that format. To
read about *when* you should use requirement files, see `Requirements
Files <./#requirements-files>`_.
Each line of the requirements file indicates something to be
installed. For example::
#!/bin/sh
POSTGISCONTRIB_PATH=`pg_config --sharedir`/contrib
POSTGIS=$(find $POSTGISCONTRIB_PATH -name 'postgis.sql')
POSTGIS_COMMENTS=$(find $POSTGISCONTRIB_PATH -name 'postgis_comments.sql')
POSTGIS_SPATIAL=$(find $POSTGISCONTRIB_PATH -name 'spatial_ref_sys.sql')
if [ -z "$POSTGIS" ] || [ -z "$POSTGIS_SPATIAL" ]; then
echo " * Not found postgis contrib files."
exit 1
@un33k
un33k / registration_view.py
Created January 26, 2012 04:16 — forked from langri-sha/registration_view.py
Class-based django-registration view
from django.views.generic.edit import FormView
from django.views.generic.base import RedirectView
from registration.models import RegistrationProfile
from forms import RegistrationForm
class AccountRegistrationView(FormView):
template_name = 'authentication/registration_form.html'
form_class = RegistrationForm
@un33k
un33k / app1.search_indexes.py
Created March 9, 2012 18:06 — forked from valyagolev/app1.search_indexes.py
django-haystack is annoying
from search_sites import register_model_for_search
from .models import CoolModel
register_model_for_search(CoolModel)
@un33k
un33k / search highlighter
Created March 27, 2012 21:38
high light search result
import re
from django import template
from django.template import Node, TemplateSyntaxError
from django.conf import settings
from django.utils.safestring import mark_safe
from wwwsite.applications.portal.utils.search import get_tokenize
register = template.Library()
def highlight_word(needles, haystack, class_name='highlight'):
@un33k
un33k / highlight needles in haystack
Created March 27, 2012 21:56
highlight needles in haystack -- search
def highlight_word(needles, haystack, cls_name='highlighted'):
regex = re.compile(r"(%s)" % "|".join(needles), re.I)
i = 0; out = ""
for m in regex.finditer(haystack):
out += "".join([haystack[i:m.start()],'<span class="%s">'%cls_name,haystack[m.start():m.end()],"</span>"])
i = m.end()
return mark_safe(out + haystack[i:])
@un33k
un33k / bookmark
Created April 8, 2012 19:07
bookmark class
from django.db import models
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
class BookmarkQuerySet(models.query.QuerySet):
def get_or_create(self, **kwargs):
new_kwargs = {}
content_object = kwargs.get('content_object')
@un33k
un33k / text.py
Created April 20, 2012 00:12 — forked from joshourisman/text.py
A Django template filter that truncates a string to at most X characters while respecting word boundaries.
from django.template import Library
from django.utils.encoding import force_unicode
from django.utils.functional import allow_lazy
from django.template.defaultfilters import stringfilter
register = Library()
def truncate_chars(s, num):
"""
Template filter to truncate a string to at most num characters respecting word
@un33k
un33k / git-create-branch.sh
Created May 8, 2012 02:01
Create a git branch
#!/bin/sh
# git-create-branch <branch_name>
if [ $# -ne 1 ]; then
echo 1>&2 Usage: $0 branch_name
exit 127
fi
set branch_name = $1
git push origin origin:refs/heads/${branch_name}
@un33k
un33k / onlinenow.py
Created June 12, 2012 01:47 — forked from dfalk/onlinenow.py
django online users
from django.core.cache import cache
from django.conf import settings
from django.contrib.auth.models import User
ONLINE_THRESHOLD = getattr(settings, 'ONLINE_THRESHOLD', 60 * 15)
ONLINE_MAX = getattr(settings, 'ONLINE_MAX', 50)
def get_online_now(self):
return User.objects.filter(id__in=self.online_now_ids or [])