A quick-and-dirty, and extremely simple, decorator to turn a simple function into a management command.
This still requires you to have the management directory structure, but allows you to name your primary method whatever you want, and encapsulates the basic functionality of an argument-accepting management commmand.
The function's docstring will be used for the command's help text if the `help` arg is not passed to the decorator.
Simple usage:
from myapp.utils import command
@command()
def my_command():
print "Hello, world"
I'm not too familiar with the intricacies of decorators and management commands, so this could probably (most likely) be improved upon, but it's a start.
**Update**:
I've taken this a bit farther and put my work up on bitbucket: https://bitbucket.org/eternicode/django-management-decorators/src
I found model definitions with large number of fields with long help_text unreadable and feel that help_text doesn't really belong to field definition. With this snippet help_text attributes can live outside field definitions in inner HelpText class so field definitions become shorter and more readable.
Usage:
from django.db import models
import readable_models
class MyModel(models.Model):
__metaclass__ = readable_models.ModelBase
name = models.CharField('Name', max_length=30)
address = models.CharField('Address', max_length=255)
# ... long list of fields
class HelpText:
name = 'The name ...'
address = 'Example: <very verbose example is here>'
Mechanism to obtain a `request.user` object without the `request` object itself. Requires `LocalUserMiddleware` in `MIDDLEWARE_CLASSES` settings variable.
**Important**: works under assumption that within a web server each request is handled by a separate thread (as for example in the Apache HTTP server).
**Beware**: [security threat](http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser), although ["thread locals only appears to be a security threat if a system has already been seriously compromised, at which point there'd be easier attacks to execute"](http://groups.google.com/group/django-users/browse_thread/thread/e7af359d7d183e04).
**Dev note**: works fine with one-threaded Django's development server, each request resets current user; no worries 'bout many media requests - they won't (at least shouldn't) be using Django on the production server.
**Ref**: originally found in the gatekeeper app.
This creates an admin interface for each model in the "your_appname" app. It also adds each column to the admin list view.
Place this in the admin.py of "your_appname".
This filter converts slashes to spaces in a a sting and then slugify's the result. However, it ignores leading and trailing slashes. For example, it can take something like this:
/some/url/with-an-existing-slug/
And turn it into this:
some-url-with-an-existing-slug
The filter was originally written to use the *curent* url as the `disqus_identifier` for Disqus comments. For example:
{{ request.META.PATH_INFO|slug_and_slash_to_dash }}
You can use this code to sign urls for streaming distributions or change it a bit and sign normal distribution's urls.
Available settings:
CLOUDFRONT_KEY - path to private key file
CLOUDFRONT_KEY_PAIR_ID - key pair id
CLOUDFRONT_EXPIRES_IN - expiration time in seconds
CLOUDFRONT_DOMAIN - domain name
The title says it all — a subclass of FileSystemStorage which will overwrite files.
Note that saves which fail part way though will leave the original file intact (see `test_upload_fails`).
Based roughly on http://djangosnippets.org/snippets/2044/ .
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/)