Skip to content

Instantly share code, notes, and snippets.

View yuanxu's full-sized avatar

yuanxu yuanxu

  • Shijiazhuang China
View GitHub Profile
@yuanxu
yuanxu / ajaxform.js
Created August 22, 2013 08:44
与bootstrap兼容的,ajax方式提交表单
define(['jquery', 'jqBootstrapValidation', 'bootbox'], function (require, exports) {
"use strict";
/***
* 符合promise及bootstrap规范的窗体帮助类.
*
* 自动绑定到标记有ajaxform类的窗体,并且提供使用jqBootstrapValidation验证功能
*
* 需要实例化才能使用
*/
@yuanxu
yuanxu / async_upload.js
Created August 22, 2013 08:44
文件异步上传
define(['jquery', 'bootstrap'], function (require, exports) {
"use strict";
var $ = require('jquery');
/**
* 上传单个文件
* @param $form 要监控的表单对象
* @param $file 要监控的文件对象
* @param $img 完成后显示结果的控件
@yuanxu
yuanxu / proxy.py
Created August 22, 2013 08:31
设置socket,支持代理服务器
# coding=utf-8
from django.conf import LazySettings
def _setup_proxy():
'''
初始化proxy
@return:
'''
settings = LazySettings()
@yuanxu
yuanxu / chatroom.js
Created July 22, 2013 06:31
knockoutjs & underscore template
/* ---- Begin integration of Underscore template engine with Knockout. Could go in a separate file of course. ---- */
ko.underscoreTemplateEngine = function () {
};
ko.underscoreTemplateEngine.prototype = ko.utils.extend(new ko.templateEngine(), {
renderTemplateSource: function (templateSource, bindingContext, options) {
// Precompile and cache the templates for efficiency
var precompiled = templateSource['data']('precompiled');
if (!precompiled) {
precompiled = _.template("<% with($data) { %> " + templateSource.text() + " <% } %>");
templateSource['data']('precompiled', precompiled);
@yuanxu
yuanxu / views.py
Created June 30, 2013 03:06
django inline formset与class based view
class OrganCreateView(PagedPerviousMixin, CreateView):
model = Organ
form_class = OrganForm
template_name = 'shared/admin/organ_form.html'
def get_context_data(self, **kwargs):
context = super(OrganCreateView, self).get_context_data(**kwargs)
if self.request.method == 'POST':
@yuanxu
yuanxu / expressions.py
Created June 29, 2013 15:08
一个生成django queryset.filter参数的例子。核心内容:只需按照常规的filter写法将参数和值拆分为一个tuple即可。如,qs.filter(id=5)对应为(id,5)。如需复杂的条件,需要继承自django.utils.tree.Node。
MODE_PROVINCE = 10000
MODE_CITY = 100
def D(district_field_name, district_id):
'''
获取区域选筛选表达式
@param district_field_name:
@param district_id:
@return:(字段表达式,值)
@yuanxu
yuanxu / gist:4601329
Created January 23, 2013 02:30
Let urllib support proxy
import socks
import socket
proxy_type, proxy_ip, proxy_port = settings.PROXY_SERVER
socks.setdefaultproxy(proxy_type, proxy_ip, proxy_port)
socket.socket = socks.socksocket
def _create_connection(address, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, source_address=None):
msg = "getaddrinfo returns an empty list"
host, port = address