DetailForm: Use Meta class instead

Instead of class attributes, make a generic reusable the
FormOption class and update the DetailForm to use a Meta
class for options.

Signed-off-by: Roberto Rosario <Roberto.Rosario@mayan-edms.com>
This commit is contained in:
Roberto Rosario
2019-01-23 14:48:23 -04:00
parent 3f48a5549e
commit 8c085331f1
2 changed files with 103 additions and 43 deletions

View File

@@ -6,6 +6,7 @@ import shutil
import tempfile
from django.conf import settings
from django.core.exceptions import FieldDoesNotExist
from django.db.models.constants import LOOKUP_SEP
from django.urls import resolve as django_resolve
from django.urls.base import get_script_prefix
@@ -142,6 +143,30 @@ def get_storage_subclass(dotted_path):
return StorageSubclass
def introspect_attribute(attribute_name, obj):
try:
# Try as a related field
obj._meta.get_field(field_name=attribute_name)
except (AttributeError, FieldDoesNotExist):
attribute_name = attribute_name.replace('__', '.')
try:
# If there are separators in the attribute name, traverse them
# to the final attribute
attribute_part, attribute_remaining = attribute_name.split(
'.', 1
)
except ValueError:
return attribute_name, obj
else:
return introspect_attribute(
attribute_name=attribute_part,
obj=related_field.related_model,
)
else:
return attribute_name, obj
def TemporaryFile(*args, **kwargs):
kwargs.update({'dir': setting_temporary_directory.value})
return tempfile.TemporaryFile(*args, **kwargs)