Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def read__execute_query(self):
if not self.resource.Meta.disable_total:
self.q.append_column(sa.func.count().over().label("total"))
result = self.session.execute(self.q).fetchall()
serialized_data = self.resource.dump(result, many=True)
id_field = get_id_field(self.resource, name_only=True)
self.obj_ids.extend([_i[id_field] for _i in serialized_data])
self.parent_payload = serialized_data
if serialized_data and not self.resource.Meta.disable_total:
self.total = result[0].total
file=sys.stderr,
)
self.Meta = ResourceMeta.from_class(class_meta)
if not isinstance(self.Meta, ResourceMeta):
raise Exception(
f"{cls_name}.Meta must inherit from ResourceMeta class"
)
if not self.Meta.name or self.Meta.name in (
"base_resource",
"_resource",
):
raise Exception(f"{cls_name} must have Meta.name")
resource_id_name = get_id_field(self, name_only=True, skip_exc=True)
if resource_id_name:
resource_id_field = self.fields.get(resource_id_name)
resource_id_field = resource_id_field.metadata.get("model_field")
if not resource_id_field:
raise Exception(
f"Resource's {cls_name} id field {resource_id_name}"
f" must have model_field."
else:
fields_to_select[field_name] = model_field
if (
isinstance(model_field, InstrumentedAttribute)
and model_field.class_ is not self.resource.Meta.model
):
to_group_by.append(model_field)
q = sa.select([clm.label(lbl) for lbl, clm in fields_to_select.items()])
joins = getattr(self.resource.Meta, "select_from", None)
if joins is not None:
q = q.select_from(joins)
if not self.is_list:
model_id = get_id_field(self.resource)
q = q.where(model_id == self.resource_id)
if self.resource.Meta.auth:
q = self.resource.Meta.auth.can_read(self, q)
if joins is not None:
model_id = get_id_field(self.resource)
q = q.group_by(model_id, *to_group_by)
self.q = q
q = sa.select([clm.label(lbl) for lbl, clm in fields_to_select.items()])
joins = getattr(self.resource.Meta, "select_from", None)
if joins is not None:
q = q.select_from(joins)
if not self.is_list:
model_id = get_id_field(self.resource)
q = q.where(model_id == self.resource_id)
if self.resource.Meta.auth:
q = self.resource.Meta.auth.can_read(self, q)
if joins is not None:
model_id = get_id_field(self.resource)
q = q.group_by(model_id, *to_group_by)
self.q = q
def __add_related_payload(self, related_res, related_data: list):
if related_res.Meta.name in self.related_payload:
related_res = related_res()
related_res_id_field = get_id_field(related_res, name_only=True)
existing_record_ids = [
rec[related_res_id_field]
for rec in self.related_payload[related_res.Meta.name]
]
for rec in related_data:
if rec[related_res_id_field] not in existing_record_ids:
self.related_payload[related_res.Meta.name].append(rec)
else:
self.related_payload[related_res.Meta.name] = related_data