Login

All snippets

Snippet List

Cleanup dirty HTML from a WYSIWYG editor

My admin allows editing of some html fields using TinyMCE, so I end up with horrible code that contains lots of nested `<p>`, `<div>`, `<span>` tags, and style properties which destroy my layout and consistence. This tag based on lxml tries to kill as much unneeded tags as possible, and style properties. These properties can be customized by adapting the regex to your needs.

  • html
  • wysiwyg
  • tinymce
  • lxml
  • dirty
  • cleanup
Read More

Composite Indexing for MySQL

A quick and dirty hack for composite indexing if you need it. Drop this into a models.py or some other place where it'll be loaded along with the rest of Django on start up. Then add an _index_together tuple specifying the fields you want a composite index on.

  • models
  • mysql
  • composite-indexing
Read More

web-key: Base64 Shared Secret for Access Control

At the [Internet Identity Workshop](http://iiw.idcommons.net/Iiw8) in May, 2009, I spoke to Alan Karp and Tyler Close of HP Labs about their research on authorization without identity. Here are my [Delicious links](http://delicious.com/sbwms/ZBAC) on the subject. This led me to write code to generate a "web-key," the shared secret needed to implement the access control method discussed. In his paper, Tyler Close recommends 70 bits for the shared secret, encoded as a 13-character Base32 string. I used 72 bits, so the secret is a 12-character, URL-safe Base64 string without padding characters. I'm new to Python and Django, so I welcome refinements!

  • password
  • web-key
  • webkey
  • zbac
  • access-control
Read More
Author: sbw
  • -1
  • 2

Template tag "ifregex" and "ifnotregex"

Outputs the contents of the block if the second argument matches (or not, depending on the tag) the regular expression represented by the first argument. Usage: {% ifregex "^/comments/" request.path %} ... {% endifregex %} {% ifnotregex "^/comments/" request.path %} ... {% else %} ... {% endifnotregex %}

  • template
  • templatetag
  • regex
  • ifregex
Read More

Digg-like pagination

My take on digg-like pagination. Save the code as 'templatetags/pagination_nav.py' in one of your apps. It relies on a 'pagination_nav.html' template. Here is a base template: {% if pages %} <div class="bottom-pagination-nav"> {% if previous_url %}<a href="{{ previous_url }}">{% else %}<span>{% endif %}&laquo; Previous{% if previous_url %}</a>{% else %}</span>{% endif %} {% for group in pages %} {% for page in group %} {% if page.current %}<span>{{ page.number }}</span>{% else %}<a href="{{ page.url }}">{{ page.number }}</a>{% endif %} {% endfor %} {% if not forloop.last %}<span>...</span>{% endif %} {% endfor %} {% if next_url %}<a href="{{ next_url }}">{% else %}<span>{% endif %}Next &raquo;{% if next_url %}</a>{% else %}</span>{% endif %} </div> {% endif %}

  • pagination
  • template-tag
  • paginator
  • digg
Read More

Currency Field Admin Integration

The BooleanField and DecimalField `elif` blocks are only included in this snippet to give context in the admin_list.py. Insert the CurrencyField block into the file and the Currency fields will display properly in record lists. If you have all of the objects ( [Currency Object](http://www.djangosnippets.org/snippets/1525/), [Currency Widget](http://www.djangosnippets.org/snippets/1526/), [Currency Form Field](http://www.djangosnippets.org/snippets/1527/), and the [Currency DB Field](http://www.djangosnippets.org/snippets/1528/) ) then all you have to do is use the DB Field object and the Admin app will (should) work properly. Please let me know if you have any problems.

  • internationalization
  • admin
  • i18n
  • currency
  • field
  • babel
  • decimal
Read More

Currency DB Field

This is an extension of the DecimalField database field that uses my [Currency Object](http://www.djangosnippets.org/snippets/1525/), [Currency Widget](http://www.djangosnippets.org/snippets/1526/), and [Currency Form Field](http://www.djangosnippets.org/snippets/1527/). I placed my Currency object in the Django\\utils directory, the widget in Django\\froms\\widgets_special.py, and the form field in Django\\forms\\fields_special.py because I integrated this set of currency objects into the Admin app ( [here](http://www.djangosnippets.org/snippets/1529/) ) and it was just easier to have everything within Django. UPDATE 08-18-2009: Added 'import decimal' and modified to_python slightly. The rest of the series: [Currency Object](http://www.djangosnippets.org/snippets/1525/), [Currency Widget](http://www.djangosnippets.org/snippets/1526/), [Currency Form Field](http://www.djangosnippets.org/snippets/1527/), [Admin Integration](http://www.djangosnippets.org/snippets/1529/)

  • internationalization
  • i18n
  • database
  • currency
  • field
  • babel
  • decimal
Read More

Currency Form Field

This is an extension of the DecimalField form field that uses my [Currency Object](http://www.djangosnippets.org/snippets/1525/) and [Currency Widget](http://www.djangosnippets.org/snippets/1526/). I placed my Currency object in the Django\\utils directory and the widget in Django\\froms\\widgets_special.py because I integrated a set of currency objects into the Admin app ( [here](http://www.djangosnippets.org/snippets/1529/) ) and it was just easier to have everything within Django. UPDATE 07-30-2009: Add the parse_string argument to properly test the string format as per the update to the [Currency Object](http://www.djangosnippets.org/snippets/1525/) UPDATE 09-15-2009: Properly handle None's in the clean method The rest of the series: [Currency Object](http://www.djangosnippets.org/snippets/1525/), [Currency Widget](http://www.djangosnippets.org/snippets/1526/), [Currency DB Field](http://www.djangosnippets.org/snippets/1528/), [Admin Integration](http://www.djangosnippets.org/snippets/1529/)

  • internationalization
  • i18n
  • currency
  • form
  • field
  • babel
  • decimal
Read More

Currency Widget

This is a simple TextInput widget that uses my [Currency object](http://www.djangosnippets.org/snippets/1525/). I placed my Currency object in the Django utils directory because I integrated a set of currency objects into the Admin app ( [here](http://www.djangosnippets.org/snippets/1529/) ) and it was just easier to have everything within Django. The rest of the series: [Currency Object](http://www.djangosnippets.org/snippets/1525/), [Currency Form Field](http://www.djangosnippets.org/snippets/1527/), [Currency DB Field](http://www.djangosnippets.org/snippets/1528/), [Admin Integration](http://www.djangosnippets.org/snippets/1529/)

  • internationalization
  • i18n
  • currency
  • babel
  • decimal
Read More

Currency Object

This object stitches together the [Babel](http://babel.edgewall.org/) number formating and the Decimal object, with a little of my own hand rolled validation for parsing. Note the comment at the end of the code. It contains two lines to add to your settings.py. CURRENCY_LANGUAGE_CODE = 'pt_BR' CURRENCY_CODE = '' # If one exists like 'USD', 'EUR' UPDATE 06-03-2009: Now with rounding UPDATE 07-14-2009: Now with - More graceful handling of missing settings variables - Support for negatives (small oversight) - More flexible format strings - Thorough doctest tests UPDATE 07-30-2009: Added the parse_string argument to the `__new__()` method. This fixes a bug when importing data using `manage.py loaddata`. I have not yet updated the tests to reflect the change. The rest of the series: [Currency Widget](http://www.djangosnippets.org/snippets/1526/), [Currency Form Field](http://www.djangosnippets.org/snippets/1527/), [Currency DB Field](http://www.djangosnippets.org/snippets/1528/), [Admin Integration](http://www.djangosnippets.org/snippets/1529/)

  • internationalization
  • i18n
  • currency
  • babel
  • decimal
Read More

limit view request rate decorator

Limit rate request decorator for view. Authenificated user can't request decorated view often then timeout. Usage: @limit_request_rate(time_beetween_request_sec) def my_view(request): ... get_cell_value from [here](http://code.activestate.com/recipes/439096/)

  • django
  • cache
  • view
  • decorator
  • limit-request-rate
  • closure
  • cell
Read More

StaticField for non-changing text data in forms

This Field, Widget and the three lines of code in the form's clean method allow an easy (and hopefully general) way to add simple, unchangable text to a form. Example use: class OnlyTextForm(forms.ModelForm): points = StaticField(label=_('Points')) reviewer = StaticField(label=_('Reviewer')) def clean(self): for name, field in self.fields.items(): if isinstance(field, StaticField): self.cleaned_data.update({name: self.initial[name]}) return self.cleaned_data

  • forms
  • field
  • widget
Read More
Author: V
  • 1
  • 2

pavement file for deploying django projects

This is the pavement file I use to deploy a django site. It's in early stages. Right now it copies everything up to the desired server over scp. But I think I'll change it to rsync in the future. It requires pavement, and pexpect. The pavement file takes slight instruction from your settings.py file. For server information, and "lib_apps" to copy into the lib directory. An example of a settings file that I use with this pavement file: http://gitweb.codeendeavor.com/?p=django_empty.git;a=blob_plain;f=settings.py;h=23bda7d2a1eb2a52ca0859004ecccd206dade4ec;hb=5d672178dab282caeed5ff0de7ed807c72e44f74 Specifically, check out the bottom for two vars: "LIB_APPS" and "DEPLOYMENTS" A good example of my empty django project is here: http://gitweb.codeendeavor.com/?p=django_empty.git;a=tree;h=5d672178dab282caeed5ff0de7ed807c72e44f74;hb=5d672178dab282caeed5ff0de7ed807c72e44f74 I think the one thing that's missing is a way to re-spawn fcgi processes. Which I'll hopefully get around to adding sometime soon. Also, I need to do a little work at making sure source control files don't get pushed through scp.

  • django
  • python
  • deploy
  • paver
  • pavement
Read More

3109 snippets posted so far.