Login

Most bookmarked snippets

Snippet List

Unfuddle-style post-commit emails - tied to a specific Django project.

I needed a quick way to send e-mails to other people involved in a project when I committed code - so that designers would know that templatetag *x* was available (or fixed), etc - and am really fond of how [unfuddle](http://unfuddle.com/) handles their subversion notifications. This isn't nearly as polished as theirs, but I figured it was a good place to start. Hope someone else finds this as handy as I have.

  • email
  • svn
  • subversion
  • post-commit
Read More

Cookie based flash errors and notices (a la Rails)

This is a light-weight flash implementation. Instead of hitting the database it uses cookies. The messages are shown to the user only once, after that the cookies are deleted. I tested it on Google App Engine, but it should work on vanilla Django as well, there's no GAE specific code. To set up, add `"path.to.flash.Middleware"` to the `MIDDLEWARE_CLASSES` list. Also add `'path.to.flash.context_processor'` to the `TEMPLATE_CONTEXT_PROCESSORS` list. In your views, import and call `flash_error(msg)` and `flash_notice(msg)` passing the message that you want to show. In your base template use this mark up: {% if flash.notice %} <div id="flash_notice"> <p>{{ flash.notice }}</p> </div> {% endif %} {% if flash.error %} <div id="flash_error"> <p>{{ flash.error }}</p> </div> {% endif %} And finally, add this to your CSS file changing colours as necessary: #flash_error { margin-top: 30px; padding: 20px; background-color: #FFCCCC; border: solid 1px #CC0000; } #flash_notice { margin-top: 30px; padding: 20px; background-color: #CCFFCC; border: solid 1px #00CC00; } #flash_error p, #flash_notice p { margin: 0px; } Please comment if you notice any FUs. I'm new to Django and will appreciate any feedback.

  • error
  • flash
  • rails
  • notification
  • notice
Read More

Decorator cache handler per view

The @cache_holding decorator works in a per-function base, you can specify a list of models or just a model and the second argument is the time in seconds to be cached. You can modify the proxy class by a keyword argument so you can do cache to all kind of things, also if you want to use a prefix as key + the hash value you can specify the prefix keyword argument.

  • cache
  • view
  • decorator
Read More

Integer Currency Input

This accepts values such as $1,000,000 and stores them to the database as integers. It also re-renders them to the screen using the django.contrib.humanize.intcomma method which takes 1000000 and turns it into 1,000,000. Useful for large currency fields where the decimals aren't really necessary.

  • newforms
  • currency
  • field
  • integer
  • input
Read More

Autogenerate admin classes in admin.py

Tired of adding admin classes to admin.py whenever you add a model? This admin.py automatically keeps itself up-to-date with your models.py file. It assumes if you have a model: MyModel, you want an admin class called AdminMyModel. Regards, Luke Miller

  • django
  • models
  • admin
Read More

is_staff decorator

Decorator for views that checks that the user is staff, redirecting to the log-in page if necessary. A wrapper for user_passes_test decorator based on login_required Possible usage: @is_staff def view.... urlpatterns = patterns('', (r'^databrowse/(.*)', is_staff(databrowse.site.root)), )

  • decorator
  • auth
Read More

auto image field w/ prepopulate_from & default

Given such code: class ProductImage(models.Model): fullsize = models.ImageField(upload_to= "products/%Y/%m/%d/") display = AutoImageField(upload_to= "products/%Y/%m/%d/",prepopulate_from='fullsize', size=(300, 300)) thumbnail = AutoImageField(upload_to="products/%Y/%m/%d/",null=True,default='/media/noimage.jpg')) display will be automatically resized from fullsize, and thumbnail will default to /media/noimage.jpg if no image is uploaded Note: At some point the code broke against trunk, which has now been updated to work properly

  • image
  • models
  • db
  • field
Read More

Automatic stripping textual form fields

Here is a class decorator that allows not to bother with stripping leading and trailing white space from user input provided via forms. This could be a temporary solution for an issue addressed in the ticket [#6362](http://code.djangoproject.com/ticket/6362). The documentation is provided in the form of doctest. The decorator works with `ModelForm`'s just as well as with ordinary forms. Note however that this is not a 100% panacea. Your models still could become malformed if theirs data is obtained from another source, not forms.

  • newforms
  • forms
  • strip
  • auto
Read More

Smarter USPhoneNumberField

The original USPhoneNumberField only validates xxx-xxx-xxxx values. This field validates... > (xxx) xxx xxxx > xxx-xxx-xxxx > (xxx)-xxx-xxxx > many others. **Explanation of the regular expression:** 1. Accepts either (xxx) or xxx at the start of the value. 2. Allows for any amount of dashes and spaces (including none) before the next set of digits. 3. Accepts 3 digits 4. Allows for any amount of dashes and spaces (including none) before the next set of digits. 5. Accepts 4 digits, finishing the value. This field saves in the same format that the original USPhoneNumberField does (xxx-xxx-xxxx). Enjoy!

  • forms
  • validation
  • field
  • phone
Read More

RefreshSessionMiddleware

This middleware refreshes the session before it expires to avoid dropping the session of an active (but read-only) user. By default it refreshes the session after half the expiry time has elapsed. (This middleware does nothing for browser-length sessions.)

  • sessions
  • refresh
Read More

isUnique validator for newforms

This is a generic unique field value validator for use with newforms. ( It's handy to plug into newforms-admin.) Example, with newforms-admin: ` class LinkAdminForm( ModelForm ): def clean_url( self ): return isUnique( self.instance, 'url', self.cleaned_data['url']) class LinkAdmin( ModelAdmin ): form = LinkAdminForm site.register( Link, LinkAdmin ) `

  • newforms
  • admin
  • unique
Read More

Delete View

Usage: @login_required def action_delete(request,object_id): return delete_view(request,object_id,ActionItem)

  • view
  • delete
Read More

phpbb (2.x) authentication backend

This class not only checks an old-style phpbb 2.x password, when the user successfully logs in, it rehashes the (correct) password in the newstyle hash and saves it. Eradicating the old, quite unsafe stored md5 password.

  • authentication
  • backend
  • phpbb
Read More

3110 snippets posted so far.