djangosnippets.org: Latest snippets posted by amituhttp://djangosnippets.org/users/amitu/2009-03-20T16:41:15.792661-05:00amiturequest_logger
2009-03-20T16:41:15.792661-05:00amituhttp://djangosnippets.org/snippets/1387/<p>Simple logger, stores all query parameter and post parameters for each query. </p>
Freely redistributablecreate_logger helper
2009-03-20T16:39:03.952459-05:00amituhttp://djangosnippets.org/snippets/1386/<p>Lets you easily create loggers. </p>
Freely redistributableexecutesql
2009-03-20T16:36:33.937519-05:00amituhttp://djangosnippets.org/snippets/1385/<p>A helper function for quick and dirty sql calls. </p>
Freely redistributablesqltojson
2009-03-20T16:35:24.618055-05:00amituhttp://djangosnippets.org/snippets/1384/<p>A helper.</p>
Freely redistributablesql to dict
2009-03-20T16:34:36.943902-05:00amituhttp://djangosnippets.org/snippets/1383/<p>A helper utility, does what name says. </p>
Freely redistributablefunction tracing decorator
2009-03-20T16:31:50.291068-05:00amituhttp://djangosnippets.org/snippets/1382/<p>This is a decorator that logs function arguments, return object and time taken for execution as well as any exception that might have been raised by the function. </p>
<p>Useful for debug logging. </p>
Freely redistributablethemed_template_loader
2009-03-02T22:44:28.337848-06:00amituhttp://djangosnippets.org/snippets/1352/<p>I developed this template loader for adding themes support in <a href="http://www.amitu.com/gitology/">gitology</a>. In order to support theming django applications, add this template loader at as the first TEMPLATE_LOADERS settings.py setting. </p>
<p>Anywhere you request base.html, blog/index.html, when the theme is set to "bw", it will look for bw/base.html or bw/blog/index.html files …</p>
Freely redistributableajax form handler generic view
2008-10-26T09:33:10.229820-05:00amituhttp://djangosnippets.org/snippets/1157/<p>Some ajax heavy apps require a lot of views that are merely a wrapper around the form. This generic view can be used for them. </p>
Freely redistributableSizeAndTimeMiddleware
2008-10-14T08:05:00.095989-05:00amituhttp://djangosnippets.org/snippets/1142/<p>Used for showing size of the page in human readable format and time taken to generate the page on the server. To use it, in your base template, somewhere put the line: <code><!-- ____SIZE_AND_DATE_PLACEHOLDER____ --></code>. May be used on production.
</p>
Freely redistributableminimal nginx conf to split get/post requests
2008-10-14T07:33:17.843728-05:00amituhttp://djangosnippets.org/snippets/1141/<p>After a point the sql server becomes the bottleneck in lots of web application, and to scale, master-slave replication with single master, multiple slave is recommended. This setup with nginx can be used to accomplish traffic distribution between master and slave based on request method. </p>
Freely redistributableajax error sink
2008-10-14T07:14:25.660668-05:00amituhttp://djangosnippets.org/snippets/1140/<p>Often its useful to get error information for ajax/javascript errors happening on various clients. This can go to something like this:</p>
<pre><code># error_sink
def error_sink(request):
# post request, with event name in "event", and event data in "data"
context = request.REQUEST.get("context", "")
context = cgi.parse_qs(context)
context["data"] = cgi.parse_qs(context.get("data", [""])[0])
context["user"] …</code></pre>
Freely redistributablebetter paginator template tag
2008-10-14T02:58:21.176693-05:00amituhttp://djangosnippets.org/snippets/1139/<p>This is slight improvement over <a href="http://www.djangosnippets.org/snippets/73/">Paginator|Snippet 73</a>. That used to not work properly if querystring already contains other parameters, like search result page. </p>
<p>website/paginator.html:</p>
<pre><code><br /><center>
<span class="lbottom">
{% if has_previous %}<a href="{{ path }}page={{ previous }}"><< Previous </a>{% else %}<span>Previous </span>{% endif %}
{% if show_first %}<a href="{{ …</code></pre>
Freely redistributableset_paths
2008-10-14T02:35:10.518978-05:00amituhttp://djangosnippets.org/snippets/1138/<p>To make all scripts relocatable.</p>
<p>The layout of my project is:</p>
<pre><code>/some/path/myproject/
/some/path/myproject/some_script
/some/path/myproject/some_other_script
/some/path/myproject/set_paths.py
/some/path/myproject/setttings.py
/some/path/myproject/lib/ # some external libraries/apps checked in with my project.
/some/path/myproject/myapp/ # my apps etc.
</code></pre>
<p>This way myproject folder can be moved anywhere on the file system, and calling right path, settings.py is used. </p>
Freely redistributablesend_html_mail
2008-10-14T02:13:49.717728-05:00amituhttp://djangosnippets.org/snippets/1137/<p>There are many versions, this is the one I like. This is quite generic, can auto generate text version of the mail if required. </p>
Freely redistributableajax_validator generic view
2008-10-14T02:00:10.890020-05:00amituhttp://djangosnippets.org/snippets/1136/<p>Sample jQuery javascript to use this view:</p>
<pre><code>$(function(){
$("#id_username, #id_password, #id_password2, #id_email").blur(function(){
var url = "/ajax/validate-registration-form/?field=" + this.name;
var field = this.name;
$.ajax({
url: url, data: $("#registration_form").serialize(),
type: "post", dataType: "json",
success: function (response){
if(response.valid)
{
$("#"+field+"_errors").html("Sounds good");
}
else
{
$("#"+field+"_errors").html(response.errors);
}
}
});
});
});
</code></pre>
<p>For each field …</p>
Freely redistributable