This code provides a mixin and decorator which, when used together, can provide advisory locking on model methods. It provides locking by using MySQL's advisory lock system. See the example at the bottom of the code.
This is a convenient and easy way to guarantee your model methods have exclusive access to a model instance.
The LockableObjects class requires a MySQL backend, but it could be modified to use other back-end locking systems.
The lock name generation in `LockableObject.get_lock_name()` could be altered to create much more complex locking schemes. Locking per-object, per-method for example..
Lock attempts have a timeout value of 45 seconds by default. If a timeout occurs, EnvironmentError is raised.
**See the bottom of the script for an example**
> **Instructions:**
* **1:** Place the code in locking.py somewhere in your path
* **2:** In your models.py (or any script with an object you want to lock):
`from locking import LockableObject, require_object_lock`
* **3:** In the model you want locking for, add the `LockableObject` mixin
* **4:** Decorate the method you want to be exclusively locked with `@require_object_lock`
- model
- mysql
- decorator
- mixin
- locking
A template loader useful for writing templates with carefully controlled newlines and spaces while retaining readable template source code formatting.
Start the template with PTFTAG (`{#ptfable#}`, here) to allow it to be processed. Common problems with doing it to most templates as-is is use of newlines to separate words and multiple spaces between tags where spaces are still needed (which is problematic with `spaceless` tag as well).
Currently intended as a template loader wrapper, and is suggested to be used with cached loader. Example settings.py configuration:
_lp = lambda lo, *ar: (lo, ar,) # loader, arguments
TEMPLATE_LOADERS = (
_lp('django.template.loaders.cached.Loader', # cache
_lp('ptf.template.ptftemplateloader.Loader', # ptf
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
#'django.template.loaders.eggs.load_template_source'
), # ptf
), # cache
)
(change `ptf.` to wherever in python path you've put it).
You might also need couple of simple template tags for explicitly inserting newlines or whitespaces:
def br():
return "\n"
br = register.simple_tag(br)
# XHTML-XMPP-template compatible.
def brx():
return "<br />\n"
brx = register.simple_tag(br)
def ws():
return " "
ws = register.simple_tag(ws)
.
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)
- django
- ImageField
- file uploads
- file name
- FileField