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.
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.
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!
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 %}
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 %}« 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 »{% if next_url %}</a>{% else %}</span>{% endif %}
</div>
{% endif %}
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.
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/)
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/)
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/)
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/)
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/)
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
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.