+# all subclasses define a convenience constructor with a default value for type,
+# and when applicable a way to define local fields in a kwd=value argument
+####################
+
+
+class RegAuthority(RegRecord):
+ __tablename__ = 'authorities'
+ __mapper_args__ = {'polymorphic_identity': 'authority'}
+ record_id = Column(Integer, ForeignKey(
+ "records.record_id"), primary_key=True)
+ # extensions come here
+ name = Column('name', String)
+ # extensions come here
+ reg_pis = relationship \
+ ('RegUser',
+ secondary=authority_pi_table,
+ primaryjoin=RegRecord.record_id == authority_pi_table.c.authority_id,
+ secondaryjoin=RegRecord.record_id == authority_pi_table.c.pi_id,
+ backref='reg_authorities_as_pi',
+ )
+
+ def __init__(self, **kwds):
+ # handle local settings
+ if 'name' in kwds:
+ self.name = kwds.pop('name')
+ # fill in type if not previously set
+ if 'type' not in kwds:
+ kwds['type'] = 'authority'
+ # base class constructor
+ RegRecord.__init__(self, **kwds)
+
+ # no proper data yet, just hack the typename
+ def __repr__(self):
+ result = RegRecord.__repr__(self).replace("Record", "Authority")
+# here again trying to display names that can be utf8 is too dangerous
+# result.replace(">", " name={}>".format(self.name))
+ return result
+
+ def update_pis(self, pi_hrns, dbsession):
+ # strip that in case we have <researcher> words </researcher>
+ pi_hrns = [x.strip() for x in pi_hrns]
+ request = dbsession.query(RegUser).filter(RegUser.hrn.in_(pi_hrns))
+ logger.info("RegAuthority.update_pis: %d incoming pis, %d matches found"
+ % (len(pi_hrns), request.count()))
+ pis = dbsession.query(RegUser).filter(RegUser.hrn.in_(pi_hrns)).all()
+ self.reg_pis = pis
+
+####################
+
+
+class RegSlice(RegRecord):
+ __tablename__ = 'slices'
+ __mapper_args__ = {'polymorphic_identity': 'slice'}
+ record_id = Column(Integer, ForeignKey(
+ "records.record_id"), primary_key=True)
+ # extensions come here
+ reg_researchers = relationship \
+ ('RegUser',
+ secondary=slice_researcher_table,
+ primaryjoin=RegRecord.record_id == slice_researcher_table.c.slice_id,
+ secondaryjoin=RegRecord.record_id == slice_researcher_table.c.researcher_id,
+ backref='reg_slices_as_researcher',
+ )
+
+ def __init__(self, **kwds):
+ if 'type' not in kwds:
+ kwds['type'] = 'slice'
+ RegRecord.__init__(self, **kwds)
+
+ def __repr__(self):
+ return RegRecord.__repr__(self).replace("Record", "Slice")
+
+ def update_researchers(self, researcher_hrns, dbsession):
+ # strip that in case we have <researcher> words </researcher>
+ researcher_hrns = [x.strip() for x in researcher_hrns]
+ request = dbsession.query(RegUser).filter(
+ RegUser.hrn.in_(researcher_hrns))
+ logger.info("RegSlice.update_researchers: %d incoming researchers, %d matches found"
+ % (len(researcher_hrns), request.count()))
+ researchers = dbsession.query(RegUser).filter(
+ RegUser.hrn.in_(researcher_hrns)).all()
+ self.reg_researchers = researchers
+
+ # when dealing with credentials, we need to retrieve the PIs attached to a slice
+ # WARNING: with the move to passing dbsessions around, we face a glitch here because this
+ # helper function is called from the trust/ area that
+ def get_pis(self):
+ from sqlalchemy.orm import sessionmaker
+ Session = sessionmaker()
+ dbsession = Session.object_session(self)
+ from sfa.util.xrn import get_authority
+ authority_hrn = get_authority(self.hrn)
+ auth_record = dbsession.query(
+ RegAuthority).filter_by(hrn=authority_hrn).first()
+ return auth_record.reg_pis
+
+ @validates('expires')
+ def validate_expires(self, key, incoming):
+ return self.validate_datetime(key, incoming)
+
+####################
+
+
+class RegNode(RegRecord):
+ __tablename__ = 'nodes'
+ __mapper_args__ = {'polymorphic_identity': 'node'}
+ record_id = Column(Integer, ForeignKey(
+ "records.record_id"), primary_key=True)
+
+ def __init__(self, **kwds):
+ if 'type' not in kwds:
+ kwds['type'] = 'node'
+ RegRecord.__init__(self, **kwds)
+
+ def __repr__(self):
+ return RegRecord.__repr__(self).replace("Record", "Node")
+
+####################
+
+
+class RegUser(RegRecord):
+ __tablename__ = 'users'