This is extremely simple decorator to add possibility to upload files with name specific in some object field. For example image with same name as object slug.
Sample **model**:
class Test(models.Model):
image = models.ImageField(\
upload_to=upload_to_dest(path='pics/', \
human_readable_field='hrname'))
hrname = models.CharField( \
max_length=128, \
blank=True, default='')
Sample **form** for admin:
FNAME_EXP = re.compile('^[A-Za-z0-9\-\_]+$')
class TestAdminForm(forms.ModelForm):
hrname = forms.RegexField(
label="Human Readable File Name", \
regex=FNAME_EXP, \
help_text="""Allowed only latin alphabet
(upper and lower cases),
underscore and minus
characters. PLEASE, DO NOT INCLUDE
EXTENSION OF THE FILE.
Sample: test-this-file""", \
required=False)
class Meta:
model = Test
Sample *admin.py* for *Test* model:
class TestAdmin(admin.ModelAdmin):
form = TestAdminForm
admin.site.register(Test, TesetAdmin)
This is a very flexible and concise way to [Handle choices the right way](http://www.b-list.org/weblog/2007/nov/02/handle-choices-right-way/) in model fields.
* Preserves order.
* Allows both a human-readable value for display in form `<select>`s as well as a code-friendly short name.
* Mimic's Django's canonical [choices format](http://docs.djangoproject.com/en/1.3/ref/models/fields/#choices).
* Doesn't restrict the value type.
* Memory efficient.
Inspired by [snippet 2373](http://djangosnippets.org/snippets/2373/) to use namedtuples as model field choices.
Note: This concerns django 1.3 but the tags does not yet exist.
We have a couple of apps, including 3rd party apps, that have the 'static' files in 'media' dirs. These files aren't found with collectstatic in django 1.3. With this snippet they will be.
To use it:
* paste the code in a file e.g. yourproject/finders.py.
* include the finder in settings.STATICFILES_FINDERS:
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'yourproject.finders.AppMediaDirectoriesFinder',
)
* ./manage.py collectstatic -n -l
This function mangles a generated form class to remove the Hold down "Control", or "Command"... messages from the help text. This is really a dirty hack awaiting a proper solution to [Django ticket 9321](http://code.djangoproject.com/ticket/9321).
This function can be useful for forms in the admin interface that use custom widgets. Basic usage:
class MyModelForm(forms.ModelForm):
class Meta:
model = MyModel
class MyAdmin(admin.ModelAdmin):
form = remove_holddown(MyModelForm, ('field1', 'field2'))
This is a version of [http://djangosnippets.org/snippets/2258/](http://djangosnippets.org/snippets/2258/) that should work with special chars (e.g. quotes) in json data.
You can use this cache backend to cache data in-process and avoid the overhead of pickling. Make absolutely sure you don't modify any data you've stored to or retrieved from the cache. Make deep copies instead if necessary.
The backend is basically identical to Django's stock locmem cache (as of r15852 - after 1.3rc1) with pickling removed. It has been tested with that specific Django revision, so basically it's >=1.3 compatible.
See [Django ticket #6124](http://code.djangoproject.com/ticket/6124) for some background information.
TimeZoneField is not supported with South 0.7. This snippet adds a custom rule to the introspection rules for TimeZoneField. The code is found in the following thread, and fixed according to the latest version of TimeZoneField.
http://groups.google.com/group/south-users/browse_thread/thread/34a05331140ee4dd
**Usage**
Simplest way is to save this snippet in a south_rules.py file and add an import in the models.py.
Firefox transparently follows redirects when AJAX calls return 3xx code. And it drops additional headers, X-Requested-With among them. Server treats redirected HTTP requested as non-AJAX. JS libraries has nothing to do here.
At 16.03.11 bug https://bugzilla.mozilla.org/show_bug.cgi?id=553888 has status "NEW" being reported at 21.03.10.
Workaround is following:
- in process_response():
if request.is_ajax() and response has status_code 3xx then put response["Location"] to session, otherwise unset session stored value (if it is there).
- in process_request():
if not request.is_ajax() and request.path equals to stored session value then monkeypatch request.is_ajax() return True (before any views come into play).
This results in smooth transparent redirects in Firefox, all treated as AJAX calls.
These base form classes add a method to return error messages as HTML encoded as JSON from Django, optionally passing in an argument to strip tags out. The method can be called in your view after checking that your form is valid. There is a ModelForm and Form class to use depending on your needs.
The sample jQuery function will take the errors returned as json, loop over the errors and insert the error after each field. If you're using a form prefix, you'll need to add a hidden field to hold the value for the prefix.
The `__all__` error(s), which are not bound to a field are appended to the end of the form, which you could easily reposition.
Happy coding!
Utility functions for generating dummy ("lorem ipsum") text, with text from
the "Karel ende Elegast" old-Dutch epic poem. [wikipedia](http://en.wikipedia.org/wiki/Karel_ende_Elegast)
by Wicher Minnaard <[email protected]> [website](http://smorgasbord.gavagai.nl/)
Sort-of monkey-patches django.contrib.webdesign.lorem_ipsum (it's an experiment).
Hopefully threadsafe, so both lorem_ipsum and elegast module functions can be
accessed simultaneously.
FastCGI handler in prefork mode first imports every application, then forks. If any application during import opens connection to database, open connection is inherited by all of child processes; open db connection cannot be shared between processes, and if one process sends a request, another can receive a reply; also, if one process closes it, others have a desynchronized database handle.
See:
* http://stackoverflow.com/questions/1573579/psycopg2-disconnects-from-server
* http://stackoverflow.com/questions/393637/django-fastcgi-randomly-raising-operationalerror
* http://code.djangoproject.com/ticket/13215
* http://groups.google.com/group/django-users/browse_thread/thread/2c7421cdb9b99e48
If you put this snippet in your project-level __init__.py, then running manage.py runfcgi will fail loudly if the database connection is open in the parent process. This will happen only when you provide debug=true option to runfcgi to avoid monkey-patching code that runs in production mode.
A middleware which will protect from page hammering using flexible spanning time windows using the cache backend.
Please read the Docstring of the class for details.