Skip to content

Instantly share code, notes, and snippets.

View k4ml's full-sized avatar
🏠
Working from home

Kamal Mustafa k4ml

🏠
Working from home
View GitHub Profile
<div class="frm-item" jsselect="$this">
<label jscontent="$this"></label><br />
<input type="text" jsvalues="value:$item[$this]"><br />
</div>
<script>
jq("a.item-result").live('click', function() {
var itemid = jq(this).attr('itemid');
var baseurl = 'proxy.php?url=http://localhost:9999/item';
jq.getJSON(baseurl + '/' + itemid, function(json) {
@k4ml
k4ml / QBuilder.class.php
Created November 11, 2010 22:06
Generic SQL query builder
<?php
class QBuilderCondition {
protected $conjunction = 'AND';
protected $wheres = array();
public function __construct($conjunction = 'AND') {
$this->conjunction = $conjunction;
}
<?php
function modulename_theme($existing, $type, $theme, $path) {
return array(
'formtemplate_horizontal' => array(
'arguments' => array('form' => NULL),
),
);
}
function theme_formtemplate_horizontal($form) {
#!/usr/bin/env python
from optparse import OptionParser
from os.path import join, dirname, abspath
from string import Template
import os, sys
parser = OptionParser()
parser.add_option("-a", "--address", default="127.0.0.1", help="Server address to listen")
@k4ml
k4ml / kvstore.py
Created April 17, 2011 23:26
Key/value store wrapper to redis
import redis
class KVStore:
def __init__(self, prefix, host='localhost', port=6379, **params):
self._prefix = prefix
self._db = redis.Redis(host=host, port=port)
def _get_prefix(self, key):
return '%s:%s' % (self._prefix, key)
@k4ml
k4ml / test_flask.py
Created May 7, 2011 19:04
Flask+SQLAlchemy
from flask import Flask, request, session
from sqlalchemy import create_engine, Table, MetaData
from sqlalchemy.orm import mapper, relationship, sessionmaker, scoped_session
from sqlalchemy.sql import select
app = Flask(__name__)
engine = create_engine('postgresql+psycopg2://kamal@localhost/clinic_dev')
metadata = MetaData(bind=engine)
DBSession = scoped_session(sessionmaker(bind=engine))
@k4ml
k4ml / gist:989248
Created May 24, 2011 17:54
Catch transaction before it being caught by transaction.commit_manually
def transact(func):
"""
Usage:
@transaction.commit_manually
@transact
def some_function(a, b):
do_something()
"""
def decorated(*args, **kwargs):
try:
@k4ml
k4ml / mini_django.py
Created March 27, 2012 20:01
Django in single file with model and admin
"""
Django in single file with model and admin. Based on:-
http://fahhem.com/blog/2011/10/django-models-without-apps-or-everything-django-truly-in-a-single-file/
"""
import sys
from os import path as osp
def rel_path(*p): return osp.normpath(osp.join(rel_path.path, *p))
rel_path.path = osp.abspath(osp.dirname(__file__))
@k4ml
k4ml / app.py
Created March 28, 2012 00:18
Refactor of gist:2219751 to make it reusable
"""
This use mini_django.py, sort of micro framework based on django.
"""
import mini_django
mini_django.configure()
from django.db import models
from django.contrib import admin
from django.http import HttpResponse
@k4ml
k4ml / gist:3483810
Created August 26, 2012 21:54
Django transaction.commit_manually wrapper
from django.db import transaction
def transaction_commit_manually_wrapper(func):
"""Wrapper to transaction.commit_manually so that we can reraise any
uncaught exception instead of getting TransactionManagementError.
Ref - https://code.djangoproject.com/ticket/6623
"""
@transaction.commit_manually
def func_wrapper(*args, **kwargs):