"""
Takes arguments & constructs Qs for filter()
We make sure we don't construct empty filters that would return too many results
We return an empty dict if we have no filters so we can still return an empty response from the view
"""
The function slices a queryset into smaller querysets containing chunk_size objects and then yield them.
It is used to avoid memory error when processing huge queryset, and also database error due to that the database pulls whole table at once.
Concurrent database modification wouldn't make some entries repeated or skipped in this process.
Implements necessary permission checks on a user model to be compatible with django admin, but just return true on all permissions without actually checking it against anything. Useful when you have a user model that should always be allowed to use django admin, and you don't care about using django's own PermissionsMixin and don't want to have those models added to your database.
This is a modificated version of `CachedPaginator` by **daniellindsley** [https://djangosnippets.org/snippets/1173/](https://djangosnippets.org/snippets/1173/) ([web-arhive-link](https://web.archive.org/web/20150927100427/https://djangosnippets.org/snippets/1173/)). Which not only cache `result_objects`, but the `total_count` of the `queryset` too (usefull if computating the count is an expensive operation too).
Rough check for unused methods in our apps. Scans models, views, utils, api, forms and signals
files for what look like methods calls, then looks at all of our classes' methods to ensure each
is called. Do not trust this blindly but it's a good way to get leads on what may be dead code. Assumes a setting called `LOCAL_APPS` so it only bothers to look at the code you've written rather than everything in `INSTALLED_APPS`.
Password hashing method using the crypt-sha512 algorithm, To be able to generate password compatible with the crypt-sha512 method avaiable in the standard crypt function since glib2.7 and used on modern linux distros. This provides compatibility with programs and systems that use the glibc crypt library for encrypting passwords (such as shadow passwords used by modern Linux distributions) while providing extra security than the regular crypt-sha1 mechanism (available in Django as CryptPasswordHasher)
To use it you just need to add something like this to your django settings file:
---
PASSWORD_HASHERS = [
'utils.hashers.CryptSHA512PasswordHasher',
'django.contrib.auth.hashers.PBKDF2PasswordHasher',
'django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher',
'django.contrib.auth.hashers.BCryptSHA256PasswordHasher',
'django.contrib.auth.hashers.BCryptPasswordHasher',
'django.contrib.auth.hashers.SHA1PasswordHasher',
'django.contrib.auth.hashers.MD5PasswordHasher',
'django.contrib.auth.hashers.UnsaltedSHA1PasswordHasher',
'django.contrib.auth.hashers.UnsaltedMD5PasswordHasher',
'django.contrib.auth.hashers.CryptPasswordHasher',
]
---
You need to keep the standard hashers on the list to be able to convert existing passwords to the new method. The next time a user login after the modification the password will be converted automatically to first hasher on the list.
Thanks mmoreaux for his improvements!!
In the last few days I spent a lot of time trying to find a library or repository of some kind that could help me generate the required DAYLIGHT and STANDARD components of ical VTIMEZONE blocks.
Since I couldn't find anything, I cobbled together this snippet to poke around in pytz timezone information and output the bare minimum I needed to make my ICS files compliant and useful (DST transitions for this year and the next).
I promise it's (superficially) tested against "real" ICS files, but that's all.
UPDATE: Thanks to @ariannedee for a much improved version (see comment for details)
This snippet allows you to use a new field type, RoundedDecimalField, that will automatically round any value affected to it according to the max_digits and decimal_places attributes. You will no longer receive validation errors if you use a float or a decimal with too many decimal digits.
What the docstring says. To not use some functionality, e.g. managing the value in the User's Profile model, delete the corresponding lines (when getting the page_size and when saving it.
Add the Mixin before the View class. e.g.: `class ItemList(PaginationMixin, generic.ListView):`
## How to use
Use this [admin filter](https://docs.djangoproject.com/en/1.8/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_filter) together with a numeric field to allow filtering changlist by field
values range (in this case, age groups):
For example, to group customers by age groups:
class Customer(models.Model):
# ...
age = models.IntegerField()
age.list_lookup_range = (
(None, _('All')),
([0, 2], '0-2'),
([2, 4], '2-4'),
([4, 18], '4-18'),
([18, 65], '18-65'),
([65, None], '65+'),
))
class CustomerAdmin(admin.ModelAdmin):
list_filter = [('age', ValueRangeFilter), ]
## Inspiration
[This snippet](https://djangosnippets.org/snippets/587/) (for django < 1.4) inspired me to make this work for newer django versions.
If you need to use some source to construct your form (maybe some database info or some user input), or you use the same fields with the same functionality in various forms and need to write the same piece of code for each of them, then you are looking in the right place. With this snippet you won't have those issues any more. :P
This snippet present a way to wean the fields from the form.
The code is made using crispy-forms, but the same idea can be applied without using it.
Best luck!
You're looking at the top-rated snippets currently on the site; if you'd like to contribute, sign up for an account and you'll be able to rate any snippet you see.