Login

Snippets by jgeewax

Snippet List

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

YouTube Template Filter

Given a youtube url (can copy and paste right from the browser) turns the url into an embedded video. If you put this in your app's templatetags/filters.py, you can do the following >{{ object.youtube_url|youtube }} and it would embed the youtube video.

  • template
  • filter
  • youtube
  • embed
Read More

View Permission Decorator Helper

This is a simple helper to make custom permission decorators for Django views. Perhaps you have an edit_comment view which you want to make sure current user is the owner of: >def edit_comment(request, comment_id): >>if request.user == Comment(id=comment_id).user: >>>... do authorized things ... >>else: >>>... do unauthorized things ... >>... >>... In this view, you might do a quick check `if request.user == Comment(id=comment_id).user`, however you now need to duplicate this code all over the place whenever you want to check if a comment is owned by the current user. Instead, you can use the built in login_required decorator, and your own decorator to do the test: >@permission >def user_owns_comment(request, comment_id): >>return request.user == Comment(id=comment_id) > >@login_required >@user_owns_comment >def edit(request, comment_id): >> ... >> ... >> ... The "tester" function will post a message using the messages module built into Django, and redirect the user to the root. It allows access and executes the view if the tester function returns anything that evaluates to True. Your permission tester should either strictly specify the same arguments as the view, or take additional *args, and **kwargs to prevent syntax errors on extra arguments being passed along.

  • view
  • decorator
  • permission
Read More

Dynamic Template Loader

This snippet provides support for dynamically importing templates. This helps you to avoid naming collisions and other problems. The format is as follows: 1. `module.submodule.app:template.html` 2. `module.submodule.app:subfolder/template.html` 3. `module.submodule.app:/subfolder/template.html` Assuming the module is located in '/var/', these would map (respectively) to: 1. `/var/module/submodule/app/templates/template.html` 2. `/var/module/submodule/app/templates/subfolder/template.html` 3. `/var/module/submodule/app/templates/subfolder/template.html` The colon splits the the python module from the template directory, meaning you can import from anything that has a "templates" directory. This helps me to avoid naming collisions by specifying the application I'm referring to, without having to put long paths in my extends and include tags inside other templates. It's also dynamic in that if I move a library outside the old path, it has no effect on the templates. To get this rolling, in your `settings.py` file, add the following:: >TEMPLATE_LOADERS = ( > 'addons.template_loader.load_template_source', # <--- add this > 'django.template.loaders.app_directories.load_template_source', > 'django.template.loaders.filesystem.load_template_source', ># 'django.template.loaders.eggs.load_template_source', >)

  • template
  • dynamic
  • import
  • loader
Read More

jgeewax has posted 4 snippets.