The way to manually control CSRF correctness for FB applications. Automatic check cannot be used because FB does POST on your canvas URL when initializing your application without CSRF token. If you still want to use Django CSRF stuff do manual checks.
You only need to perform manual check when there is no correct signed_request present in your request - correct request means you really deal with FB. Use facebook_csrf_check to verify POST requests when signed_request is absent.
Apply a decorator to every urlpattern and URLconf module returned by Django's include() method . This allows you use a decorator on any number of views without having to decorate each one individually.
The use case here is wrapping all of the Django Admin with a superuser decorator. This is code that's better left alone where we can't actually go in and decorate the Admin views and urlpatterns manually. It's also almost guaranteed the Admin will include() other URL files. So the added bonus is all the INSTALLED_APPS that have their admin.py files registered by admin.autodiscover() will be decorated automatically as well.
This snippet is greatly inspired by [@miracle2k](http://djangosnippets.org/users/miracle2k/)'s excellent [#532](http://djangosnippets.org/snippets/532/). In the comments there @timbroder offers a modification to decorate includes but I think this is cleaner, simpler code and not subject to changes in the Django base code driving _get_url_patterns().
When you want to save integers to the db, you usually have the choice between 16-, 32- and 64-bit Integers (also 8- and 24-bit for MySQL). If that doesn't fit your needs and you want to use your db-memory more efficient, this field might be handy to you.
Imagine you have 3 numbers, but need only 10 bit to encode each (i.e. from 0 to 1000). Instead of creating 3 smallint-fields (48 bit), you can create one 'ByteSplitterField' which implements 3 'subfields' and automatically encodes them inside a 32 bit integer. You don't have to take care how each 10-bit chunk is encoded into the 32-bit integer, it's all handled by the field (see also field's description).
Additionally, the Field offers opportunity to use decimal_places for each of your subfields. These are 'binary decimal places', meaning the integer-content is automatically divided by 2, 4, 8, etc. when you fetch the value from the field.
You can also specify how values are rounded ('round' parameter) and what happens when you try to save a value out of range ('overflow' parameter)
Not implemented (maybe in the future if I should need it sometime):
* signed values. All values are positive right now!
* real (10-based) decimal places (actually you could probably directly use DecimalFields here)
* further space optimization, i.e. saving into CharField that's length can be chosen byte-wise
This is a function based on django's urlize modified to show different media based on their url. It supports images, links, mp3/ogg links, youtube videos, vimeo videos and dailymotion videos.
I added a switch called mini to have two modes to show things in different places. When mini is activated it will only parse the first url provided and discard the rest as well as limiting the height.
You can use this function to change an admin option dynamically.
For example, you can add a custom callable to *list_display* based on request, or if the current user has required permissions, as in the example below:
class MyAdmin(admin.ModelAdmin):
list_display = ('__unicode__', 'other_field')
def changelist_view(self, request, extra_context=None):
if request.user.is_superuser:
add_dynamic_value(self, 'list_display', my_custom_callable)
return super(MyAdmin, self).changelist_view(request, extra_context)
Rather simple usage, modelforms/in the admin:
class CustomAdminForm(forms.ModelForm):
class Meta:
model = Something
widgets = {
'image': URLFileInput(default_exts=[".png", ".gif", ".jpg"]),
}
class SomethingAdmin(admin.ModelAdmin):
form = CustomAdminForm
admin.site.register(Something, SomethingAdmin)
Basically, this will pull the image from the URL instead of only pulling it from your harddrive for upload.
Also accepts optional default_exts argument which limits the file types. Defaults to images.
For example:
Last modified: {% localdt item.modified_utc %} ({% localtimesince time.modified_utc %})
Converts the input datetimes to the timezone specified by the localtz context variable (it can also be explicitly specified, and all those other sensible things).
Input UTC datetimes can be specified using either a datetime or a timestamp.
Provides `localdt`, `localtime`, `localdate` and `localtimesince`.
If you want to test for trivial error in your add and changelist admin views, use this snippet.
Save the snippet in admintests.py and put it anywhere in your pythonpath.
Put this code in your tests.py:
from django.test import TestCase
from admintest import adminviews_test
class TestAdminViews(TestCase):
def test_admin_views(self):
adminviews_test(self)
Django lacks support of MySQL's "tinyint" 8-bit-integer datatype. This snippet gives you a TinyIntegerField and a PositiveTinyIntegerField. Falls back Django's SmallIntegerField if a different database-engine is used
This snippet allows you to return back to the filtered change_list after clicking "Save" on a change form. Other snippets I've found don't seem to take into account clicking on "Save and add another" or "Save and continue"
Django's built-in {% regroup %} template tag is great, but sometimes, you need to pass in the attribute you want to group on instead of declaring the attribute when you define the tag.
{% dynamic_regroup %} is identical in function to {% regroup %}, except that it will attempt to resolve a context variable for the attribute you want to group by.
{% dynamic regroup %} is also backward compatible, so you can also hand in the attribute literal and it will work as expected.
See the end of the code for an example of usage.
**Description**
A filestorage system that
+ is whitlisted,
+ changes the file name and targeting directory to put the file in - with respect to (runtime) instance information.
+ replaces files if they exists with the same name.
Kudos to [jedie](http://djangosnippets.org/users/jedie/) - http://djangosnippets.org/snippets/977/
This allows you to order the models on the index page of the administration site in a custom way.
This modification goes in the index method of django.contrib.admin.sites.AdminSite. I personally monkey patched it in where my models are loaded and registered with the admin site. Be careful that if you add new models you update the sorting dictionary, else you will get a key error. If no sorting is defined for an app, it will default to alphabetical order.
Note that you'll probably want to also insert this into the app_index function as well.
---
If you like my work, please check out my employer's site at 829llc.com
- Dan
You're looking at the most-bookmarked snippets on the site; if you'd like to help useful snippets show up here, sign up for an account and you'll get your own bookmarks list.