This code uses oracle as an authentication back end. It creates a new connection to the db and attempts to login. If successful it will then create an upper case User account with _ORACLE appended to the username.
My urls.py call:
from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'^accounts/login/$', 'django.contrib.auth.views.login',
{'template_name': 'login.html'}),
)
My setting.py specific settings:
AUTHENTICATION_BACKENDS = (
'oracleauth.views.OracleAuthBackend',
)
LOGIN_URL = '/accounts/login/'
ORACLE_CONNECT = 'database-host:1521/database'
DEBUG=True
unique_together doesn't work with ManyToMany, yet. See: http://code.djangoproject.com/ticket/702
Here a simple test in save() that would raise a IntegrityError.
Wanted a neat way to redirect views based on GeoIP determined criteria, from HTTP_REFERER. Decorator approach seemed the best way to make it straightforward to redirect views.
To use, installed the Max Mind Python GeoIP API : http://www.maxmind.com/app/python
This snippet helps preserving query parameters such as page number when the view perform redirects.
It does not support hooking templates and contexts currently.
TestableTemplate behaves just like django.template.Template, but you can give it a list of template.Libraries to load before parsing the template. This is equivalent to adding a bunch of {% load %} tags to the beginning of your template string, but you can use custom tag libraries which do not belong to Django applications' templatetags packages.
This is occasionally useful in testing.
Simple Python snippet to detect if any word in a list of words is inside your string. Use for profanity checking (my use case), auto tag detection, scoring, etc.
This will return an empty list if the word is not in the list. Assumes everything in words_to_find is lower case. Can probably be done cleaner with regular expressions but this method is extremely readable for those that prefer none regex solutions.
When using the django admin as a means of moderating reviews on a site, the obvious choice was to use admin actions and do everything from a single screen. The I stumbled across was that after the actions were peformed, the app redirected to the change list without any filters. This meant that filtering on un-moderated reviews was lost as soon as a change was made.
It turns out that the solution is pretty simple, you just put a redirect to request.get_full_path() at the end of the admin action. I think this should be the default behaviour, but the fix is simple nonetheless.
Example of using django localeurl with sitemaps. Create sitemap instance for each combination of the sitemap section and language.
In your sitemap class create method
`
def location(self, obj):
return chlocale(obj.get_absolute_url(), self.language)
`
or inherit it from LocaleurlSitemap class.
If you use decorators to views, it will greatly improve readability and extensibility of your code. I'm using a couple of decorators like this to reduce complexity and redundancy in my view codes.
`wraps` from Python 2.5's standard library make the attributes (name, docstring, and etc) of the decorated function same to those of `view_func` so that it could be easily recognized when you run `./manage.py show_urls` or debug.
Since r7806, the `User` field is unsorted which makes it harder to find specific users in the list if there is more than a few. This snippet is an `django.contrib.admin.ModelAdmin` subclass which searches through all of the fields on a form and automatically sorts fields which have a relation with `User`. It also filters on having `active=True`.
Just import the `SortedActiveUserModelAdmin` class in your `admin.py` and subclass your `ModelAdmin` classes from it instead of `admin.ModelAdmin`.
This is an example of how you change a ChoiceField select widget into a hidden field if the right GET variable is passed. In this example code it would change the select widget into something like the following if something like "?d=3" was passed.
`<p><label for="id_designation">Designation</label>Designation Option<input type="hidden" name="designation" value="3" id="id_designation" /></p>`
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.