Skip to content

Instantly share code, notes, and snippets.

View darklow's full-sized avatar

Kaspars Sprogis darklow

View GitHub Profile
@darklow
darklow / json_encode_pretty.php
Created April 18, 2012 13:53
JSON Encode pretty
<?php
function json_encode_pretty($obj, $indentation = 0)
{
switch (gettype($obj))
{
case 'object':
$obj = get_object_vars($obj);
case 'array':
if (!isset($obj[0]))
@darklow
darklow / gist:2483009
Created April 24, 2012 19:37
Elasticsearch geo search debug
// Create index with mapping
curl -XPUT http://192.168.0.10:9200/geotest/ -d '{
"settings" : {
"number_of_shards" : 1
},
"mappings" : {
"places" : {
"properties" : {
"name" : { "type" : "string" },
"location" : { "type" : "geo_point", "lat_lon": true }
@darklow
darklow / gist:2757570
Created May 20, 2012 10:16
Elasticsearch log mapping
// Index config
{
"number_of_shards": 5,
"number_of_replicas": 1,
"analysis": {
"analyzer": {
"log_analyzer": {
"type": "custom",
"tokenizer": "pattern",
"filter": [
@darklow
darklow / TsvectorType.php
Created July 17, 2012 12:13
Doctrine2 PostgreSQL tsvector type class
<?php
namespace Acme\AcmeBundle\Dbal;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;
class TsvectorType extends Type
{
public function getName()
{
@darklow
darklow / GearmanClient.php
Created August 10, 2012 07:00
GearmanClient class extended to prevent duplicate jobs
<?php
class GearmanClient extends \GearmanClient
{
const PRIORITY_LOW = 'low';
const PRIORITY_HIGH = 'high';
const PRIORITY_NORMAL = 'normal';
public function getUniqueId($function_name, $workload, $priority = null)
{
@darklow
darklow / .bashrc
Last active December 13, 2015 21:18
Auto-detect virtualenv relatively from current directory
# auto source virtual envs if it can find one
has_virtualenv() {
path="../env/bin/activate"
if [ -e $path ]; then
path=$(readlink -f $path)
if [ -z "$CURRENT_VENV_PATH" -o -z "$_OLD_VIRTUAL_PATH" -o "$CURRENT_VENV_PATH" != "$path" ] ; then
source $path
export CURRENT_VENV_PATH="$path"
echo -e "\e[01;30mvirtualenv $path activated\e[00m"
fi
class ReadOnlyWidget(forms.TextInput):
def render(self, name, value, attrs=None):
if value is None:
value = ''
final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
if value != '':
# Only add the 'value' attribute if a value is non-empty.
final_attrs['value'] = force_unicode(self._format_value(value))
return mark_safe(u'<span%s />%s</span>' % (flatatt(final_attrs), value))
# urls.py
urlpatterns = patterns('',
# Example for custom menu
url(r'^admin/custom/$', views.custom_view),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
# urls.py
url(r'^admin/dashboard/', staff_member_required(DashboardListView.as_view()), name='dashboard'),
# views.py
class DashboardListView(ListView):
context_object_name = 'task_list'
template_name = 'admin/dashboard.html'
def get_queryset(self):
# urls.py
urlpatterns = patterns('',
# ...
# Example for custom view
url(r'^admin/custom/$', views.custom_view),
# ...
# Admin