def _make_new_property(state, record_id, property_name, data_type, signer): property_address = addressing.make_property_address( record_id, property_name, 0) property_container = _get_container(state, property_address) new_prop = Property( name=property_name, record_id=record_id, data_type=data_type, reporters=[ Property.Reporter( public_key=signer, authorized=True, index=0, ) ], current_page=1, wrapped=False, ) property_container.entries.extend([new_prop]) property_container.entries.sort(key=lambda prop: prop.name) _set_container(state, property_address, property_container)
def _accept_proposal(state, signer, proposal, timestamp): record_id, issuing_agent, receiving_agent, role, properties = \ proposal.record_id, proposal.issuing_agent, \ proposal.receiving_agent, proposal.role, proposal.properties record, record_container, record_address = _get_record(state, record_id) if role == Proposal.OWNER: if not _is_owner(record, issuing_agent): LOGGER.info('Issuing agent is not owner') return Proposal.CANCELED record.owners.extend([ Record.AssociatedAgent( agent_id=receiving_agent, timestamp=timestamp) ]) record.owners.sort(key=lambda agent: agent.timestamp) _set_container(state, record_address, record_container) # Authorize the new owner as a reporter on all of the record's # properties and deauthorize the old owner, leaving everything # else as-is record_type, _, _ = _get_record_type(state, record.record_type) for prop_name in (prop.name for prop in record_type.properties): prop, prop_container, prop_address = _get_property( state, record_id, prop_name) old_owner = next( reporter for reporter in prop.reporters if reporter.public_key == issuing_agent ) old_owner.authorized = False try: new_owner = next( reporter for reporter in prop.reporters if reporter.public_key == receiving_agent ) if not new_owner.authorized: new_owner.authorized = True _set_container(state, prop_address, prop_container) except StopIteration: new_owner = Property.Reporter( public_key=receiving_agent, authorized=True, index=len(prop.reporters), ) prop.reporters.extend([new_owner]) _set_container(state, prop_address, prop_container) return Proposal.ACCEPTED elif role == Proposal.CUSTODIAN: if not _is_custodian(record, issuing_agent): LOGGER.info('Issuing agent is not custodian') return Proposal.CANCELED record.custodians.extend([ Record.AssociatedAgent( agent_id=receiving_agent, timestamp=timestamp) ]) record.custodians.sort(key=lambda agent: agent.timestamp) _set_container(state, record_address, record_container) return Proposal.ACCEPTED elif role == Proposal.REPORTER: if not _is_owner(record, issuing_agent): LOGGER.info('Issuing agent is not owner') return Proposal.CANCELED for prop_name in properties: prop, container, address = _get_property( state, record_id, prop_name) prop.reporters.extend([ Property.Reporter( public_key=signer, authorized=True, index=len(prop.reporters), ) ]) _set_container(state, address, container) return Proposal.ACCEPTED