Login

Most bookmarked snippets

Snippet List

Decorator for authenticating token based API calls

Uses the token generator located at django.contrib.auth.tokens as an authentication mechanism aimed mainly at API calls. Any POST request with a valid token and user parameter will work as if the user were logged in normally.

  • decorator
  • login
  • auth
  • token
Read More

View to retrieve objects meeting a complex tag query

This view parses complex tag queries. It generates a list of model instances matching an expression of tags. The expression currently supports intersection, union and subtraction. Expressions can also be put in parenthesis and then combined with other expressions. The expression must be passed to this view in the tag_expression argument. In my application this is simply passed from the URL querystring. This snippet uses the django-tagging app. It assumes that tags are composed of alphanumeric characters, underscores, hyphens and spaces, but the django-tagging application allows tags with other characters, so you might either want to restrict users to using tags that only contain the above characters, or you might prefer to improve this snippet. Example: This URL http://example.com/people/?(deceased&parrot)|"Monty Python" will retrieve all people who are either deceased parrots or members of Monty Python. In the tag_expression argument: * ALL is treated as a keyword. If you happen (by some sad chance) to have a tag called ALL and want to use it in the expression, surround it in quotation marks. E.g. "ALL" * Examples: - famous -returns all instances of the model tagged with famous - famous&deceased -returns all instances of the model tagged both famous and deceased. - famous|deceased -returns all instances of the model tagged famous or deceased. - parrot-deceased -returns all alive parrots in the model. - ALL-deceased -returns all instances of the model that are not tagged deceased. - ALL -returns all instances of the model - "ALL" -returns all instances of the model that are tagged ALL - "great author"&deceased -returns all models tagged as great authors and deceased. Arguments: * request -- HTTP Request object * tag_expression -- a set expression of tags, supporting intersection, union, parenthesis and difference * app_name -- App for the model we're working on (defaults to pubman) * model_name -- Model on which to apply the set operations (defaults to Article) * view -- view to redirect to after the model instance list has been constructed * html_template -- HTML template to redirect (defaults to 'pubman/tag.html')

  • view
  • django-tagging
  • tag expression
Read More

notify admin what fields have changed in form submission

here is some working code from a site that maintains profile information (address, phone number, etc) for users ("Partners"). this snippet handles the submitted form (after validation) and checks to see if any fields have been changed by the partner. if so, it shoots off an email to the admin showing the current profile information with changed fields marked with asterisks.

  • fields
  • forms
  • changed
Read More
Author: pjv
  • 0
  • 2

Semi-Portable recaptcha integration with django forms

It is not so portable and easy as I wanted it to be because of how django forms work - they don't play well with recaptcha. To get it to work: * Add two variables to your app settings, **RECAPTCHA_PUBKEY** and **RECAPTCHA_PRIVKEY** * Derive forms you want to have a captcha from the provided `ReCaptchaForm` class (how to get it working with ModelForm? any ideas?) * * If you override the form's clean method make sure you firstly call the `ReCaptchaForm`'s clean method * * In your view, upon receiving the form data initialize the objects like this `form = YouFormClassDerivedFromReCaptchaForm(remoteip=request.META['REMOTE_ADDR'], data=request.POST)` (or request.GET of course) - this is because reCaptcha needs the user's remote ip.

  • forms
  • captcha
  • recaptcha
Read More

Bit.ly url shortener

A small function to convert a url to another shortened via the bit.ly service. Requires a username and password in django settings.

  • bit.ly
  • rest-api
  • url-shortening
Read More

slugify with transliteration

This slugify correctly transliterates special characters using the translitcodec package from PyPI. Make sure you've installed http://pypi.python.org/pypi/translitcodec/ before using this.

  • slug
  • slugify
  • special chars
  • trans
  • transliteration
  • umlauts
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

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

Improved command to generate UML diagrams of whole project or specified apps

example of use: 1. python manage.py yuml yourapp yoursecondapp --scruffy -s 75 -o test.png 2. python manage.py yuml justoneapp --scruffy -o test.pdf 3. generate whole project yuml : python manage.py yuml -a -o test.jpg 4. python manage.py yuml auth contenttypes sessions admin -o test.pdf [github repository](http://github.com/dzhibas/django-yuml)

  • django
  • command
  • basecommand
  • uml
  • yuml
Read More

ReportBug() with tons of debug in mail

This basically takes the debug you get from setting debug=True, but instead, pipes it into an email and sends it over to you. I have extracted this out of our de framework, it should work, but some modifications may be necessary.

  • mail
  • report
  • bug
  • mail_admins
  • reportbug
Read More

Export Django data to datestamped tarball -- choose individual models for handy packaging and archiving

Just like it says -- set it up and run. Use it for server migrations, for project handoffs, in cron jobs, you name it. I have never had problems exporting models to individual fixtures in this way, and only one bout of trouble re-importing them (and that was, like, an impossible-triangle of OneToOneField dependencies anyway, and they were going from a sqlite file to a postgres schema that totally had inappropriate nullable columns in it). I find that the json files named for what they contain is helpful when and if manage.py does freak out during an import, as the output from `loaddata` command is so opaque it's autistic, basically. A trivial refactoring effort could make it into a management command -- it already makes use of the builtin `dumpdata` command class internally. However I did not feel like overthinking it enough to set it up in a repository (doubtlessly padded with unrelated 'utilities' and explanatory .rst files) and then writing a blog post to sell it to you. That is why you are reading this code here, instead of on GitHub. Don't get me wrong, GitHub is awesome, and like a consummate host... but not the way I love me some quick and dirty snippet code, these days. Whatever, you say lazy, I say productively relaxed, potato/potahto. Erm. In any case please do enjoy this model fixture-exporter. Yes.

  • django
  • python
  • json
  • export
  • data
  • script
  • command
  • archive
  • django1.1
  • backup
  • datestamp
  • tar
  • tarball
Read More

3110 snippets posted so far.