set last_updated and date_created as read only fields
[sfa.git] / sfa / util / record.py
index 3075edb..45b77db 100644 (file)
@@ -14,6 +14,8 @@ from sfa.trust.gid import *
 import sfa.util.report
 from sfa.util.rspec import *
 from sfa.util.parameter import *
+from sfa.util.misc import *
+
 
 class GeniRecord(dict):
     """ 
@@ -36,14 +38,21 @@ class GeniRecord(dict):
     """
 
     ### the wsdl generator assumes this is named 'fields'
+    internal_fields = {
+        'record_id': Parameter(int, 'An id that uniquely identifies this record'),
+        'pointer': Parameter(int, 'An id that uniquely identifies this record in an external database ')
+    }
+
     fields = {
+        'authority': Parameter(str, "The authority for this record"),
+        'peer_authority': Parameter(str, "The peer authority for this record"),
         'hrn': Parameter(str, "Human readable name of object"),
         'gid': Parameter(str, "GID of the object"),
         'type': Parameter(str, "Record type"),
-        'last_updated': Parameter(int, 'Date and time of last update'),
-        'date_created': Parameter(int, 'Date and time this record was created'),
+        'last_updated': Parameter(int, 'Date and time of last update', ro=True),
+        'date_created': Parameter(int, 'Date and time this record was created', ro=True),
     }
-
+    all_fields = dict(fields.items() + internal_fields.items())
     ##
     # Create a Geni Record
     #
@@ -53,12 +62,13 @@ class GeniRecord(dict):
     # @param pointer is a pointer to a PLC record
     # @param dict if !=None, then fill in this record from the dictionary
 
-    def __init__(self, hrn=None, gid=None, type=None, pointer=None, dict=None, string=None):
+    def __init__(self, hrn=None, gid=None, type=None, pointer=None, peer_authority=None, dict=None, string=None):
         self.dirty = True
         self.hrn = None
         self.gid = None
         self.type = None
         self.pointer = None
+        self.set_peer_auth(peer_authority)
         if hrn:
             self.set_name(hrn)
         if gid:
@@ -71,8 +81,7 @@ class GeniRecord(dict):
             self.load_from_dict(dict)
         if string:
             self.load_from_string(string)
-
-    
+        
     def update(self, new_dict):
         if isinstance(new_dict, list):
             new_dict = new_dict[0]
@@ -96,6 +105,7 @@ class GeniRecord(dict):
         Set the name of the record
         """
         self.hrn = hrn
+        self['hrn'] = hrn
         self.dirty = True
 
     ##
@@ -110,8 +120,10 @@ class GeniRecord(dict):
 
         if isinstance(gid, StringTypes):
             self.gid = gid
+            self['gid'] = gid
         else:
             self.gid = gid.save_to_string(save_parents=True)
+            self['gid'] = gid.save_to_string(save_parents=True)
         self.dirty = True
 
     ##
@@ -124,6 +136,7 @@ class GeniRecord(dict):
         Set the type of the record
         """
         self.type = type
+        self['type'] = type
         self.dirty = True
 
     ##
@@ -136,6 +149,13 @@ class GeniRecord(dict):
         Set the pointer of the record
         """
         self.pointer = pointer
+        self['pointer'] = pointer
+        self.dirty = True
+
+
+    def set_peer_auth(self, peer_authority):
+        self.peer_authority = peer_authority
+        self['peer_authority'] = peer_authority
         self.dirty = True
 
     ##
@@ -180,19 +200,6 @@ class GeniRecord(dict):
         """
         return GID(string=self.gid)
 
-    ##
-    # Return a key that uniquely identifies this record among all records in
-    # Geni. This key is used to uniquely identify the record in the Geni
-    # database.
-
-    def get_key(self):
-        """
-        Return a key that uniquely identifies this record among all records in
-        Geni. This key is used to uniquely identify the record in the Geni
-        database.
-        """
-        return self.hrn + "#" + self.type
-
     ##
     # Returns a list of field names in this record. 
 
@@ -211,10 +218,13 @@ class GeniRecord(dict):
         """
         Given a field name ("hrn", "gid", ...) return the value of that field.
         """
-        if fieldname == "key":
-            val = self.get_key()
+        if fieldname == "authority":
+            val = get_authority(self['hrn'])
         else:
-            val = getattr(self, fieldname)
+            try:
+                val = getattr(self, fieldname)
+            except:
+                val = self[fieldname] 
         if isinstance(val, str):
             return "'" + str(val) + "'"
         else:
@@ -229,10 +239,7 @@ class GeniRecord(dict):
         """
         Given a list of field names, return a list of values for those public.
         """
-        strs = []
-        for fieldname in fieldnames:
-            strs.append(self.get_field_value_string(fieldname))
-        return strs
+        return [ self.get_field_value_string (fieldname) for fieldname in fieldnames ]
 
     ##
     # Return the record in the form of a dictionary
@@ -321,7 +328,7 @@ class GeniRecord(dict):
             if key not in order:
                 order.append(key)
         for key in order:
-            if key in (self and self.fields):
+            if key in self and key in self.fields:
                 if key in 'gid' and self[key]:
                     gid = GID(string=self[key])
                     print "     %s:" % key