Login

All snippets written in Python

2956 snippets

Snippet List

send a simple Campfire chat message from fabric script

look ma, no api! a python method for [fabric](fabfile.org) script to send a message to your [campfire](campfirenow.com) chat room. not really a django script but I didn't know where else to put it. I use it to send a deployment messages to campfire when we deploy new revisions. like the comment mentions, put your api key in ~/.fabricrc. the example api key is garbage so don't waste your time.

  • basecamp
  • campfire
  • chat
  • fabric
Read More

Bypass CSRF check for Facebook canvas apps using POST for canvas

This assumes that you have a method called **decode_signed_request** which will validate the signed_request parameter and return None if the validation check fails. A similar method can be found here - https://github.com/iplatform/pyFaceGraph/blob/70e456c79f1ac1c7eddece03af323346a00481ef/src/facegraph/canvas.py

  • django
  • python
  • post
  • facebook
  • csrf
  • fb
Read More

Templatetag to manage GET arguments in template

Example usage: Add static var with static value to get : {% urlget 'var'='val' %} Add dynamic val (from template vars) to static variable: {% urlget 'var'=val %} Using dynamic variable names works similiar - adding dynamic varialbe (from template vars) : {% urlget var='val' %} Clearing variable from GET string : {% urlget 'var'='' %} Retrieving GET string: {% urlget %}

  • get
  • tempatetag
  • urlget
Read More

Analogue template filter to removetags that also removes the content of the tag

Django's builtin `removetags` filter removes the supplied tags, but leaves the enclosed text alone. Sometimes you need the complete tag, including its content to go away. Example: <h1>Some headline</h1> <p>Some text</p> Applying `removetags:"h1"` to this html results in Some headline <p>Some text</p> while `killtags:"h1"` leaves <p>Some text</p>

  • filter
  • removetags
Read More

Sane OneToOne getter

Since django insists on throwing a DoesNotExist rather than just returning a None from the far side of a null OneToOneField, I wrote this to have a sane way of getting those fields out without having to try/except all over in my code.

  • DoesNotExist
  • OneToOne
  • OneToOneField
Read More

LogTrace

This small decorator will trace the execution of your code every time it enters or exits a decorated function (by thread) and will insert appropriate indent into the log file along with exception information.

  • decorator
  • logging
  • hax
  • trace
Read More

New view decorator to only cache pages for anonymous users

This is an addition to Django's cache_page decorator. I wanted to cache pages for anonymous users but not cache the same page for logged in users. There's middleware to do this at a site-wide level, and it also gives you the ability to partition anonymous users, but then you have to tell Django which pages not to cache with @never_cache.

  • cache
  • view
  • decorator
  • anonymous
Read More

RelatedNullFilterSpec: django-admin custom filter all/null/not null/choices

A simple django-admin filter to replace standar RelatedFilterSpec with one with the "All"/"Null"/"Not null" options. It applies to all relational fields (ForeignKey, ManyToManyField). You can put the code in the `__init__.py` or wherever you want. The `_register_front` idea is copied on [this snippet](http://djangosnippets.org/snippets/1963/)

  • filter
  • admin
  • null
  • filterspec
  • ManyToManyField
  • ForeignKey
  • not-null
Read More

Custom FileField with content type and size validation

Usage described in this blog post: [Django: FileField with ContentType and File Size Validation](http://nemesisdesign.net/blog/coding/django-filefield-content-type-size-validation/) Snippet inspired by: [Validate by file content type and size](http://djangosnippets.org/snippets/1303/)

  • forms
  • validation
  • upload
  • filefield
  • file
  • content-type
  • max_upload_size
Read More