A simple template filter for parsing tweets (linking @ replies, hashtages and standard URLs whilst stripping out links to yFrog and TwitPic images), and two simple tags for displaying the yFrog and TwitPic images linked to in tweets.
If you put the snippet in a file called tweets.py, as long as it lives in an app's templatetags directory, you should be able to do the following (where `text` refers to the text of a tweet):
{% load twitter %}
<p>{{ text|tweet }}</p>
{% yfrog_images text 1 'fancybox' %}
{% twitpic_images text 1 'fancybox' %}
The first argument in the two tags controls how many images to render. Set this to -1 for an unlimited number, per tweet.
Thumbnail images are displayed, and you can specify the class that is applied to the `<a>` tags rendered. Here I've used 'fancybox', and I would normally include jQuery code to turn the images inside the `<a>` tags into lightboxes.
Using jQuery UI (with Grappelli in use) to add "drag and drop" reordering of items in the admin list view. The model must have an "order" field to store the order value in.
This snippet is based on [Bill Freeman's MultiSelect checkbox iterator template filter](http://djangosnippets.org/snippets/2151/).
Usage: See docstring
if_installed checks to see if the app is in installed apps. If it is not then it excludes it from being resolved in the url structure. In this example, myapp.urls will not be imported if myapp is not installed
So you need to change some settings when running an individual test in a test case. You could just wrap the test between `old_value = settings.MY_SETTING` and `settings.MY_SETTING = old_value`. This snippet provides a helper which makes this a bit more convenient, since settings are restored to their old values automatically.
Example usage:
class MyTestCase(TestCase):
def test_something(self):
with patch_settings(MY_SETTING='my value',
OTHER_SETTING='other value'):
do_my_test()
Put this snippet in a file in a templatetags/ directory and load it in your templates. Then use it to encode your emails:
`{{"[email protected]"|html_encode_email}}`
Or if you want some control over the anchor tag:
`<a href="mailto:{{"[email protected]"|html_encode}}&subject=Feedback">Send Feedback</a>`
From [fahhem.com](http://fahhem.com/) and [Recrec Labs](http://recreclabs.com/)
Because BaseCommand catches all CommandError exceptions and turns them into nicely formatted strings before calling sys.exit(1), it can be tricky to properly test failure.
Normally, this is great, but it makes testing that a command fails when we expect to a little harder. That's where this snippet comes in. It redirects sys.stderr to an instance StringIO where we can monitor what's output and catches the BaseException raised by sys.exit(1). Form here, it's trivial to test that a management command fails exactly as you'd expect it to.
I have many heavy views which run slowly when accessed at same time in multiple threads. I make this decorator to allow run only one view at the time and cache returned result. Other threads will wait to complete first thread and use response from the cache if executed thread put it to cache.
Also I take idea of MintCache to refresh staled cache and return cached (stale) response while response refreshed in the cache.
Usage:
@single_cacheable(cache_timeout=60,
stale_timeout=30,
key_template='my_heavy_view-{arg1}-{arg2}')
def heavy_view(request, arg1, arg2):
response = HttpResponse()
... your code here
# cache timeout may be set from inside of view
response._cache_timeout = cache_time
return responce
The "key_template" is a template for cache key. Some my views have additinal parameter "cache_time" which set from parent page and request.path is different for these pages but they need to be cached not depending of this parameter.
Variable "key_template" in the example used named agrument, if you have no named paramaters you need to use 'my_heavy_view-{1}-{2}-{...}' where {1},{2}, {...} arguments of view.
The line with setting "key" variable may be changed to:
key = "sc_"+request.get_full_path()
if you need to take full URL path as cache key.
Sometimes you want more rendering control than common widgets give you. We wrote this, for example, to allow rendering the checkboxes of a multi select field with checkbox widget, as multiple columns.
Place this in the templatetags subdirectory of an app you are loading (be sure it has a __init__.py file), and load it something like this (you may want to shorten the name):
{% load checkboxiterator_filter %}
Then you can say something like:
{% for checkbox in form.categories|checkboxiterator %}
{# other code #}{{ checkbox }}{# other code #}
{% endfor %}
where form.categories is a multiple select field (no doubt this technique could be applied to radio buttons and a single select field).
The filter does use some internal interfaces, but may well still work on newer Django version.
Just a dump of a countries data, including standard codes & dial-codes, based on the following model:
- Country
-- name : CharField
-- code : CharField
-- dial_code : CharField
Note that countries aren't unique, as some may have several intl. dial codes.
You can parse it using script or load it using the loaddata command.
Ok, I just took [wolever](http://djangosnippets.org/users/wolever/)'s snippet at [http://djangosnippets.org/snippets/1926/](http://djangosnippets.org/snippets/1926/) and added template variable/filter support for range values.
For instance, you could write something like this:
`{% mkrange 1 tool_page.paginator.num_pages|add:"1" as page_range %}`
I'm just learning python and django, so any advice would be appreciated.
A Django form widget which displays help text for individual items in a set of radio buttons.
It overrides the RadioSelect widget, adding a small bit of HTML after each <input> element, with the help text for each item.
It was developed for a Django Dash project I'm working on (called transphorm.me), so isn't as feature-rich as it could be, but if you have any trouble installing it - or if I've miscopied any of my code in my rush - please let me know.
This is the snippet of rafacbd but upgraded... (http://djangosnippets.org/snippets/955/)
Now support keep the aspect ratio or not, changing the size string (x1, x0) this choice use pil.resize or pil.thumbnail.
Remember change the variable CAPS NAMES of the cleanup code.
This cleanup function get the original main image file name and create a regexp similar to "[filename]_[width]x[height]x[ratio].[ext]" and remove all thumbs.
By example if the main image is spain_rulez.jpg the script remove by example spain_rulez_100x100x1.jpg spain_rulez_200x150x0.jpg etc... etc...
Use this if you want to check if commenting is allowed for a certain object/model/... in a template. Like this;
`{% if not object.comments_allowed %}
<p>Comments are now closed.</p>
{% else %}
{% render_comment_form for object %}
{% endif %}`
You can replace the CommentModerator class with your own custom moderation class ofcourse.