Another one like [980](http://www.djangosnippets.org/snippets/980/), but using re's instead.
I haven't benchmarked these, but my guess is that [981](http://www.djangosnippets.org/snippets/981/) is faster for strings of pure digits and this is faster for larger chunks of text that happen to contain digits. If you're generating the numbers yourself, I'd just use 981 on a number right when you generate it.
This is based on [980](http://www.djangosnippets.org/snippets/980/), removing the unnecessary use of StringIO. Hopefully the translation can be educational.
Arabic and Farsi languages use their own digits. This template filter translates any digits in the supplied unicode string into the correct ones for the language. The previous version used StringIO to parse the string one character at a time. It now uses regular expressions.
I just saw that kcarnold created two snippets that also removed the need for StringIO: [981](http://www.djangosnippets.org/snippets/981/) and [982](http://www.djangosnippets.org/snippets/982/). That last snippet is almost the same as this one.
Optio's [soaplib](http://trac.optio.webfactional.com/wiki/soaplib) makes it really straightforward to write SOAP web service views by using a decorator to specify types. Plus it's the only Python library, as of today, which is able to generate WSDL documents for your web service.
You can test it with a soaplib client:
>>> from soaplib.client import make_service_client
>>> from foo.views import HelloWorldService
>>> client = make_service_client('http://localhost:8000/hello_world/', HelloWorldService())
>>> client.say_hello('John', 2)
['Hello, John', 'Hello, John']
And get an WSDL document:
>>> client.server.wsdl('')
'<?xml version=\'1.0\' encoding=\'utf-8\' ?><definitions name="HelloWorldService"
...
</definitions>'
Usage:
from django.db import models
from imagevariations.fields import ImageVariationsField, Thumbnail
class Image(models.Model):
name = models.CharField(max_length=20)
image = ImageVariationsField(upload_to='testimages', variations=(Thumbnail,) )
How to use in templates:
Use the lowercase name of the image variation class.
{{ object.image.variations.thumbnail.url }}
By default all image variations will use the same storage backend as the field but can be replaced per variation by setting self.storage on the variation class.
A simple FileField with a addition file extension whitelist. Raised ValidationError("Not allowed filetype!") if a filename contains a extension witch is not in the whitelist.
This is a simple implementation overwrite of the FileSystemStorage. It removes the addition of an '_' to the filename if the file already exists in the storage system. I needed a model in the admin area to act exactly like a file system (overwriting the file if it already exists).
PayPal's [https://www.paypal.com/ipn](IPN mechanism) is ridiculously easy to consume. You can tell PayPal to POST every single transaction on your account to a URL somewhere, then set up a handler at that URL which processes those transactions in some way. Security is ensured by POSTing the incoming data back to PayPal for confirmation that the transaction is legitimate.
These classes are probably over-engineered, but they were a fun experiment in creating class-based generic views.
A simple addition for the urls.py that exposes the 404/500 templates during development. This way you can test how those look. They're mounted under /404/ and /505/ respectively.
Add this at the bottom of your main urls.py.
The `{% switch %}` tag compares a variable against one or more values in
`{% case %}` tags, and outputs the contents of the matching block. An
optional `{% else %}` tag sets off the default output if no matches
could be found:
{% switch result_count %}
{% case 0 %}
There are no search results.
{% case 1 %}
There is one search result.
{% else %}
Jackpot! Your search found {{ result_count }} results.
{% endswitch %}
Each `{% case %}` tag can take multiple values to compare the variable
against:
{% switch username %}
{% case "Jim" "Bob" "Joe" %}
Me old mate {{ username }}! How ya doin?
{% else %}
Hello {{ username }}
{% endswitch %}
An example of using it in your settings.py:
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.middleware.doc.XViewMiddleware',
'util.loginmiddleware.RequireLoginMiddleware',
)
LOGIN_REQUIRED_URLS = (
r'/payment/(.*)$',
r'/accounts/home/(.*)$',
r'/accounts/edit-account/(.*)$',
)
In a nutshell this requires the user to login for any url that matches against whats listing in LOGIN_REQUIRED_URLS. The system will redirect to [LOGIN_URL](http://www.djangoproject.com/documentation/settings/#login-url)
A custom FileSystemStorage made for normalizing extensions. It lets PIL look at the file to determine the format and append an always lower-case extension based on the results.
DualPasswordForm is a simple form that contains two password fields and validation to ensure that the two passwords match. A minimum password length of 7 characters is imposed, but feel free to change that.
Django's testing framework assumes you will be running your tests against "live" views that have been plugged in to your site's URL configuration - but sometimes you might want to run a test against a view function without first wiring it in to the rest of the site. This class makes it easy to do that by providing a "factory" for creating mock request objects, re-using the existing test Client interface (and most of the code). Once you've created a request object in your test you can use it to call your view functions directly, then run assertions against the response object that gets returned.
Given such code:
class ProductImage(models.Model):
fullsize = models.ImageField(upload_to= "products/%Y/%m/%d/")
display = AutoImageField(upload_to= "products/%Y/%m/%d/",prepopulate_from='fullsize', size=(300, 300))
thumbnail = AutoImageField(upload_to="products/%Y/%m/%d/",null=True,default='/media/noimage.jpg'))
display will be automatically resized from fullsize, and thumbnail will default to /media/noimage.jpg if no image is uploaded
Note:
At some point the code broke against trunk, which has now been updated to work properly