Get rid of eval in metadata type default and lookup fields. gh-issue #151.

This commit is contained in:
Roberto Rosario
2015-07-23 02:49:29 -04:00
parent 58d919d173
commit 0a0a92116e
9 changed files with 188 additions and 40 deletions

View File

@@ -1,9 +1,8 @@
from __future__ import unicode_literals
from django.core.exceptions import ObjectDoesNotExist
from django.utils.translation import ugettext_lazy as _
from .models import MetadataType
class DocumentMetadataHelper(object):
@staticmethod
@@ -17,7 +16,35 @@ class DocumentMetadataHelper(object):
def __getattr__(self, name):
try:
return self.instance.metadata.get(metadata_type__name=name).value
except MetadataType.DoesNotExist:
except ObjectDoesNotExist:
raise AttributeError(
_('\'metadata\' object has no attribute \'%s\'') % name
)
class MetadataLookup(object):
_registry = []
@classmethod
def get_as_context(cls):
result = {}
for entry in cls._registry:
result[entry.name] = entry.value
return result
@classmethod
def get_as_help_text(cls):
result = []
for entry in cls._registry:
result.append(
'{{{{ {0} }}}} = "{1}"'.format(entry.name, entry.description)
)
return ' '.join(result)
def __init__(self, description, name, value):
self.description = description
self.name = name
self.value = value
self.__class__._registry.append(self)