|
@@ -21,12 +21,14 @@ class InstanceData:
|
|
|
self._fields = fields if fields else self.model.fields()
|
|
|
|
|
|
def values(self):
|
|
|
+ r = []
|
|
|
o = self.instance
|
|
|
for f in self.model.fields():
|
|
|
if f.is_relation:
|
|
|
- yield self._value_of_field(o, f)
|
|
|
+ r.append(self._value_of_field(o, f))
|
|
|
else:
|
|
|
- yield self._value_of_field(o, f)
|
|
|
+ r.append(self._value_of_field(o, f))
|
|
|
+ return r
|
|
|
|
|
|
def _value_of_field(self, instance, field):
|
|
|
"""
|
|
@@ -36,8 +38,8 @@ class InstanceData:
|
|
|
e o terceiro elemento é uma sequencia de URLs para os itens do primeiro elemento,
|
|
|
ou um valor falso caso não haja links.
|
|
|
"""
|
|
|
- if hasattr(instance, field.bare_name()):
|
|
|
- v = getattr(instance, field.bare_name())
|
|
|
+ if hasattr(instance, field.accessor_name()):
|
|
|
+ v = getattr(instance, field.accessor_name())
|
|
|
else: # Many to many relations without value may disappear
|
|
|
return [], True
|
|
|
if hasattr(v, '__iter__'):
|
|
@@ -49,9 +51,11 @@ class InstanceData:
|
|
|
return (v, ()), False
|
|
|
|
|
|
def fields_and_values(self):
|
|
|
+ r = []
|
|
|
for field in self.model.fields():
|
|
|
value, is_multiple = self._value_of_field(self.instance, field)
|
|
|
- yield field, value, is_multiple
|
|
|
+ r.append((field, value, is_multiple))
|
|
|
+ return r
|
|
|
|
|
|
def is_controlled(self):
|
|
|
return self.model.is_controlled()
|
|
@@ -91,27 +95,35 @@ class InstanceData:
|
|
|
return registry.get_url_of_action(self.model.model, "select")
|
|
|
|
|
|
def actions(self):
|
|
|
+ r = []
|
|
|
acts = registry.model_entry(self.model.model)
|
|
|
- if self.request:
|
|
|
+ if self.request and acts:
|
|
|
for a in acts.values():
|
|
|
if self.has_permission(self.request, a.action.name) and\
|
|
|
a.action.visibility > Action.Visibility.hidden:
|
|
|
- yield (a, a.get_url(self.instance))
|
|
|
+ r.append((a, a.get_url(self.instance)))
|
|
|
+ return r
|
|
|
|
|
|
def model_actions(self):
|
|
|
+ r = []
|
|
|
for (a, u) in self.actions():
|
|
|
if not a.action.query_parameters:
|
|
|
- yield (a, u)
|
|
|
+ r.append((a, u))
|
|
|
+ return r
|
|
|
|
|
|
def instance_actions(self):
|
|
|
+ r = []
|
|
|
for (a, u) in self.actions():
|
|
|
if a.action.query_parameters:
|
|
|
- yield (a, u)
|
|
|
+ r.append((a, u))
|
|
|
+ return r
|
|
|
|
|
|
def list_actions(self):
|
|
|
+ r = []
|
|
|
for (a, u) in self.instance_actions():
|
|
|
if a.action.visibility == Action.Visibility.list:
|
|
|
- yield (a, u)
|
|
|
+ r.append((a, u))
|
|
|
+ return r
|
|
|
|
|
|
def has_permission(self, request, action_name):
|
|
|
m = registry.model_entry(self.model.model).get(action_name)
|
|
@@ -146,32 +158,34 @@ class ModelData:
|
|
|
return self.model._default_manager
|
|
|
|
|
|
def all_fields(self):
|
|
|
+ r = []
|
|
|
relations = []
|
|
|
for f in itertools.chain(self.local_fields(), self.related_fields()):
|
|
|
if f.is_relation():
|
|
|
relations.append(f)
|
|
|
else:
|
|
|
if f.name not in self.excludes:
|
|
|
- yield f
|
|
|
+ r.append(f)
|
|
|
for f in relations:
|
|
|
if f.name not in self.excludes:
|
|
|
- yield f
|
|
|
+ r.append(f)
|
|
|
+ return r
|
|
|
|
|
|
def fields(self):
|
|
|
- for f in self._fields:
|
|
|
- yield f
|
|
|
+ return self._fields
|
|
|
|
|
|
def local_fields(self):
|
|
|
+ r = []
|
|
|
for f in self.model._meta.local_fields:
|
|
|
if f.name not in self.excludes:
|
|
|
- yield FieldData(f, self.request)
|
|
|
+ r.append(FieldData(f, self.request))
|
|
|
for f in self.model._meta.local_many_to_many:
|
|
|
if f.name not in self.excludes:
|
|
|
- yield FieldData(f, self.request)
|
|
|
+ r.append(FieldData(f, self.request))
|
|
|
+ return r
|
|
|
|
|
|
def related_fields(self):
|
|
|
- for f in self.model._meta.get_all_related_objects():
|
|
|
- yield FieldData(f, self.request)
|
|
|
+ return [FieldData(f, self.request) for f in self.model._meta.get_all_related_objects()]
|
|
|
|
|
|
def is_controlled(self):
|
|
|
return registry.is_controlled(self.model)
|
|
@@ -200,13 +214,14 @@ class ModelData:
|
|
|
return registry.get_url_of_action(self.model, "select")
|
|
|
|
|
|
def actions(self):
|
|
|
+ r = []
|
|
|
acts = registry.model_entry(self.model)
|
|
|
- if self.request:
|
|
|
+ if self.request and acts:
|
|
|
for a in acts.values():
|
|
|
if self.has_permission(self.request, a.action.name) and\
|
|
|
not a.action.query_parameters and\
|
|
|
a.action.visibility > Action.Visibility.hidden:
|
|
|
- yield (a, a.get_url())
|
|
|
+ r.append((a, a.get_url()))
|
|
|
|
|
|
def has_permission(self, request, action_name):
|
|
|
m = registry.model_entry(self.model).get(action_name)
|
|
@@ -240,14 +255,21 @@ class FieldData:
|
|
|
def bare_name(self):
|
|
|
return unicode(self.field.name)
|
|
|
|
|
|
+ def accessor_name(self):
|
|
|
+ if hasattr(self.field, 'get_accessor_name'):
|
|
|
+ return unicode(self.field.get_accessor_name())
|
|
|
+ return unicode(self.field.name)
|
|
|
+
|
|
|
def name(self):
|
|
|
- if self.is_auto():
|
|
|
- return ModelData(self.related_model()).model_name_plural()
|
|
|
+ if self.is_auto() and self.is_relation():
|
|
|
+ return self.related_model().model_name_plural() + u' - ' + self.related_field().name()
|
|
|
if hasattr(self.field, "verbose_name"):
|
|
|
return unicode(self.field.verbose_name)
|
|
|
return unicode(self.field.name)
|
|
|
|
|
|
def name_plural(self):
|
|
|
+ if self.is_auto() and self.is_relation():
|
|
|
+ return self.related_model().model_name_plural() + u' - ' + self.related_field().name_plural()
|
|
|
if hasattr(self.field, "verbose_name_plural"):
|
|
|
return unicode(self.field.verbose_name_plural)
|
|
|
return self.name() + "s"
|
|
@@ -271,6 +293,11 @@ class FieldData:
|
|
|
return ModelData(self.field.to)
|
|
|
return None
|
|
|
|
|
|
+ def related_field(self):
|
|
|
+ if hasattr(self.field, "field"):
|
|
|
+ return FieldData(self.field.field, self.request)
|
|
|
+ return None
|
|
|
+
|
|
|
def is_auto(self):
|
|
|
return self.field.auto_created
|
|
|
|