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 / 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 / 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 / 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 / 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 / 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
#!/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 / 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::
@un33k
un33k / reset_spotlight.txt
Created November 9, 2011 00:33
reset spot light metadata and disable it
To display the status of indexing
# mdutil -s /
To clear the metadata store
Turn indexing off for /
# mdutil -i off /
Clear the metadata store for /
# mdutil -E /
Turn indexing back on for /
# mdutil -i on /
@un33k
un33k / admin.py
Created October 25, 2011 02:00 — forked from uhop/admin.py
Adding Dojo's rich editor to Django's Admin.
# Example how to add rich editor capabilities to your models in admin.
from django.contrib.admin import site, ModelAdmin
import models
# we define our resources to add to admin pages
class CommonMedia:
js = (
'https://ajax.googleapis.com/ajax/libs/dojo/1.6.0/dojo/dojo.xd.js',
@un33k
un33k / pickle.py
Created October 24, 2011 13:34
simple serialization of a python object
try:
import cPickle as pickle
except:
import pickle
import pprint
data1 = [ { 'a':'A', 'b':2, 'c':3.0 } ]
print 'BEFORE:',
pprint.pprint(data1)