Commits

askory  committed 4a345e9

Modified structure of VirtualPhone and VirtualMessage models for commandloop backend, including adding a flag for inbound vs. outbound messages (so to/from_num can be arbitrary). Implemented send() and get_messages(), but still haven't implemented checker structure.

  • Participants
  • Parent commits 5172c96

Comments (0)

Files changed (2)

File backends/commandloop/connection.py

-import backends.commandloop.util
+from backends.commandloop.helper import *
 from core.message_set import MessageSet
 from core.connection import Connection as SuperConn
-from helper import *
 
 class Connection(SuperConn):
     """Manages connections with user modifiable MongoDB"""
     
     def get_messages(self):
-        """Retrieves messages from MongoDB"""
-        messages = MessageSet(vm.to_message for vm in 
+        """Retrieves unread inbound messages from commandloop DB"""
+        messages = MessageSet(
+            vm.to_message for vm in VirtualMessage.objects(
+                is_read=False,
+                direction=INBOUND
+            )
+        )
 
     def send(self,msg):
-        """Mimic sending a message
-        Send a message to a different MongoDB specified by phone number in msg
-        Also creates outgoing message in sender MongoDB"""
-
-        pass
+        """Mimic sending a message by writing to the commandloop db setting
+        direction flag to OUTBOUND"""
+        VirtualMessage.from_message(msg,OUTBOUND).save()
 
 def __init__(self,cid,**args):
-    """Connection to single MongoDB
+    """Connection to a fake SMS gateway that is actually a MongoDB
+
     kwargs:
-        NUMBER = phoneish number
-        reference to db #TODO"""
 
-    print "Connecting to MongoDB that mimics phone # " % args['NUMBER']
-
-    self.db = ME.connect('virtualbackend')
-    self.vphone = VirtualPhone(number=args['NUMBER'])
-    self.vphone.save()
+        NUMBER  --   <string> phoneish number"""
 
     self.id = cid
+    self.num = args['NUMBER']
+    self.backend = 'commandloop'
 
-    self.num = args['NUMBER']
-    self.backend = 'VirtualPhone'
-
-
-

File backends/commandloop/helper.py

 from core.message import Message
 import mongoengine as ME
 
+INBOUND = 1
+OUTBOUND = -1
+
 ME.connect('commandloop')
 
 class VirtualPhone(ME.Document):
+
     number = ME.StringField(required=True)
-    messages = ME.ListField(EmbeddedDocument(VirtualMessage))
 
+    def __repr__(self):
+        return "<VirtualPhone {number: \"%s\"}>" % self.number
 
-class VirtualMessage(ME.EmbeddedDocument):
+class VirtualMessage(ME.Document):
     
-    from_num = ME.StringField(required=True)
-    to_num = ME.StringField(required=True)
+    direction = ME.IntField
+    from_phone = ME.ReferenceField(VirtualPhone)
+    to_phone = ME.ReferenceField(VirtualPhone)
     text = ME.StringField(required=True)
     datetime = ME.DateTimeField(required=True)
     labels = ME.ListField(ME.StringField(max_length=50))
-    unread = ME.BooleanField(default=True)
+    is_read = ME.BooleanField(default=True)
     
     def to_message(self):
         return Message(
-            from_num=self.from_num,
-            to_num=self.to_num,
+            from_num=self.from_phone.number,
+            to_num=self.to_phone.number,
             text=self.text,
             datetime = self.datetime,
             labels = self.labels,
-            unread = self.unread
+            is_read = self.is_read
         )
+        
+    @staticmethod
+    def from_message(msg,direction=OUTBOUND):
+
+        to_phone = VirtualPhone.objects.get_or_create(number=msg.to_num)[0]
+        from_phone = VirtualPhone.objects.get_or_create(number=msg.from_num)[0]
+
+        return VirtualMessage(
+            from_phone=from_phone,
+            to_phone=to_phone,
+            text=msg.text,
+            datetime = msg.datetime,
+            labels = msg.labels,
+            is_read = msg.is_read,
+            direction = direction
+        )
+
+        
+    def __repr__(self):
+        return "<VirtualMessage {from: \"%s\", to: \"%s\"}>" % (self.from_phone.number, self.to_phone.number)