Login

Top-rated snippets

Snippet List

vary_on_user

@vary_on_user doesn't work properly if your site uses third-party cookies, since the cache key is created from *all* of the cookies in the request header. This decorator caches pages based on the user ID, so it works reliably.

  • decorator
  • vary_on_cookie
Read More
Author: TAH
  • 0
  • 0

Email Auth

Yet another authentication by email address. This one is quick and dirty as we are saving email address in both Username and Email fields. For proper way how to deal with it see https://docs.djangoproject.com/en/1.6/topics/auth/customizing/#auth-custom-user

  • email
  • auth
Read More

File Mimetype Validator (Using python-magic)

This validator works well with FileField form fields and can validate that an uploaded file has an acceptable mimetype. Place this snippet in your app's `validators.py`. Requirements: This snippet uses [python-magic](https://github.com/ahupp/python-magic). To install: pip install python-magic Usage (in forms.py): from validators import MimetypeValidator class MyForm(forms.Form): file = forms.FileField( allow_empty_file=False, validators=[MimetypeValidator('application/pdf')], help_text="Upload a PDF file" )

  • validation
  • upload
  • magic
  • mimetype
  • FileField
Read More

JSON serializer supporting natural primary keys

Copy this file into `your_app/serializer.py`, and add this to your settings: SERIALIZATION_MODULES = { 'json': 'your_app.serializer', } Now you can dump your models with the classical `dumpdata -n` command and load it with `loaddata` and get support for natural primary keys and not only with foreign keys and many to many fields.

  • serializer
Read More

Display values from a bound (submitted) form

Function that takes a bound form (submitted form) and returns a list of pairs of field label and user chosen value. It takes care of: 1. fields that are not filled out 2. if you want to exclude some fields from the final list 3. ChoiceField (select or radio button) 4. MultipleChoiceField (multi-select or checkboxes) Usage: if form.is_valid(): form_data = get_form_display_data(form, exclude=['captcha']) It's trivial to display the list of pairs in a template: {% for key, value in form_data %} {{ key }}: {{ value }}{% endfor %}

  • template
  • email
  • form
Read More

autogenerated UUID model field

Provides UUIDField for your models. This version creates very short UUID represenation (21 chars) when the record is added eg. in admin. Generated ids are safe to be used in URLs. You can put represent it in admin as 'readonly_fields=("uuid",)'

  • model
  • field
  • uuid
Read More

Paypal form with better sandbox support

This is a class I use instead of the default PayPalPaymentsForm in django-paypal. I wanted to use the Paypal sandbox on my development site and the real Paypal site on my production site. Currently, if you want to output the Paypal form with the sandbox you have to call the sandbox() method on the form, rather than the render() method. Using this class instead of the default PayPalPaymentsForm, you can just set PAYPAL_DEBUG to True in the settings file and the form will take you through to the sandbox instead. Don't forget to set the sandbox merchant account's business email address as PAYPAL_RECEIVER_EMAIL too.

  • paypal
Read More

A more complete Drupal 7 compatible password hasher

Use this class to authenticate against Drupal 7 password strings. When importing passwords from Drupal, the database values should be prefixed with "drupal$". In contrast to the two present solutions, this class also works with passwords which were imported to Drupal 7 (e.g. from Drupal 6 or phpBB) and with passwords with variable iteration numbers. It is possible to use this class for encoding passwords, but due to questionable design decisions in Drupal (like truncating the non-standard base64 encoded hash at 43 characters) I'd recommend to do this this only if you really need to be able to migrate back to Drupal.

  • drupal
  • drupal7
Read More

Haystack whoosh backend for stemming non-english language words

It's haystack whoosh backend code which involves stemming for specific language in time of indexing. 2 lines was changed comparing to original whoosh backend (StemmingAnalyzer replaced with LanguageAnalyzer for russian language, list of supported languages: [here](https://bitbucket.org/mchaput/whoosh/raw/default/src/whoosh/lang/snowball/__init__.py) )

  • haystack
  • stemming
  • whoosh
Read More

3110 snippets posted so far.