Login

Snippets by amitu

Snippet List

request_logger

Simple logger, stores all query parameter and post parameters for each query.

  • debugging
  • logger
Read More

function tracing decorator

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. Useful for debug logging.

  • decorator
  • debugging
  • tracing
Read More

themed_template_loader

I developed this template loader for adding themes support in [gitology](http://www.amitu.com/gitology/). In order to support theming django applications, add this template loader at as the first TEMPLATE_LOADERS settings.py setting. 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 first. Takes care of both render_to_response() in view or {% load template %} in templates.

  • templateloader
  • theming
Read More

SizeAndTimeMiddleware

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: `<!-- ____SIZE_AND_DATE_PLACEHOLDER____ -->`. May be used on production.

  • middleware
Read More

minimal nginx conf to split get/post requests

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.

  • middleware
  • nginx
  • loadbalancing
  • master-slave
Read More

ajax error sink

Often its useful to get error information for ajax/javascript errors happening on various clients. This can go to something like this: # 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"] = request.vuser context["referrer"] = request.META.get('HTTP_REFERER', "referrer not set") context = pformat(context) send_mail( "ajax error", context, "[email protected]", ["[email protected]",], fail_silently=True ) return JSONResponse({"status": "ok" }) # }}}

  • ajax
  • jquery
  • error
  • reporting
Read More

better paginator template tag

This is slight improvement over [Paginator|Snippet 73](http://www.djangosnippets.org/snippets/73/). That used to not work properly if querystring already contains other parameters, like search result page. website/paginator.html: <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="{{ path }}page=1">First </a>{% endif %} {% for page_no in page_numbers %} {% ifnotequal page_no page %} <a href="{{ path }}page={{ page_no }}">{{ page_no }} </a> {% else %} {{ page_no }} {% endifnotequal %} {% endfor %} {% if show_last %}<a href="{{ path }}page={{ pages }}">Last </a>{% endif %} {% if has_next %}<a href="{{ path }}page={{ next }}">Next >></a>{% else %}<span>Next </span>{% endif %} </span> <br /></center>

  • templatetag
  • paginator
Read More

set_paths

To make all scripts relocatable. The layout of my project is: /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. This way myproject folder can be moved anywhere on the file system, and calling right path, settings.py is used.

  • django
  • cron
  • scripts
Read More

send_html_mail

There are many versions, this is the one I like. This is quite generic, can auto generate text version of the mail if required.

  • html-mail
Read More

ajax_validator generic view

Sample jQuery javascript to use this view: $(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); } } }); }); }); For each field you will have to put a div/span with id like fieldname_errors where the error message will be shown.

  • ajax
  • javascript
  • view
  • generic
  • jquery
  • validation
  • form
Read More

amitu has posted 19 snippets.