Login

3110 snippets

Snippet List

CompressedTextField

I found my self doing data migration for a client, and also found that their old system (CSV) had 4 text fields that were 2 to 4MB each and after about 2 minutes of hammering the mysql server as I was parsing this and trying to insert the data the server would drop all connections. In my testing I found that if I didnt send those 4 fields the mysql server was happy to let me migrate all my data all (240GB of it). So I started thinking, "I should just store these fields compress anyways, a little over head to render the data, but thats fine by me." So thus was born a CompressedTextField. It bz2 compresses the contents then does a base64 encode to play nice with the server storage of text fields. Once I started using this my data migration, though a bit slower then without the field data, ran along all happy.

  • text
  • model
  • field
  • compress
Read More

Cached template filters

Say you'd like to cache one of your template filters. This decorator acts sort of like memoize, caching a result set based on the arguments passed in (which are used as the cache key).

  • template
  • cache
Read More

Latest instances template filter

Writing templatetags is obnoxious. Say you have a small blurb on all your pages that shows the latest 5 comments posted to the site -- using this filter, you could write the following: {% for comment in "comments.comment"|latest:5 %} ...display comment here... {% endfor %}

  • template
Read More

Class Feeds DRY TemplateTag

I'm using the Django Feeds Framework and it's really nice, very intuitive and easy to use. But, I think there is a problem when creating links to feeds in HTML. For example: <link rel="alternate" type="application/rss+xml" title="{{ feed_title }}" href="{{ url_of_feed }}" /> Link's `HREF` attribute can be easily found out, just use `reverse()` But, what about the `TITLE` attribute? Where the template engine should look for this? Even more, what if the feed is build up dinamically and the title depends on parameters (like [this](http://docs.djangoproject.com/en/dev/ref/contrib/syndication/#a-complex-example))? This is the solution I came up with. However, as you can see, there is some caveats: * Requires Django 1.2 Class Feeds, don't know exactly how to do this with the old way of feeds. * If the feed class uses the request object, the `request` [context processor](http://docs.djangoproject.com/en/dev/ref/templates/api/#django-core-context-processors-request) must be configured, since `None` is passed if it isn't present in the context. * There's an oddity with Feed.__get_dynamic_attr(). The Feed subclass instance doesn't have this method; instead, it appears with another name. Don't know how to figure the name out at runtime... I've posted this problem on [StackOverflow](http://stackoverflow.com/questions/2784659/django-dry-feeds), but didn't get a better answer.

  • Feeds DRY templatetag HTML LINK
Read More

Parsing Tag decorator

A simple decorator to register a template.Node object as a tag, without the need to write the usual "`token.split_contents()` yatta yatta blatta blatta" function. The decorator should be called with the name of the tag, and an optional `required` named argument in case your tag requires a minimum number of arguments. Please note that all the arguments in the templatetag call will be passed to the node (except the templatetag name and the `for` and `as` keywords) in the order given. Suggestions etc are very welcome, of course =)

  • templatetag
Read More

Capture Stack Trace Decorator

Put this decorator on any function to capture any exceptions generated within and print to a stack trace. example: @catch def my_func(): # code that may raise an exception here

  • decorator
  • debugging
Read More

Detect iPhone & Switch Template via render_to_response

A fast way to implement an iPhone template switcher, especially if you have a lot of existing views using the render_to_response() shortcut. This checks for the iPhone browser and then modifies the chosen template by adding -mobile to the html's file name. Check out [this more complete list of user agents](http://minidetector.googlecode.com/svn/trunk/minidetector/tests/mobile_useragents.txt) if you need to detect specific mobile devices.

  • iphone template render_to_response
Read More

csrf_token for mako

Tag that can be used as `${ tags.csrf_token() }` in mako templates. Remember to import the tags namespace in your template, as such: <%namespace name="tags" module="my_app.tags"/>

  • mako csrf_token csrf
Read More

Dynamically alter the attributes of a formset

Allow you to specify a "General case formset/modelformset" and then alter the attributes of that formset, specificly: extra, can_order, can_delete and max_num. So you specify: >>> formset = AuthorFormSet(queryset=Author.objects.filter(name__startswith='O')) and then you want to dynamically add multiple fields with javascript and save the new ones. By default a formset only has 1 extra field. With this you can return a new formset (using the same queryset, forms, formset base class, etc) but with different attributes. So you could then add 10 extra fields if the user added 10 new forms.

  • dynamic
  • formset
  • attributes
Read More

icon shortcut - pseudohtml tag with attribute merging and variables resolving

A shortcut for generating img-s with predefined classes and attributes that mimics a html tag, while resolving context variables inside {% %} without crutches like tag/stuff/endtag. Used as `{% icon test class="spam" eggs="{{ object.pk }}" %}` yields `<img src="http://host.tld/media/icons/test.png" alt="" class="icon16 spam" eggs="42"/>`. Not customizable here for simplicity.

  • html
  • macro
Read More
Author: wiz
  • 0
  • 0