def context_bind_fields(context): """ Fields in field rows are late bound so entity values do not appear in the actual context used. This method binds entity values to fields so that all field values can be tested. """ # bound_context = Context(context.flatten()) # Doesn't work for ContextList used for tests context_vals = {} for k in context.keys(): context_vals[k] = context[k] bound_context = Context(context_vals) bound_rows = [] for row in context['fields']: if 'row_field_descs' in row: entity_vals = row['entity_value'] extras = row['context_extra_values'] bound_cols = [] for field in row['row_field_descs']: if isinstance(field, FieldDescription): # fields in row are late-bound, so create a binding now field = bound_field(field, entity_vals, context_extra_values=extras) bound_cols.append(field) bound_row = row.copy() bound_row._field_description['row_field_descs'] = bound_cols else: bound_row = row bound_rows.append(bound_row) bound_context['fields'] = bound_rows return bound_context
def render(self, context): """ Renders a repeating field group. `context` is a dictionary-like object that provides information for the rendering operation. returns a string that is incorporated into the resulting web page. `context['field']` is a `bound_field` value that combines the field definition, entity values and additional context information. The entity value is either the entire entity that is currently being rendered, or a list of repeated values that are each formatted using the supplied body template. """ log.info("RenderRepeatGroup.render") try: # log.info("RenderRepeatGroup.render field: %r"%(context['field'],)) # log.info("RenderRepeatGroup.render descs: %r"%(context['field']['group_field_descs'],)) response_parts = [self._template_head.render(context)] repeat_index = 0 extras = context['field']['context_extra_values'] for g in context['field']['field_value']: log.debug("RenderRepeatGroup.render field_val: %r" % (g)) r = [ bound_field(f, g, context_extra_values=extras) for f in context['field']['group_field_descs'] ] repeat_id = context.get('repeat_prefix', "") + context['field']['group_id'] repeat_dict = ({ 'repeat_id': repeat_id, 'repeat_index': str(repeat_index), 'repeat_prefix': repeat_id + ("__%d__" % repeat_index), 'repeat_bound_fields': r, 'repeat_entity': g }) # log.info("RenderRepeatGroup.render repeat_dict: %r"%(repeat_dict)) with context.push(repeat_dict): response_parts.append(self._template_body.render(context)) repeat_index += 1 response_parts.append(self._template_tail.render(context)) except Exception as e: log.exception("Exception in RenderRepeatGroup.render") ex_type, ex, tb = sys.exc_info() traceback.print_tb(tb) response_parts = (["Exception in RenderRepeatGroup.render"] + [repr(e)] + traceback.format_exception(ex_type, ex, tb) + ["***RenderRepeatGroup.render***"]) del tb return "".join(response_parts)
def map_entity_to_context(self, entityvals, context_extra_values=None): """ Returns a bound_field, which is a dictionary-like of values to be added to the display context under construction """ # log.info("map entity %s to context %s, vals %r"%(self.e, self.i, entityvals)) # log.info("map_entity_to_context: bound_field: context_extra_values %r"%(context_extra_values,)) boundfield = bound_field(field_description=self.f, entityvals=entityvals, context_extra_values=context_extra_values) return {self.c: boundfield}
def render(self, context): """ Renders column values for multiple fields in a group `context` is a dictionary-like object that provides information for the rendering operation. `context['field']` contains the group field descriptions. returns a string that is incorporated into the resulting web page. """ # log.info("RenderMultiFields_value.render (mode: %s)"%context['render_mode']) group_field_descs = context['field']['group_field_descs'] if group_field_descs is None: return ( """<span class="value-missing">Missing field group for %(field_id)s</span>"""% context['field'] ) try: # log.info("RenderMultiFields_value.render field: %r"%(context['field'],)) # log.info("RenderMultiFields_value.render descs: %r"%(context['field']['group_field_descs'],)) target_vals = context['field']['target_value'] extras = context['field']['context_extra_values'] # log.debug("RenderMultiFields_value.render target_vals: %r"%(target_vals)) group_fields = [ bound_field(f, target_vals, context_extra_values=extras) for f in group_field_descs ] group_dict = ( { 'group_bound_fields': group_fields # @@TODO: is group_entity actually used anywhere?? , 'group_entity': target_vals }) # log.info("RenderMultiFields_value.render group_dict: %r"%(group_dict)) response_parts = [self._template_head.render(context)] with context.push(group_dict): response_parts.append(self._template_body.render(context)) response_parts.append(self._template_tail.render(context)) except TargetIdNotFound_Error as e: response_parts = [ target_blank%str(e) ] except TargetEntityNotFound_Error as e: response_parts = [ target_missing%str(e) ] except Exception as e: log.exception("Exception in RenderMultiFields_value.render") ex_type, ex, tb = sys.exc_info() traceback.print_tb(tb) response_parts = ( ["Exception in RenderMultiFields_value.render"]+ [repr(e)]+ traceback.format_exception(ex_type, ex, tb)+ ["***RenderMultiFields_value.render***"] ) del tb return "".join(response_parts)
def map_entity_to_context(self, entityvals, context_extra_values=None): """ Add row of fields to display context. The context returned uses a single field with a special renderer that handles expansion of contained fields, wrapped ibn markup that presents the data as a new row. """ rowcontext = bound_field(self.rd, entityvals, context_extra_values=context_extra_values) return {self.c: rowcontext}
def get_view_choices_field(self, viewinfo): """ Returns a bound_field object that displays as a view-choice selection drop-down. """ # @@TODO: Possibly create FieldValueMap and return map_entity_to_context value? # or extract this logic and share? field_description = field_description_from_view_field( viewinfo.collection, {ANNAL.CURIE.field_id: "View_choice"}, None) entityvals = { field_description.get_field_property_uri(): viewinfo.view_id } return bound_field(field_description, entityvals)
def context_list_item_fields(context, entity): """ Returns all list fields to be displayed as a list of bound_field values """ # log.info(context['List_rows']) if 'List_rows' in context: fds = context['List_rows'].description['group_field_descs'] return [bound_field(fd, entity) for fd in fds] elif 'fields' in entity: return entity['fields'] log.warning("No field value found: context %r, entity %r" % (context.keys(), entity.keys())) return None
def map_entity_to_context(self, entityvals, context_extra_values=None): """ Returns a bound_field, which is a dictionary-like of values to be added to the display context under construction """ # log.info("map entity %s to context %s, vals %r"%(self.e, self.i, entityvals)) # log.info("map_entity_to_context: bound_field: context_extra_values %r"%(context_extra_values,)) boundfield = bound_field( field_description=self.f, entityvals=entityvals, context_extra_values=context_extra_values ) return { self.c: boundfield }
def context_list_item_field(context, entity, fid): """ Returns indicated field to be displayed as a bound_field value """ # log.info(context['List_rows']) if 'List_rows' in context: fd = context['List_rows']['group_field_descs'][fid] return bound_field(fd, entity) elif 'fields' in entity: return entity['fields'][fid] log.warning("No field value found: context %r, entity %r" % (context.keys(), entity.keys())) return None
def map_entity_to_context(self, entityvals, context_extra_values=None): """ Add row of fields to display context. The context returned uses a single field with a special renderer that handles expansion of contained fields, wrapped in markup that presents the data as a new row. """ rowcontext = bound_field( self.rd, entityvals, context_extra_values=context_extra_values ) return { self.c: rowcontext }
def render(self, context): """ Renders column values for multiple fields in a group `context` is a dictionary-like object that provides information for the rendering operation. context['field'] is a bound_field combining the reference field description bound to an entity that contains a reference to some target entity. returns a string that is incorporated into the resulting web page. """ # log.info("RenderMultiFields_value.render (mode: %s)"%context['render_mode']) group_field_descs = context['field'].description['group_field_descs'] if group_field_descs is None: return ( """<span class="value-missing">Missing field group for %(field_id)s</span>""" % context['field']) try: target_vals = context['field'].get_targetvals() extras = context['field']['context_extra_values'] group_fields = [ bound_field(f, target_vals, context_extra_values=extras) for f in group_field_descs ] group_dict = ({ 'group_bound_fields': group_fields # @@TODO: is group_entity actually used anywhere?? , 'group_entity': target_vals }) # log.info("RenderMultiFields_value.render group_dict: %r"%(group_dict)) response_parts = [self._template_head.render(context)] with context.push(group_dict): response_parts.append(self._template_body.render(context)) response_parts.append(self._template_tail.render(context)) except TargetIdNotFound_Error as e: response_parts = [target_blank % str(e)] except TargetEntityNotFound_Error as e: response_parts = [target_missing % str(e)] except Exception as e: log.exception("Exception in RenderMultiFields_value.render") ex_type, ex, tb = sys.exc_info() traceback.print_tb(tb) response_parts = (["Exception in RenderMultiFields_value.render"] + [repr(e)] + traceback.format_exception(ex_type, ex, tb) + ["***RenderMultiFields_value.render***"]) del tb return "".join(response_parts)
def context_view_field(context, rownum, colnum): row = context['fields'][rownum] if 'row_field_descs' in row: # Pick column from row field = row['row_field_descs'][colnum] else: # Field is not row-wrapped field = row if isinstance(field, FieldDescription): # fields in row are late-bound, so create a binding now entity_vals = context['fields'][rownum]['entity_value'] extras = context['fields'][rownum]['context_extra_values'] field = bound_field(field, entity_vals, context_extra_values=extras) return field
def get_list_choices_field(self, listinfo): """ Returns a bound_field object that displays as a list-choice selection drop-down. """ # @@TODO: Possibly create FieldValueMap and return map_entity_to_context value? # or extract this logic and share? See also entityedit view choices field_description = field_description_from_view_field( listinfo.collection, { ANNAL.CURIE.field_id: "List_choice" , ANNAL.CURIE.field_placement: "small:0,12;medium:0,9" }, {} ) entityvals = { field_description['field_property_uri']: listinfo.list_id } return bound_field(field_description, entityvals)
def get_list_choices_field(self, listinfo): """ Returns a bound_field object that displays as a list-choice selection drop-down. """ # @@TODO: Possibly create FieldValueMap and return map_entity_to_context value? # or extract this logic and share? See also entityedit view choices field_description = field_description_from_view_field( listinfo.collection, { ANNAL.CURIE.field_id: "List_choice", ANNAL.CURIE.field_placement: "small:0,12;medium:5,5" }, {}) entityvals = { field_description['field_property_uri']: listinfo.list_id } return bound_field(field_description, entityvals)
def context_view_repeat_fields(context, repeat_bound_field): """ Returns a list of lists of bound fields corresponding to the items of a repeated value display field. Result is indexed by [row][field] to get individual bound fields """ field_descs = repeat_bound_field.description['group_field_descs'] extras = repeat_bound_field['context_extra_values'] bound_rows = [] for row_vals in repeat_bound_field.field_value: r = [] for field_desc in field_descs: f = bound_field(field_desc, row_vals, context_extra_values=extras) r.append(f) bound_rows.append(r) return bound_rows
def render(self, context): """ Renders column labels for multiple fields in a group `context` is a dictionary-like object that provides information for the rendering operation. `context['field']` contains the group field descriptions. returns a string that is incorporated into the resulting web page. """ group_field_descs = context['field'].description['group_field_descs'] if group_field_descs is None: return ( """<span class="value-missing">Missing field group for %(group_id)s</span>""" % context['field']) try: group_fields = [bound_field(f, {}) for f in group_field_descs] group_dict = ({'group_bound_fields': group_fields}) log.info("RenderMultiFields_label.render group_dict: %r" % (group_dict)) with context.push(group_dict): response_parts = [self._template_head.render(context)] response_parts.append(self._template_body.render(context)) response_parts.append(self._template_tail.render(context)) except TargetIdNotFound_Error as e: response_parts = [target_blank % str(e)] except TargetEntityNotFound_Error as e: response_parts = [target_missing % str(e)] except Exception as e: log.exception("Exception in RenderMultiFields_label.render") ex_type, ex, tb = sys.exc_info() traceback.print_tb(tb) response_parts = (["Exception in RenderMultiFields_label.render"] + [repr(e)] + traceback.format_exception(ex_type, ex, tb) + ["***RenderMultiFields_label.render***"]) del tb return "".join(response_parts)
def render(self, context): """ Renders multiple fields in a row `context` is a dictionary-like object that provides information for the rendering operation. `context['row_bound_fields']` provides a list of bound fields to be rendered. returns a string that is incorporated into the resulting web page. """ # log.info("RenderFieldRow.render (mode: %s)"%context['render_mode']) # row_bound_fields = context['field']['row_bound_fields'] # log.info("RenderFieldRow.render field: %r"%(context['field'],)) # log.info("RenderFieldRow.render descs: %r"%(context['field']['row_bound_fields'],)) try: row_field_descs = context['field']['row_field_descs'] entity_vals = context['field']['entity_value'] extras = context['field']['context_extra_values'] row_bound_fields = [ bound_field(f, entity_vals, context_extra_values=extras) for f in row_field_descs ] with context.push({'row_bound_fields': row_bound_fields}): response_parts = [self._template_head.render(context)] response_parts.append(self._template_body.render(context)) response_parts.append(self._template_tail.render(context)) except Exception as e: log.exception("Exception in RenderFieldRow.render") ex_type, ex, tb = sys.exc_info() traceback.print_tb(tb) response_parts = ( ["Exception in RenderFieldRow.render"]+ [repr(e)]+ traceback.format_exception(ex_type, ex, tb)+ ["***RenderFieldRow.render***"] ) del tb return "".join(response_parts)