Login

Snippets by grillermo

Snippet List

Instructions and code to use drupal 7 passwords

This is another fork of http://djangosnippets.org/snippets/2729/ that fixes the issue. Unlike those other versions i give you instructions so it works for you, this is modified a little. Instructions: If you want to import the passwords from drupal you need to prepend to each of them the word drupal so it looks like this: drupal$S$DQjyXl0F7gupCqleCuraCkQJTzC3qAourXB7LvpOHKx0YAfihiPC Then add this snippet to someapp/hashers/DrupalPasswordHasher.py And then in your settings add this: PASSWORD_HASHERS = ( 'django.contrib.auth.hashers.PBKDF2PasswordHasher', 'someapp.hashers.DrupalPasswordHasher', 'django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher', 'django.contrib.auth.hashers.BCryptPasswordHasher', 'django.contrib.auth.hashers.SHA1PasswordHasher', 'django.contrib.auth.hashers.MD5PasswordHasher', 'django.contrib.auth.hashers.CryptPasswordHasher', ) So, what did i modify First i added the attribute algorithm to the class, django uses this word to identify wich hasher to use, so the passwords beggining with drupal should be verified with the hasher.algorithm == 'drupal' Ok so know django knows to use our class, but now our passwords won't validate because we changed them by adding the word drupal, so what do we do? we modify the verify method to remove the word drupal before verification :P Hope it helps

  • django
  • password
  • drupal
Read More

Trim the center of a string

I had to build unique strings for a payment system and i wanted to make them kindof friendly so i generated them with usernames and datetimes(safe enough uniqueness in combo), some usernames are long and they break the limit of this payment system so i thought i should cut the center of the string so it stills has a part of the username and a part of the datetime, the most changing part of the datetime is of course the last part, as microseconds vary rapidly. So i wrote this little function to cut the center of a string i thought it cute so i leave it here. Pay attention to the comment so you can see what is going on.

  • python
  • string
  • slice
Read More

Expand(flatten) url patterns

This is a small function for those time when you want a list of all your urls, expanding included urls, so in the end is like all your urls are in one module. This function recursively does it, so it doesnt matter how nested the includes are in the end you get one flat list.

  • urlpatterns
Read More

Amazon S3 browser-based upload form(FIXED)

This is a 'fixed' version of snippet [1868](http://djangosnippets.org/snippets/1868/) Changes: *Correctly handle the Content-Type, because amazon requieres it to be named with a dash and we can't use dashes in the form attributes declaration. *Also added max_size handling, with the corresponding update to the policy generation. *Added an example usage with some javascript for basic validation. [See the amazon reference](http://aws.amazon.com/articles/1434?_encoding=UTF8&jiveRedirect=1)

  • s3
  • amazon
  • html form
  • upload form
Read More

Generate a dineromail form in python

This is the code we use on bandtastic.me to build a html that sends users to dineromail to pay with. This code builds a form thats ready to send multiple items.

  • django
  • python
  • dineromail
  • express checkout
Read More

get and image object

I use this snippet to save images to my imagefields on django models. This uses the very awesome requests library, so install it first pip install requests You probably want to have this be done on a celery queu, if the image is big enough.

  • imagefield
  • save file
  • save url
Read More

Encrypted paypal buttons

Function and usage in views and template with django-paypal to have encrypted paypal buttons with a cart(adding multiple elements). All credits go to Jon Atkinson, http://jonatkinson.co.uk/paypal-encrypted-buttons-django/ I just added it here with a complete implementation using a cart(his example didnt include it). I know there is some redundancy in the data passed to the dict and the submit form, i'm just not sure what can i take out, the paypal docs are not clear about it, if you test this code without some of the data and it works, please tell me. The key parts are the cmd _s-xclick and again the cmd '_cart' both are needed.

  • paypal
  • encrypted button
  • cart
Read More

Redirect to no slash

The canonical notion of urls ending in slashes dates from a web where urls were used to access documents and files this is no longer the case so keeping your urls witouth trailing slashes makes them prettier. The problem is that many people/blogs/spiders/browsers could end up with a url with slashes which can be problematic for you SEO, or confuse users. This script is for sites with no trailing slash dicipline in their urls, and to prevent everybody getting a horrible 404 for a simple slash you just got to remove it and issue a permanent redirect (301) and you'll get your pretty urls your cake and eat it too. I must stress, you will have to edit all your public urls removing the slashes like so: url(r'^login$', login,} If you forget, to edit them and visit the url, your browser will remember the redirect and you'll have to clean the browsing history to fix it.

  • permanent redirect
  • no slashes
  • no slash
Read More

Save image in field

Small function that i use to save files to an imagefield, the file can be either an url or a file. This function has the following requirements. import requests from django.core.files import File from django.core.files.temp import NamedTemporaryFile from django.conf import settings Get the awesome requests library: pip install requests This function uses MEDIA_ROOT to know the location of the static dir, you can change that chaging the static_dir variable.

  • image field
  • save file
Read More

Send templated email with text | html | optional files

Use this to send emails to your users, takes one template and renders it as html or text as needed. Credits to """ Jutda Helpdesk - A Django powered ticket tracker for small enterprise. (c) Copyright 2008 Jutda. All Rights Reserved. See LICENSE for details. lib.py - Common functions (eg multipart e-mail) """ MIT licence I only removed the specific project parts and made it general to use. The original project repository https://github.com/rossp/django-helpdesk/

  • template
  • email
  • function
Read More

grillermo has posted 12 snippets.