AiC and REST login
[myslice.git] / rest / __init__.py
index faef22e..fccfede 100644 (file)
@@ -1,12 +1,13 @@
-from manifold.core.query            import Query
-from manifoldapi.manifoldapi        import execute_query
+import decimal
+import datetime
+import json
 
 from django.http                    import HttpResponse
 
+from manifold.core.query            import Query
+from manifoldapi.manifoldapi        import execute_query
 
-import decimal
-import datetime
-import json
+from myslice.settings               import logger
 
 # handles serialization of datetime in json
 DateEncoder = lambda obj: obj.strftime("%B %d, %Y %H:%M:%S") if isinstance(obj, datetime.datetime) else None
@@ -40,21 +41,32 @@ class ObjectRequest(object):
             self.filters['disabled'] = '0'
             self.filters['gateway_type'] = 'sfa'
             self.filters['platform'] = '!myslice'
-        elif(self.type.startswith('local:')):
-            # XXX TODO: find a generic Query to get the fields like 
-            # select column.name from local:object where table == local:user
+        #elif(self.type.startswith('local:')):
+        elif ':' in self.type:
             table = self.type.split(':')
+            prefix = table[0]
             table = table[1]
-            if table == "user":
-                self.id = table + '_id'
-                self.fields = ['user_id', 'email', 'password', 'config','status'];
-            elif table == "account":
-                # XXX TODO: Multiple key for account = (platform_id, user_id)
-                self.id = "platform_id, user_id"
-                self.fields = ['platform_id', 'user_id', 'auth_type', 'config'];
-            elif table == "platform":
-                self.id = 'platform'
-                self.fields = ['platform', 'platform_longname', 'platform_url', 'platform_description','gateway_type'];
+            if prefix == 'local':
+                # XXX TODO: find a generic Query to get the fields like 
+                # select column.name from local:object where table == local:user
+                table = self.type.split(':')
+                table = table[1]
+                if table == "user":
+                    self.id = table + '_id'
+                    self.fields = ['user_id', 'email', 'password', 'config','status'];
+                elif table == "account":
+                    # XXX TODO: Multiple key for account = (platform_id, user_id)
+                    self.id = "platform_id, user_id"
+                    self.fields = ['platform_id', 'user_id', 'auth_type', 'config'];
+                elif table == "platform":
+                    self.id = 'platform'
+                    self.fields = ['platform', 'platform_longname', 'platform_url', 'platform_description','gateway_type'];
+            else:
+                # If we use prefix, set the key without the prefix then add it again
+                self.type = table
+                self.setKey()
+                self.setLocalFields()
+                self.type = prefix + ':' + table
         else :
             self.setKey()
             self.setLocalFields()
@@ -63,7 +75,7 @@ class ObjectRequest(object):
         # What about key formed of multiple fields???
         query = Query.get('local:object').filter_by('table', '==', self.type).select('key')
         results = execute_query(self.request, query)
-        print "key of object = %s" % results
+        logger.debug("key of object = {}".format(results))
         if results :
             for r in results[0]['key'] :
                 self.id = r
@@ -102,6 +114,8 @@ class ObjectRequest(object):
                     query.filter_by(k, '<=', f[2:])
                 elif (f[:1] == "<") :
                     query.filter_by(k, '<', f[1:])
+                elif (f[:8] == "CONTAINS") :
+                    query.filter_by(k, 'CONTAINS', f[8:])
                 else :
                     query.filter_by(k, '==', f)
         return query
@@ -122,9 +136,9 @@ class ObjectRequest(object):
         if self.params :
             for p in self.params :
                 for k,v in p.iteritems() :
-                    print "param: %s : %s" % (k,v)
+                    logger.debug("param: {} : {}".format(k, v))
                     query.set({k : v})
-            print "query = ",query
+            logger.debug("query = {}".format(query))
         else:
             raise Exception, "Params are required for create"
         return execute_query(self.request, query)
@@ -136,9 +150,9 @@ class ObjectRequest(object):
         if self.params :
             for p in self.params :
                 for k,v in p.iteritems() :
-                    print "param: %s : %s" % (k,v)
+                    logger.debug("param: {} : {}".format(k, v))
                     query.set({k : v})
-            print "query = ",query
+            logger.debug("query = {}".format(query))
         else:
             raise Exception, "Params are required for update"
 
@@ -153,7 +167,7 @@ class ObjectRequest(object):
         if self.filters :
             query.set(self.filters)
         else:
-            raise Exception, "Filters are required for update"
+            raise Exception, "Filters are required for delete"
         return execute_query(self.request, query)
     
     def json(self):