Have everybody call the same return_attrib function
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
import types
|
||||
|
||||
from django import forms
|
||||
from django.utils.safestring import mark_safe
|
||||
from django.utils.translation import ugettext as _
|
||||
@@ -7,22 +5,8 @@ from django.db import models
|
||||
from django.conf import settings
|
||||
|
||||
|
||||
def return_attrib(obj, attrib, arguments=None):
|
||||
try:
|
||||
result = reduce(getattr, attrib.split("."), obj)
|
||||
if isinstance(result, types.MethodType):
|
||||
if arguments:
|
||||
return result(**arguments)
|
||||
else:
|
||||
return result()
|
||||
else:
|
||||
return result
|
||||
except Exception, err:
|
||||
if settings.DEBUG:
|
||||
return "Attribute error: %s; %s" % (attrib, err)
|
||||
else:
|
||||
pass
|
||||
|
||||
from common.utils import return_attrib
|
||||
|
||||
|
||||
class DetailSelectMultiple(forms.widgets.SelectMultiple):
|
||||
def __init__(self, queryset=None, *args, **kwargs):
|
||||
@@ -76,7 +60,8 @@ class DetailForm(forms.ModelForm):
|
||||
else:
|
||||
self.fields[extra_field['field']]=forms.CharField(
|
||||
label=extra_field['label'],
|
||||
initial=getattr(self.instance, extra_field['field'], None),
|
||||
#initial=getattr(self.instance, extra_field['field'], None),
|
||||
initial=return_attrib(self.instance, extra_field['field'], None),
|
||||
widget=PlainWidget)
|
||||
|
||||
for field_name, field in self.fields.items():
|
||||
|
||||
@@ -1,34 +1,11 @@
|
||||
import types
|
||||
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.conf import settings
|
||||
from django.template.defaultfilters import stringfilter
|
||||
from django.template import Library, Node, Variable, VariableDoesNotExist
|
||||
|
||||
from common.utils import return_attrib
|
||||
|
||||
register = Library()
|
||||
|
||||
def return_attrib(obj, attrib, arguments={}):
|
||||
try:
|
||||
if isinstance(obj, types.DictType) or isinstance(obj, types.DictionaryType):
|
||||
return obj[attrib]
|
||||
elif isinstance(attrib, types.FunctionType):
|
||||
return attrib(obj)
|
||||
else:
|
||||
result = reduce(getattr, attrib.split("."), obj)
|
||||
if isinstance(result, types.MethodType):
|
||||
if arguments:
|
||||
return result(**arguments)
|
||||
else:
|
||||
return result()
|
||||
else:
|
||||
return result
|
||||
except Exception, err:
|
||||
if settings.DEBUG:
|
||||
return "Error: %s; %s" % (attrib, err)
|
||||
else:
|
||||
pass
|
||||
|
||||
@register.filter
|
||||
def object_property(value, arg):
|
||||
return return_attrib(value, arg)
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
import types
|
||||
|
||||
from django.utils.http import urlquote as django_urlquote
|
||||
from django.utils.http import urlencode as django_urlencode
|
||||
from django.utils.datastructures import MultiValueDict
|
||||
from django.conf import settings
|
||||
|
||||
|
||||
def urlquote(link=None, get={}):
|
||||
u'''
|
||||
@@ -38,3 +42,25 @@ def urlquote(link=None, get={}):
|
||||
return u'%s%s' % (link, django_urlencode(get, doseq=True))
|
||||
else:
|
||||
return django_urlquote(link)
|
||||
|
||||
|
||||
def return_attrib(obj, attrib, arguments={}):
|
||||
try:
|
||||
if isinstance(obj, types.DictType) or isinstance(obj, types.DictionaryType):
|
||||
return obj[attrib]
|
||||
elif isinstance(attrib, types.FunctionType):
|
||||
return attrib(obj)
|
||||
else:
|
||||
result = reduce(getattr, attrib.split("."), obj)
|
||||
if isinstance(result, types.MethodType):
|
||||
if arguments:
|
||||
return result(**arguments)
|
||||
else:
|
||||
return result()
|
||||
else:
|
||||
return result
|
||||
except Exception, err:
|
||||
if settings.DEBUG:
|
||||
return "Attribute error: %s; %s" % (attrib, err)
|
||||
else:
|
||||
pass
|
||||
|
||||
Reference in New Issue
Block a user