Login

Top-rated snippets

Snippet List

Cached Del.icio.us API Calls

This will save your del.icio.us bookmarks in your own database with a Bookmarks model. It depends on several things: 1. `DELICIOUS_USER` and `DELICIOUS_PASS` defined in settings.py 2. [pydelicious](http://code.google.com/p/pydelicious/) installed 3. Any view that uses del.icio.us should call the delicious() function, to update the database. 4. The [cache_function](http://www.djangosnippets.org/snippets/109/) decorator must be available. (I have it in a decorators.py file in my app; if you move it, you need to update the import) 5. The [django-tagging](http://code.google.com/p/django-tagging/) app, although it could be modified to not need this rather easily. Other than all those dependencies, it's actually rather simple. It will call del.icio.us for the 5 most recent posts, and create them if they're not already in the database. Otherwise, it'll check for updates, and change as appropriate. It won't return anything, as it updates the models themselves.

  • cache
  • delicious
  • pydelicious
  • api
  • bookmarks
Read More

Akismet Webservice

A short little bit of code to test for comment spam against Akimet. Use: a = Akismet('<AkismetKey>', 'http://sneeu.com/blog/') a.verify_key() print a.comment_check( comment_author='...', comment_author_email='[email protected]', user_ip='10.0.0.1', comment_content="""Comment content!""" )

  • rest
  • python
  • akismet
  • spam
  • webservice
  • comment
Read More

Keep settings.py in version control safely

Here is a trivial way to keep your Django project in shared version control or in a public repository without exposing settings that could have security implications, and without needing to modify settings.py for your local test or production environments on checkout. This is also a way to separate development settings from production settings, or to deploy the same code on multiple servers while only changing one site-specific "dotfile."

  • files
  • configuration
  • settings
  • development
  • debug
Read More

Simple profile middleware

Intall -------- In your settings.py, set it in MIDDLEWARE_CLASSES. Default all the profile files will be save in ./profile folder(if there is no this directory, it'll automatically create one), and you can set a PROFILE_DATA_DIR option in settings.py, so that the profile files can be saved in this folder. How does it works ------------------- Record every request in a profile file. As you can see in the code: profname = "%s.prof" % (request.path.strip("/").replace('/', '.')) so if you request an url multi-times, only the last request will be saved, because previous profile files will be overriden. And you can find a commentted line, it'll save each request in different file according the time, so if you like that you can change the code. # profname = "%s.%.3f.prof" % (request.path.strip("/").replace('/', '.'), time.time()) and if you want to see the output, you can run below code, but you should change the filename value: import hotshot, hotshot.stats stats = hotshot.stats.load(filename) #stats.strip_dirs() #stats.sort_stats('time', 'calls') stats.print_stats()

  • middleware
  • profile
Read More

Generating vCards using VObject

Use this code to generate downloadable [vCard][] objects. See the [VObject docs][1] for more details on the API. [1]: http://vobject.skyhouseconsulting.com/ [vcard]: http://en.wikipedia.org/wiki/VCard

  • vcard
  • view
Read More
Author: pbx
  • 6
  • 14

Memcache Status

I use this script to keep track of the cache usage for various sites. It presently only supports memcached because that's all that I use, but leave a comment with patches for other systems and I'll add them.

  • memcache
  • cache
  • status
Read More

Parsing and Highlighting &lt;code&gt; Blocks

This function takes a string (most likely from a template), searches it for `<code>[...]</code>`, highlights it with Pygments, and returns the entire thing back, as a string. (Note: the `<code>[...]</code>` must have a class corresponding to the language inside. If it lacks the class, then it's silently ignored.)

  • pygments
  • beautifulsoup
Read More

Contact Form

A simple way to get started using newforms. Implement a contact form. Mine saves the data in a table and sends a dedicated mailbox the feedback as well. I added support for TinyMCE as described in the django wiki. http://code.djangoproject.com/wiki/CustomWidgetsTinyMCE?format=txt Anyway use this as a starting point for writing your own form handling.

  • newforms
Read More

Readonly Tabluar Inline

An Image says more than 100 words: [readonlytabularinline.png](http://img39.imageshack.us/img39/6555/readonlytabularinline.png) Use `editable_fields` to exclude some fields from being readonly.

  • admin
  • tabular-inlines
Read More

Auto-documenting Django Models with Sphinx

In my sphinx documentation I really wanted a nice clean list of the fields on my Django models. The most obvious way of doing this is to add ":param blah:" tags to the docstring, but this takes a long time to implement and violates DRY principles when you already have lots of nice help text in the model definition. Another way is to use "#:" comments on attributes, but this approach suffers from the same issues as the previous method with the additional problem of Sphinx crapping out on file and custom fields. So anyway, this is my solution. It uses the Sphinx docstring processing callback to intercept any objects that inherit from django.models.Model and creates a nice param list of all the fields on that model. Param descriptions come from the field's help text or verbose name if no help text is defined. To use this, just add it to the end of your source/conf.py, filling out the project path as appropriate. You may be able to skip the Django environment setup lines if you're adding this to a Sphinx doc that already has Django models set up.

  • sphinx
  • introspection
  • documentation
  • docs
  • autodoc
Read More

Use email addresses for user name for django 1.3

[Chris' code](http://djangosnippets.org/snippets/1845/) adapted to django 1.3. Basicly E-mail authorisation backend. Put it as one of your AUTHENTICATION_BACKENDS in settings.py: AUTHENTICATION_BACKENDS = ( 'community.auth.EmailBackend', )

  • email
  • auth
  • backend
  • authorisation
Read More

3110 snippets posted so far.