This snippit is meant to be used with the pre_delete signal to delete any files associated with a model instance before the instance is deleted. It will search the model instance for fields that are subclasses of FieldFile, and then delete the corresponding files. As such, it will work with any model field that is a subclass of FileField.
This snippit can be used to generate unique file names for uploads. `upload_to` allows a callable, but only provides two arguments: `instance` and `filename`. In order to prevent dumping all of the files for a model into one directory, this utilizes the `partial` decorator from `functools`, which essentially allows adding an extra argument to the function. The returned path will be of the form: `[model name]/[field_name]/[random hash].[filename extension]`
Based on [snippet #1212](http://djangosnippets.org/snippets/1212/) along with it's comments.
Replaced the for loop with translate.
example usage:
from django.core.cache import cache
from mysnippet import cache_key_clean
expensive_func = lambda x: 'x{0}x'.format(x)
input_string = "I wanted a nice value."
key = cache_key_clean(input_string)
result = cache.get(key)
if result is None:
result = expensive_func(input_string)
cache.set(key, result)
Based on the original http://code.google.com/p/django-thumbs/
Added South support too.
Usage:
` photo = ImageWithThumbsField(upload_to='images', sizes=((125,125,True),(300,200),)`
To retrieve image URL, exactly the same way as with ImageField:
`my_object.photo.url`
To retrieve thumbnails URL's just add the size to it:
`my_object.photo.url_125x125`
`my_object.photo.url_300x200`
To convert to greyscale set the third attribute of size to True
This method will return an inline formset class that validates values across the given field are unique among all forms. For instance:
ApprovedUserFormSet = inlineformset_factory(Request, ApprovedUser, formset=unique_field_formset('email'), form=ApprovedUserForm)
Will make sure all ApprovedUser objects created for the Request have unique "email" fields.
There is a lot of debate on whether there is a real future for the Django CBVs (class based views). Personally, I find them tedious, and just wanted a way to keep my views clean.
So, here is a really minimalistic way of having class based views, without the fuss.
This is a fork from:
http://stackoverflow.com/questions/742/class-views-in-django
http://djangosnippets.org/snippets/2041/
This one takes a template path as an argument. Return dictionary with template values from your view. It's simple as:
@render_to_template('posts/post_list.html')
def api_get_all(request):
return {'test': 'testing!'}
Validate Ukraine telephone numbers in popular formats:
+380 XX XXX-XX-XX
0XX-XXX-XX-XX
(0XX) XXX-XX-XX
This snippet fixes the errors found in
http://djangosnippets.org/snippets/2579/
Validates and cleans UK telephone numbers. Number length is checked, and numbers are cleaned into a common format. For example, "+44 (0)1234 567890" will be stored as "01234 567890".
Can reject premium numbers (0912 312 3123) or service numbers (1471, 118 118) with UKPhoneNumberField(reject=('premium', 'service'))
Can reject multiple number types so you can tune the form input to accept only landline or only mobile, or whatever combination you want.
Corrects the errors found in http://djangosnippets.org/snippets/1207/ and adds extra functionality and detail to the code found at http://djangosnippets.org/snippets/2809/
In particular, this version rejects individual invalid area codes and caters for area codes with mixed-length numbering in fine-grained detail.
**Uses info from:** [here](http://www.aa-asterisk.org.uk/index.php/Regular_Expressions_for_Validating_and_Formatting_UK_Telephone_Numbers)
Validates and cleans UK telephone numbers. Number length is checked, and numbers are cleaned into a common format. For example, "+44 (0)1234 567890" will be stored as "01234 567890"
Can reject premium numbers (0912 312 3123) or service numbers (1471, 118 118) with UKPhoneNumberField(reject=('premium', 'service'))
Corrects the errors found in http://djangosnippets.org/snippets/1207/
Showing a list of logged users using the *user_logged_in* and *user_logged_out* signals.
See [login and logout signals](https://docs.djangoproject.com/en/1.4/topics/auth/#login-and-logout-signals) in Django docs.
A *SimpleListFilter* derived class that can be used to filter by taggit tags in the admin.
To use, simply add this class to the *list_filter* attribute of your ModelAdmin class.
Ex.:
class ItemAdmin(admin.ModelAdmin):
list_display = ('name', 'unit', 'amount')
list_filter = ('unit', TaggitListFilter)
Based in [ModelAdmin.list_filter documentation](https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_filter).
This is hardcoded to use [django-discover-runner](http://pypi.python.org/pypi/django-discover-runner) since that's my main test runner but could easily be adopted to use Django's own test runner. If you're using a terminal that is capable of showing 256 colors use the `Terminal256Formatter` formatter instead.
Enabled it with the `TEST_RUNNER` setting:
TEST_RUNNER = 'dotted.path.to.highlighted.runner.HighlightedDiscoverRunner'
Where `dotted.path.to.highlighted.runner` is the Python import path of the file you saved the runner in.