Requires [GitPython](http://gitorious.org/projects/git-python).
Grabs the most recent commits to a Git repository, as defined in `settings.py`. Inspired by [Simon Willison](http://simonwillison.net)'s [about page](http://simonwillison.net/about/).
**Example:**
{% get_recent_commits 10 as commits %}
<ul>
{% for commit in commits reversed %}
<li>{{ commit.commited_date }} - {{ commit.message }}</li>
{% endfor %}
</ul>
Rather than using the full GZipMiddleware, you may want to just compress some views. This decorator lets you do that.
@gzip_compress
def your_view(request, ...):
....
While we're on the topic of SSLRedirect (See snippet 240 and 880) here's what I add to the end of my ssl middleware module, so that SSLRedirect wont break my automated testing.
It simply creates a dummy middleware class that removes the SSL argument, but does not do any redirecting. If you were to simply remove the middleware from settings, the extra SSL argument will then get passed on to all the relevant views.
Of course, you'll need to define a TESTING variable in settings, or change this to something else like settings.DEBUG. Having the separate variable for testing allows you to run your server in DEBUG mode without side effects like the change above.
In an admin custom view I had the requirement to modify the upload handlers.
However, the @staff_member_required locked the Files upload handlers as it uses the request.POST - see [Ticket 7654](http://code.djangoproject.com/ticket/7654).
These decorators can be used before other decorators to allow setting of the upload handlers.
Usage example:
@upload_handlers_insert(0, QuotaUploadHandler)
@staff_member_required
def upload(request):
pass
This class works in compatibility with a ModelForm, but instead of show a form, it shows the field values.
The Meta class attributes can be used to customize fields to show, to be excluded, to divide by sections, to urlize text fields, etc.
**Example 1:**
class InfoSingleCertificate(ModelInfo):
class Meta:
model = SingleCertificate
sections = (
(None, ('vessel','description','document','comments','status',)),
('Issue', ('issue_date','issue_authority','issue_location',)),
)
**Example 2:**
class InfoSingleCertificate(ModelInfo):
class Meta:
model = SingleCertificate
fields = ('vessel','description','document','comments','status',
'issue_date','issue_authority','issue_location',)
**How to use:**
Just save this snippet as a file in your project, import to your views.py module (or a new module named "info.py") and create classes following seemed sytax you use for ModelForms.
Sometimes you need to prevent concurrent access to update/calculate some properties right. Here is (MySQL) specific example to lock one table with new object manager functions.
Creating new field to handle checkbox validation in situations where the checkbox must be checked, as in check to agree to terms and such.
Thanks to Daniel Pope for the [suggestion](http://code.djangoproject.com/ticket/5957#comment:7) on Django Trac Ticket #[5957](http://code.djangoproject.com/ticket/5957)
If you feel nostalgic for the old days of crufty URLs, put this middleware somewhere in your Django app and add the first entry in settings.MIDDLEWARE_CLASSES as shown below. Keep in mind that you need to replace 'path.to' with the correct Python path to the middleware.
MIDDLEWARE_CLASSES = (
'path.to.RandomFileExtensionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
)
Note: Although this works it's still a joke.
Extension to the normal ManyToManyField to support default values.
Build for the following use case:
publish_on = ManyToManyFieldWithDefault(Site, verbose_name=_('publish on'), default=Site.objects.get_current)
Where with a plain ManyToManyField the default site will not be selected. The ManyToManyFieldWithDefault fixes this by automatically selecting the default value if no other selections are given.
When the field also have null=True and Blank=True it will not select the default !
Sometimes the order of the fields you get from a model needs to be adjusted when displaying its modelform. If it's just a few fields you can do it in the template, but what if you want to iterate over the form?
The fields are stored in a SortedDict, so you can change the order in the __init__ of the form. A bit clunky, yes.
This view assumes you have downloaded [modelviz.py](http://django-command-extensions.googlecode.com/svn/trunk/extensions/management/modelviz.py) and placed it in a python module called utils within my_project. You also must have graphviz installed and a subprocess-capable python. From there you can feed it a URL query list of the options you want to pass to generate_dot, and it will dynamically draw png or svg images of your model relationships right in the browser. All it wants is a nice form template for graphically selecting models. Most of this code and the main idea thereof was shamelessly plagiarized from [someone else](http://gundy.org/).
Examples:
`http://localhost:8000/model_graph/?image_type=svg&app_labels=my_app&include_models=Polls&include_models=Choices`
`http://localhost:8000/model_graph/?image_type=png&all_applications&disable_fields&group_models`
put this code into a file in a folder "templatetags" inside some application and use it like this:
{% load your_file_name %}
{% for item in your_list|order_by:"field1,-field2,other_class__field_name"
**This is an alternative to User.get_profile.**
Rather than having you call `User.get_profile` directly, this retrieves the profile instance for a `User` and attaches the fields from the profile to the `User` object when instantiated. The special methods for `DateField`, `FileField`, `ImageField` and fields with `choices` are also created.
Since the profile object still has to be retrieved from the database before its fields can be added to the `User`, the costs for using this might outweigh the rewards unless you are heavily using profiles.
To install, place it in a module on your `PYTHONPATH` and add it to `INSTALLED_APPS`.
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.