Enable Metadata's default option, using user definable functions
This commit is contained in:
0
apps/documents/conf/__init__.py
Normal file
0
apps/documents/conf/__init__.py
Normal file
9
apps/documents/conf/settings.py
Normal file
9
apps/documents/conf/settings.py
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
import datetime
|
||||||
|
|
||||||
|
from django.conf import settings
|
||||||
|
|
||||||
|
default_available_functions = {
|
||||||
|
'current_date':datetime.datetime.now().date,
|
||||||
|
}
|
||||||
|
|
||||||
|
AVAILABLE_FUNCTIONS = getattr(settings, 'DOCUMENTS_METADATA_AVAILABLE_FUNCTIONS', default_available_functions)
|
||||||
@@ -10,6 +10,8 @@ from common.utils import urlquote
|
|||||||
from models import Document, DocumentType, DocumentTypeMetadataType
|
from models import Document, DocumentType, DocumentTypeMetadataType
|
||||||
|
|
||||||
|
|
||||||
|
from documents.conf.settings import AVAILABLE_FUNCTIONS
|
||||||
|
|
||||||
class DocumentForm(forms.ModelForm):
|
class DocumentForm(forms.ModelForm):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Document
|
model = Document
|
||||||
@@ -29,8 +31,14 @@ class MetadataForm(forms.Form):
|
|||||||
required=False, widget=forms.TextInput(attrs={'readonly':'readonly'}))
|
required=False, widget=forms.TextInput(attrs={'readonly':'readonly'}))
|
||||||
self.fields['value'] = forms.CharField(label=_(u'Value'))
|
self.fields['value'] = forms.CharField(label=_(u'Value'))
|
||||||
if hasattr(self, 'metadata_type'):
|
if hasattr(self, 'metadata_type'):
|
||||||
self.fields['name'].initial=self.metadata_type.name
|
self.fields['name'].initial=self.metadata_type.name
|
||||||
self.fields['id'].initial=self.metadata_type.id
|
self.fields['id'].initial=self.metadata_type.id
|
||||||
|
if self.metadata_type.default:
|
||||||
|
try:
|
||||||
|
self.fields['value'].initial = eval(self.metadata_type.default, AVAILABLE_FUNCTIONS)
|
||||||
|
except Exception, err:
|
||||||
|
self.fields['value'].initial = err
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class DocumentCreateWizard(BoundFormWizard):
|
class DocumentCreateWizard(BoundFormWizard):
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ from datetime import datetime
|
|||||||
from django.db import models
|
from django.db import models
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
|
||||||
|
from documents.conf.settings import AVAILABLE_FUNCTIONS
|
||||||
|
|
||||||
|
|
||||||
def get_filename_from_uuid(instance, filename, directory='documents'):
|
def get_filename_from_uuid(instance, filename, directory='documents'):
|
||||||
populate_file_extension_and_mimetype(instance, filename)
|
populate_file_extension_and_mimetype(instance, filename)
|
||||||
@@ -46,9 +48,12 @@ class Document(models.Model):
|
|||||||
return self.uuid
|
return self.uuid
|
||||||
|
|
||||||
|
|
||||||
|
available_functions_string = (_(u' Available functions: %s') % ','.join(['%s()' % name for name, function in AVAILABLE_FUNCTIONS.items()])) if AVAILABLE_FUNCTIONS else ''
|
||||||
|
|
||||||
class MetadataType(models.Model):
|
class MetadataType(models.Model):
|
||||||
name = models.CharField(max_length=32, verbose_name=_(u'name'))
|
name = models.CharField(max_length=32, verbose_name=_(u'name'))
|
||||||
default = models.CharField(max_length=64, blank=True, null=True, verbose_name=_(u'default'))
|
default = models.CharField(max_length=64, blank=True, null=True,
|
||||||
|
verbose_name=_(u'default'), help_text=_(u'Enter a string to be evaluated.%s') % available_functions_string)
|
||||||
lookup = models.CharField(max_length=64, blank=True, null=True, verbose_name=_(u'lookup'))
|
lookup = models.CharField(max_length=64, blank=True, null=True, verbose_name=_(u'lookup'))
|
||||||
#datatype = models.
|
#datatype = models.
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user