Login

All snippets

Snippet List

Update All Apps to Latest Revision

This snippet is based on [#844](http://www.djangosnippets.org/snippets/844/ "#844") and [#892](http://www.djangosnippets.org/snippets/892/ "#892") and updates all apps in the current directory using hg, svn, git or bzr. Including subdirectories not under version control (subfolders to keep your stuff organized). For example: python/lib/ django-trunk/ django-0.96/ pydelicious/ (...) django-apps/ django-tagging/ django-pagination/ django-registration/ django-threadedcomments/ django-mptt/ (...) The script will iterate through all of your apps (in the current dir and also recursively in subdirs NOT under version control) and update them to the latest version. To run, simply execute: python update_apps.py in the desired parent folder. Just in case it could be useful: In my case I'm using MAC OS X. I have a folder full of miscellaneous scripts under my HOMEDIR, with this content: /Users/Dedaluz/bin/update_apps.py /Users/Dedaluz/bin/update_apps (this is a bash script) The update_apps script contains simply: #!/bin/bash python /Users/Dedaluz/bin/update_apps.py Then I put this folder in my path, so in my /HOMEDIR/.bash_profile I add this line export PATH=$PATH:$HOME/bin And I just can update from any parent folder just going there and typing: update_apps

  • script
  • update
  • svn
  • git
  • hg
  • bzr
Read More

syncdata command

A django admin command that takes a fixture and makes the target database the same as that fixture, deleting objects that in the database but not in the fixture, updating objects that are different in the database, and inserting missing ones. Place this code in your_app/management/commands/syncdata.py You will need to use manage.py (not django-admin.py) for Django to recognise custom commands (see http://www.djangoproject.com/documentation/django-admin/#customized-actions). This snippet is the 'loaddata' command with this patch applied: http://code.djangoproject.com/ticket/7159 (with minor tweaks). The intention is that 'dumpdata' on system A followed by 'syncdata' on system B is equivalent to a database copy from A to B. The database structure in A and B must match.

  • admin
  • database
  • import
  • commands
  • copy
  • command
  • synchronise
  • publish
Read More

[middleware] Rewrite anchors to point into Coral CDN

This simple middleware rewrites the 'href' attribute of any `<a>` tags in your response content. The URL href is modified by appending the string '.nyud.net', which causes the Coral Content Distribution Network to retrieve a copy of the page and cache it before returning it to the user agent. This might be useful if you're writing another Slashdot and you want to avoid turning the servers you link to into smoking craters. You [should be able](http://www.djangosnippets.org/snippets/910/) to apply this functionality to a single view as well (though I haven't tried this yet).

  • middleware
  • coral
  • cdn
  • anchor-rewrite
Read More

Serializing booleans correctly when doing dumpdata from a MySQL database using Django 0.96

Django 0.96 seems to have a bug when serializing from MySQL. BooleanFields are encoding as 0/1 instead of true/false. Hacking the python serializer seems to fix that. The bug shows up as (fx. when using loaddata on a dump from MySQL in PostgreSQL): Problem installing fixture '/tmp/data.json': ERROR: column "is_staff" is of type boolean but expression is of type integer HINT: You will need to rewrite or cast the expression.

  • dumpdata
  • 0.96
  • seralization
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

Load a local settings file for dev/test environments

Add the snippet to your settings.py. If you have a settings_local.py it will load that one. Can be used in development environments where you might have different settings for your dev sandbox. You should exclude settings_local.py from SVN. By Rudy and Ed Menendez

  • settings
  • environment
  • production
Read More

Delete View

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

  • view
  • delete
Read More

Command to make fixtures.

"Make fixture" command. Highly useful for making test fixtures. Use it to pick only few items from your data to serialize, restricted by primary keys. By default command also serializes foreign keys and m2m relations. You can turn off related items serialization with `--skip-related` option. How to use: python manage.py makefixture will display what models are installed python manage.py makefixture User[:3] or python manage.py makefixture auth.User[:3] or python manage.py makefixture django.contrib.auth.User[:3] will serialize users with ids 1 and 2, with assigned groups, permissions and content types. python manage.py makefixture YourModel[3] YourModel[6:10] will serialize YourModel with key 3 and keys 6 to 9 inclusively. Of course, you can serialize whole tables, and also different tables at once, and use options of dumpdata: python manage.py makefixture --format=xml --indent=4 YourModel[3] AnotherModel auth.User[:5] auth.Group

  • serialize
  • admin
  • model
  • fixtures
  • tests
  • test
  • management
  • commands
  • fixture
  • command
  • make
Read More

Type less with newforms admin

I recently converted a site with over 60 models to newforms admin. I like the seperation of the display from the defintion, but it does introduce quite a bit more typing which isn't in the spirit of Django DRY... I particular I got bored of typing admin.site.register(Model, ModelAdmin) Over and over again so I wrote this little bit of code which does a bit of introspection. It assumes that 1. You import all your models into your admin.py, ie from "myapp.models import *" 2. The admin class for Model is called ModelAdmin Put this snippet at the end of your admin.py I hope that saves someone a bit of typing!

  • admin
Read More
Author: ncw
  • -4
  • 4

change a widget attribute in ModelForm without define the field

I will change a model form widget attribute without define the complete field. Because many "meta" information are defined in the model (e.g. the help_text) and i don't want to repeat this. I found a solution: Add/change the widget attribute in the __init__, see example code.

  • newforms
  • forms
  • field
  • modelform
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

FilteredSelect

Displays as an ordinary selectbox with an additional text-input for filtering the options. An adaption of the example at [1] for use with Django. The code assumes the java script from [2] is downloaded into your media folder. Remember to output your form's media, for instance like: "{{form.media|safe}}", somewhere above your form. **License:** [2] by Mr Patrick Fitzgerald is licensed under GPL and the python code written by myself is GPL as well. **References:** 1. http://www.barelyfitz.com/projects/filterlist/index.php/ 2. http://www.barelyfitz.com/projects/filterlist/filterlist.js

  • javascript
  • widget
Read More

Using Google Apps Premium infrastructure for user management

To create a lower entry barrier to logging into our intranet I created a very simple backend using Google Apps Premium provisioning API (http://code.google.com/apis/apps/gdata_provisioning_api_v2.0_reference.html). This enables controlling access for your users based on their status in your Google Apps instance. *(NOTE! Since the provisioning API is only available in the Premium version of Google Apps you first need to upgrade if you haven't done so already)* Requirements: You need Google Data libraries for python. They can be downloaded from http://code.google.com/p/gdata-python-client/downloads/list Google Apps Premium -- User used by the script must have admin rights

  • authentication
  • login
  • auth
  • google
  • google-apps
  • auth-backend
Read More

3109 snippets posted so far.