You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.3 KiB
48 lines
1.3 KiB
import random
|
|
import string
|
|
from django.db.models.fields.files import FieldFile
|
|
from signal_additional_func import fill_missing_languages
|
|
from functions.form_check import translit_with_separator
|
|
|
|
|
|
def pre_save_handler(sender, **kwargs):
|
|
obj = kwargs['instance']
|
|
url = getattr(obj, 'url')
|
|
|
|
if url:
|
|
return
|
|
|
|
if hasattr(obj, 'language_code') and obj.language_code =='en':
|
|
try:
|
|
name = getattr(obj, 'name')
|
|
obj.url = translit_with_separator(name)
|
|
except AttributeError:
|
|
pass
|
|
|
|
|
|
if not obj.url:
|
|
obj.url = ''.join([random.choice(string.ascii_lowercase) for n in xrange(8)])
|
|
|
|
|
|
def post_save_handler(sender, **kwargs):
|
|
"""
|
|
receiver function
|
|
take object
|
|
fill missing languages
|
|
fill settings if its exist for this object
|
|
|
|
"""
|
|
obj = kwargs['instance']
|
|
fill_missing_languages(obj)
|
|
|
|
|
|
def file_cleanup(sender, instance, *args, **kwargs):
|
|
"""
|
|
Deletes the file(s) associated with a model instance. The model
|
|
is not saved after deletion of the file(s) since this is meant
|
|
to be used with the pre_delete signal.
|
|
"""
|
|
for field_name, _ in instance.__dict__.iteritems():
|
|
field = getattr(instance, field_name)
|
|
if issubclass(field.__class__, FieldFile) and field.name:
|
|
field.delete(save=False) |