Add default normalize logic for API views

and use common logic for getting field list in traditional Form class
This commit is contained in:
Lance Edgar 2022-08-30 21:56:46 -05:00
parent 960d6279a9
commit 35728e20be
3 changed files with 25 additions and 22 deletions

View file

@ -28,7 +28,10 @@ from __future__ import unicode_literals, absolute_import
import json import json
import six
from rattail.config import parse_bool from rattail.config import parse_bool
from rattail.db.util import get_fieldnames
from cornice import resource, Service from cornice import resource, Service
@ -268,6 +271,21 @@ class APIMasterView(APIView):
query = self.Session.query(cls) query = self.Session.query(cls)
return query return query
def get_fieldnames(self):
if not hasattr(self, '_fieldnames'):
self._fieldnames = get_fieldnames(
self.rattail_config, self.model_class,
columns=True, proxies=True, relations=False)
return self._fieldnames
def normalize(self, obj):
data = {'_str': six.text_type(obj)}
for field in self.get_fieldnames():
data[field] = getattr(obj, field)
return data
def _collection_get(self): def _collection_get(self):
from sqlalchemy_filters import apply_filters, apply_sort, apply_pagination from sqlalchemy_filters import apply_filters, apply_sort, apply_pagination

View file

@ -49,21 +49,16 @@ class WorkOrderView(APIMasterView):
self.workorder_handler = app.get_workorder_handler() self.workorder_handler = app.get_workorder_handler()
def normalize(self, workorder): def normalize(self, workorder):
return { data = super(WorkOrderView, self).normalize(workorder)
'_str': six.text_type(workorder), data.update({
'uuid': workorder.uuid,
'id': workorder.id,
'customer_uuid': workorder.customer.uuid,
'customer_name': workorder.customer.name, 'customer_name': workorder.customer.name,
'estimated_total': workorder.estimated_total,
'notes': workorder.notes,
'status_code': workorder.status_code,
'status_label': self.enum.WORKORDER_STATUS[workorder.status_code], 'status_label': self.enum.WORKORDER_STATUS[workorder.status_code],
'date_submitted': six.text_type(workorder.date_submitted or ''), 'date_submitted': six.text_type(workorder.date_submitted or ''),
'date_received': six.text_type(workorder.date_received or ''), 'date_received': six.text_type(workorder.date_received or ''),
'date_released': six.text_type(workorder.date_released or ''), 'date_released': six.text_type(workorder.date_released or ''),
'date_delivered': six.text_type(workorder.date_delivered or ''), 'date_delivered': six.text_type(workorder.date_delivered or ''),
} })
return data
def create_object(self, data): def create_object(self, data):

View file

@ -37,6 +37,7 @@ from sqlalchemy.ext.associationproxy import AssociationProxy, ASSOCIATION_PROXY
from rattail.time import localtime from rattail.time import localtime
from rattail.util import prettify, pretty_boolean, pretty_quantity from rattail.util import prettify, pretty_boolean, pretty_quantity
from rattail.core import UNSPECIFIED from rattail.core import UNSPECIFIED
from rattail.db.util import get_fieldnames
import colander import colander
import deform import deform
@ -396,19 +397,8 @@ class Form(object):
if not self.model_class: if not self.model_class:
raise ValueError("Must define model_class to use make_fields()") raise ValueError("Must define model_class to use make_fields()")
mapper = orm.class_mapper(self.model_class) return get_fieldnames(self.request.rattail_config, self.model_class,
columns=True, proxies=True, relations=True)
# first add primary column fields
fields = FieldList([prop.key for prop in mapper.iterate_properties
if not prop.key.startswith('_')
and prop.key != 'versions'])
# then add association proxy fields
for key, desc in sa.inspect(self.model_class).all_orm_descriptors.items():
if desc.extension_type == ASSOCIATION_PROXY:
fields.append(key)
return fields
def make_renderers(self): def make_renderers(self):
""" """