Skip to content

Instantly share code, notes, and snippets.

View timonweb's full-sized avatar

Tim Kamanin timonweb

View GitHub Profile
@timonweb
timonweb / gist:1127138
Created August 5, 2011 08:35
Sortable Table with Pager in Drupal 7
<?php
function folks_db_report() {
$header = array(
'id' => array('data' => t('Id'), 'field' => 'u.id'),
'first_name' => array('data' => t('First Name'), 'field' => 'u.first_name'),
'last_name' => array('data' => t('Last Name'), 'field' => 'u.last_name'),
'color' => array('data' => t('Favorite Color'), 'field' => 'u.color'),
);
@timonweb
timonweb / gist:1229857
Created September 20, 2011 18:19
Drupal 6 theme_links which doesn't escape HTML in anchors
<?php
function YourThemeName_links($links, $attributes = array('class' => 'links')) {
global $language;
$output = '';
if (count($links) > 0) {
$output = '<ul' . drupal_attributes($attributes) . '>';
$num_links = count($links);
$i = 1;
@timonweb
timonweb / gist:1334321
Created November 2, 2011 17:41
Russian Plural Formatter
/**
* Plural form for Russian Language
* $form1 - singular value
* $form2 - double value
* $form3 - multiple value
*/
function format_russian_plural($n, $form1, $form2, $form3) {
$n = abs($n) % 100;
$n1 = $n % 10;
if ($n > 10 && $n < 20) return $form3;
@timonweb
timonweb / gist:2149035
Created March 21, 2012 16:05
Deletes url aliases from a desired content type in Drupal 6
<?php
$result = db_query("SELECT nid FROM {node} WHERE type='publicprofile'");
while ($data = db_fetch_object($result)) {
db_query("DELETE FROM {url_alias} WHERE src LIKE '%s'", 'node/' . $data->nid);
drupal_set_message('Deleted alias for a node %nid', array('%nid' => $data->nid));
}
?>
@timonweb
timonweb / gist:2622961
Created May 6, 2012 15:33
Django FileField with upload_to determined at runtime
def content_file_name(instance, filename):
return '/'.join(['content', instance.user.username, filename])
class Content(models.Model):
name = models.CharField(max_length=200)
user = models.ForeignKey(User)
file = models.FileField(upload_to=content_file_name)
@timonweb
timonweb / gist:2623793
Created May 6, 2012 18:55
Django: Programmatically saving image from URL to FileField or ImageField http://twigstechtips.blogspot.com/2012/04/django-programmatically-saving-image.html
class Product(models.Model):
# other fields
image = models.FileField(storage = MogileFSStorage(), upload_to = 'product_images')
from django.core.files import File
from django.core.files.temp import NamedTemporaryFile
product = Product()
# set all your variables here
product.save()
@timonweb
timonweb / file_cleanup.py
Created May 7, 2012 13:47
File cleanup callback used to emulate the old delete behavior using signals. Initially django deleted linked files when an object containing a File/ImageField was deleted.
import os
from django.db.models.fields.files import FileField
from django.core.files.storage import default_storage
def file_cleanup(sender, **kwargs):
"""
File cleanup callback used to emulate the old delete
behavior using signals. Initially django deleted linked
files when an object containing a File/ImageField was deleted.
@timonweb
timonweb / gist:2693658
Created May 14, 2012 12:18
Shell script to watch the disk space
#!/bin/sh
# set -x
# Shell script to monitor or watch the disk space
# It will send an email to $ADMIN, if the (free available) percentage of space is >= 90%.
# -------------------------------------------------------------------------
# Set admin email so that you can get email.
ADMIN="root"
# set alert level 90% is default
ALERT=90
# Exclude list of unwanted monitoring, if several partions then use "|" to separate the partitions.
<?xml version="1.0" ?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
@timonweb
timonweb / profile_middleware.py
Created May 23, 2012 07:45 — forked from kesor/profile_middleware.py
Django cProfile middleware
from django.core.exceptions import MiddlewareNotUsed
from django.conf import settings
import cProfile
import pstats
import marshal
from cStringIO import StringIO
class ProfileMiddleware(object):
def __init__(self):
if not settings.DEBUG: