nicer RegSlice.__repr__ (shows # researchers)
[sfa.git] / sfa / storage / model.py
1 from types import StringTypes
2 from datetime import datetime
3
4 from sqlalchemy import Column, Integer, String, DateTime
5 from sqlalchemy import Table, Column, MetaData, join, ForeignKey
6 from sqlalchemy.orm import relationship, backref
7 from sqlalchemy.orm import column_property
8 from sqlalchemy.orm import object_mapper
9 from sqlalchemy.orm import validates
10 from sqlalchemy.ext.declarative import declarative_base
11
12 from sfa.storage.record import Record
13 from sfa.util.sfalogging import logger
14 from sfa.util.sfatime import utcparse, datetime_to_string
15 from sfa.util.xml import XML 
16
17 from sfa.trust.gid import GID
18
19 ##############################
20 Base=declarative_base()
21
22 ####################
23 # dicts vs objects
24 ####################
25 # historically the front end to the db dealt with dicts, so the code was only dealing with dicts
26 # sqlalchemy however offers an object interface, meaning that you write obj.id instead of obj['id']
27 # which is admittedly much nicer
28 # however we still need to deal with dictionaries if only for the xmlrpc layer
29
30 # here are a few utilities for this 
31
32 # (*) first off, when an old pieve of code needs to be used as-is, if only temporarily, the simplest trick
33 # is to use obj.__dict__
34 # this behaves exactly like required, i.e. obj.__dict__['field']='new value' does change obj.field
35 # however this depends on sqlalchemy's implementation so it should be avoided 
36 #
37 # (*) second, when an object needs to be exposed to the xmlrpc layer, we need to convert it into a dict
38 # remember though that writing the resulting dictionary won't change the object
39 # essentially obj.__dict__ would be fine too, except that we want to discard alchemy private keys starting with '_'
40 # 2 ways are provided for that:
41 # . dict(obj)
42 # . obj.todict()
43 # the former dict(obj) relies on __iter__() and next() below, and does not rely on the fields names
44 # although it seems to work fine, I've found cases where it issues a weird python error that I could not get right
45 # so the latter obj.todict() seems more reliable but more hacky as is relies on the form of fields, so this can probably be improved
46 #
47 # (*) finally for converting a dictionary into an sqlalchemy object, we provide
48 # obj.load_from_dict(dict)
49
50 class AlchemyObj(Record):
51     def __iter__(self): 
52         self._i = iter(object_mapper(self).columns)
53         return self 
54     def next(self): 
55         n = self._i.next().name
56         return n, getattr(self, n)
57
58 #    # only intended for debugging 
59 #    def inspect (self, logger, message=""):
60 #        logger.info("%s -- Inspecting AlchemyObj -- attrs"%message)
61 #        for k in dir(self):
62 #            if not k.startswith('_'):
63 #                logger.info ("  %s: %s"%(k,getattr(self,k)))
64 #        logger.info("%s -- Inspecting AlchemyObj -- __dict__"%message)
65 #        d=self.__dict__
66 #        for (k,v) in d.iteritems():
67 #            logger.info("[%s]=%s"%(k,v))
68
69
70 ##############################
71 # various kinds of records are implemented as an inheritance hierarchy
72 # RegRecord is the base class for all actual variants
73 # a first draft was using 'type' as the discriminator for the inheritance
74 # but we had to define another more internal column (classtype) so we 
75 # accomodate variants in types like authority+am and the like
76
77 class RegRecord (Base,AlchemyObj):
78     __tablename__       = 'records'
79     record_id           = Column (Integer, primary_key=True)
80     # this is the discriminator that tells which class to use
81     classtype           = Column (String)
82     # in a first version type was the discriminator
83     # but that could not accomodate for 'authority+sa' and the like
84     type                = Column (String)
85     hrn                 = Column (String)
86     gid                 = Column (String)
87     authority           = Column (String)
88     peer_authority      = Column (String)
89     pointer             = Column (Integer, default=-1)
90     date_created        = Column (DateTime)
91     last_updated        = Column (DateTime)
92     # use the 'type' column to decide which subclass the object is of
93     __mapper_args__     = { 'polymorphic_on' : classtype }
94
95     fields = [ 'type', 'hrn', 'gid', 'authority', 'peer_authority' ]
96     def __init__ (self, type=None, hrn=None, gid=None, authority=None, peer_authority=None, 
97                   pointer=None, dict=None):
98         if type:                                self.type=type
99         if hrn:                                 self.hrn=hrn
100         if gid: 
101             if isinstance(gid, StringTypes):    self.gid=gid
102             else:                               self.gid=gid.save_to_string(save_parents=True)
103         if authority:                           self.authority=authority
104         if peer_authority:                      self.peer_authority=peer_authority
105         if pointer:                             self.pointer=pointer
106         if dict:                                self.load_from_dict (dict)
107
108     def __repr__(self):
109         result="<Record id=%s, type=%s, hrn=%s, authority=%s, pointer=%s" % \
110                 (self.record_id, self.type, self.hrn, self.authority, self.pointer)
111         # skip the uniform '--- BEGIN CERTIFICATE --' stuff
112         if self.gid: result+=" gid=%s..."%self.gid[28:36]
113         else: result+=" nogid"
114         result += ">"
115         return result
116
117     @validates ('gid')
118     def validate_gid (self, key, gid):
119         if gid is None:                     return
120         elif isinstance(gid, StringTypes):  return gid
121         else:                               return gid.save_to_string(save_parents=True)
122
123     def validate_datetime (self, key, incoming):
124         if isinstance (incoming, datetime):     return incoming
125         elif isinstance (incoming, (int,float)):return datetime.fromtimestamp (incoming)
126
127     @validates ('date_created')
128     def validate_date_created (self, key, incoming): return self.validate_datetime (key, incoming)
129
130     @validates ('last_updated')
131     def validate_last_updated (self, key, incoming): return self.validate_datetime (key, incoming)
132
133     # xxx - there might be smarter ways to handle get/set'ing gid using validation hooks 
134     def get_gid_object (self):
135         if not self.gid: return None
136         else: return GID(string=self.gid)
137
138     def just_created (self):
139         now=datetime.now()
140         self.date_created=now
141         self.last_updated=now
142
143     def just_updated (self):
144         now=datetime.now()
145         self.last_updated=now
146
147 ##############################
148 # all subclasses define a convenience constructor with a default value for type, 
149 # and when applicable a way to define local fields in a kwd=value argument
150 ####################
151 class RegAuthority (RegRecord):
152     __tablename__       = 'authorities'
153     __mapper_args__     = { 'polymorphic_identity' : 'authority' }
154     record_id           = Column (Integer, ForeignKey ("records.record_id"), primary_key=True)
155     
156     def __init__ (self, **kwds):
157         # fill in type if not previously set
158         if 'type' not in kwds: kwds['type']='authority'
159         # base class constructor
160         RegRecord.__init__(self, **kwds)
161
162     # no proper data yet, just hack the typename
163     def __repr__ (self):
164         return RegRecord.__repr__(self).replace("Record","Authority")
165
166 ####################
167 # slice x user (researchers) association
168 slice_researcher_table = \
169     Table ( 'slice_researcher', Base.metadata,
170             Column ('slice_id', Integer, ForeignKey ('records.record_id'), primary_key=True),
171             Column ('researcher_id', Integer, ForeignKey ('records.record_id'), primary_key=True),
172             )
173
174 ####################
175 class RegSlice (RegRecord):
176     __tablename__       = 'slices'
177     __mapper_args__     = { 'polymorphic_identity' : 'slice' }
178     record_id           = Column (Integer, ForeignKey ("records.record_id"), primary_key=True)
179     #### extensions come here
180     reg_researchers     = relationship \
181         ('RegUser', 
182          secondary=slice_researcher_table,
183          primaryjoin=RegRecord.record_id==slice_researcher_table.c.slice_id,
184          secondaryjoin=RegRecord.record_id==slice_researcher_table.c.researcher_id,
185          backref="reg_slices_as_researcher")
186
187     def __init__ (self, **kwds):
188         if 'type' not in kwds: kwds['type']='slice'
189         RegRecord.__init__(self, **kwds)
190
191     def __repr__ (self):
192         result=RegRecord.__repr__(self).replace("Record","Slice")
193         result=result.replace(">","")
194         result += " [%d res.]"%(len(self.reg_researchers))
195         result += ">"
196         return result
197
198 ####################
199 class RegNode (RegRecord):
200     __tablename__       = 'nodes'
201     __mapper_args__     = { 'polymorphic_identity' : 'node' }
202     record_id           = Column (Integer, ForeignKey ("records.record_id"), primary_key=True)
203     
204     def __init__ (self, **kwds):
205         if 'type' not in kwds: kwds['type']='node'
206         RegRecord.__init__(self, **kwds)
207
208     def __repr__ (self):
209         return RegRecord.__repr__(self).replace("Record","Node")
210
211 ####################
212 class RegUser (RegRecord):
213     __tablename__       = 'users'
214     # these objects will have type='user' in the records table
215     __mapper_args__     = { 'polymorphic_identity' : 'user' }
216     record_id           = Column (Integer, ForeignKey ("records.record_id"), primary_key=True)
217     #### extensions come here
218     email               = Column ('email', String)
219     # can't use name 'keys' here because when loading from xml we're getting
220     # a 'keys' tag, and assigning a list of strings in a reference column like this crashes
221     reg_keys            = relationship \
222         ('RegKey', backref='reg_user',
223          cascade="all, delete, delete-orphan")
224     
225     # so we can use RegUser (email=.., hrn=..) and the like
226     def __init__ (self, **kwds):
227         # handle local settings
228         if 'email' in kwds: self.email=kwds.pop('email')
229         if 'type' not in kwds: kwds['type']='user'
230         RegRecord.__init__(self, **kwds)
231
232     # append stuff at the end of the record __repr__
233     def __repr__ (self): 
234         result = RegRecord.__repr__(self).replace("Record","User")
235         result.replace (">"," email=%s"%self.email)
236         result += ">"
237         return result
238
239     @validates('email') 
240     def validate_email(self, key, address):
241         assert '@' in address
242         return address
243
244 ####################
245 # xxx tocheck : not sure about eager loading of this one
246 # meaning, when querying the whole records, we expect there should
247 # be a single query to fetch all the keys 
248 # or, is it enough that we issue a single query to retrieve all the keys 
249 class RegKey (Base):
250     __tablename__       = 'keys'
251     key_id              = Column (Integer, primary_key=True)
252     record_id             = Column (Integer, ForeignKey ("records.record_id"))
253     key                 = Column (String)
254     pointer             = Column (Integer, default = -1)
255     
256     def __init__ (self, key, pointer=None):
257         self.key=key
258         if pointer: self.pointer=pointer
259
260     def __repr__ (self):
261         result="<key id=%s key=%s..."%(self.key_id,self.key[8:16],)
262         try:    result += " user=%s"%self.reg_user.record_id
263         except: result += " no-user"
264         result += ">"
265         return result
266
267 ##############################
268 # although the db needs of course to be reachable for the following functions
269 # the schema management functions are here and not in alchemy
270 # because the actual details of the classes need to be known
271 # migrations: this code has no notion of the previous versions
272 # of the data model nor of migrations
273 # sfa.storage.migrations.db_init uses this when starting from
274 # a fresh db only
275 def init_tables(engine):
276     logger.info("Initializing db schema from current/latest model")
277     Base.metadata.create_all(engine)
278
279 def drop_tables(engine):
280     logger.info("Dropping tables from current/latest model")
281     Base.metadata.drop_all(engine)
282
283 ##############################
284 # create a record of the right type from either a dict or an xml string
285 def make_record (dict={}, xml=""):
286     if dict:    return make_record_dict (dict)
287     elif xml:   return make_record_xml (xml)
288     else:       raise Exception("make_record has no input")
289
290 # convert an incoming record - typically from xmlrpc - into an object
291 def make_record_dict (record_dict):
292     assert ('type' in record_dict)
293     type=record_dict['type'].split('+')[0]
294     if type=='authority':
295         result=RegAuthority (dict=record_dict)
296     elif type=='user':
297         result=RegUser (dict=record_dict)
298     elif type=='slice':
299         result=RegSlice (dict=record_dict)
300     elif type=='node':
301         result=RegNode (dict=record_dict)
302     else:
303         logger.debug("Untyped RegRecord instance")
304         result=RegRecord (dict=record_dict)
305     logger.info ("converting dict into Reg* with type=%s"%type)
306     logger.info ("returning=%s"%result)
307     # xxx todo
308     # register non-db attributes in an extensions field
309     return result
310         
311 def make_record_xml (xml):
312     xml_record = XML(xml)
313     xml_dict = xml_record.todict()
314     logger.info("load from xml, keys=%s"%xml_dict.keys())
315     return make_record_dict (xml_dict)
316