def __init__(self, driver=None, cache=None):
     if driver:
         self.driver = driver
     else:
         self.driver = PyMongoDriver('frustratio', 'broadcasts')
     if cache:
         self.cache = cache
     else:
         self.cache = RedisDriver()
 def __init__(self, driver=None, cache=None):
     if driver:
         self.driver = driver
     else:
         self.driver = PyMongoDriver('frustratio', 'broadcasts')
     if cache:
         self.cache = cache
     else:
         self.cache = RedisDriver()
class BroadcastsDataStore():
    def __init__(self, driver=None, cache=None):
        if driver:
            self.driver = driver
        else:
            self.driver = PyMongoDriver('frustratio', 'broadcasts')
        if cache:
            self.cache = cache
        else:
            self.cache = RedisDriver()

    def get_empty_broadcast(self):
        return Broadcast()

    def create(self, broadcast, appType=None):
        '''Store a newly created Broadcast instance.
        '''
        if appType:
            namespace = appType + '_GLOBAL'
        else:
            namespace = 'GLOBAL'
        broadcast.createdTime = int(time.time())
        head = self.read_head(namespace)
        if head:
            if namespace in broadcast.nextBroadcastIDs:
                broadcast.nextBroadcastIDs[namespace].append(head.globalID)
            else:
                broadcast.nextBroadcastIDs[namespace] = [head.globalID]
            response = self.driver.replace(namespace + '_HEAD',
                                           broadcast.to_dictionary())
        else:
            response = self.driver.insert(namespace + '_HEAD',
                                          broadcast.to_dictionary())
        response = self.driver.insert(broadcast.id, broadcast.to_dictionary())
        # Push to cache.
        self.cache.create(broadcast.id, json.dumps(broadcast.to_dictionary()))
        self.cache.create(namespace + '_HEAD',
                          json.dumps(broadcast.to_dictionary()))
        # TODO: Throw exception here instead.
        if response:
            return broadcast
        else:
            return None

    def read_head(self, namespace='GLOBAL'):
        '''Return the current head of the Broadcasts list.
        '''
        return self.read(namespace + '_HEAD')

    def read(self, id=None):
        '''Return a Broadcast instance given an ID.
        '''
        cacheResponse = self.cache.read(id)
        if cacheResponse:
            response = json.loads(cacheResponse)
        else:
            response = self.driver.get(id)
        if not response:
            return None
        broadcast = Broadcast()
        broadcast.from_dictionary(response)
        return broadcast

    def get_last(self, n, appType):
        output = []
        count = 0
        if appType:
            namespace = appType + '_GLOBAL'
        else:
            namespace = 'GLOBAL'
        node = self.read_head(namespace)
        #print(node.to_dictionary())
        while node and count < n:
            output.append(node)
            #print(node.to_dictionary())
            nextBroadcastIDs = node.nextBroadcastIDs.get(namespace)
            if nextBroadcastIDs and len(nextBroadcastIDs) >= 1:
                nextNodeID = node.nextBroadcastIDs.get(namespace)[0]
            else:
                nextNodeID = None
            node = self.read(nextNodeID)
            count += 1
        return output

    # TODO: Implement caching for this.
    def read_multiple(self, ids=[]):
        '''Return a list/dict of Broadcast instances given a list of IDs.
        '''
        if not ids:
            return {}
        response = self.driver.get_multiple(ids)
        output = {}
        for x in response:
            output[x] = Broadcast()
            output[x].from_dictionary(response[x])
        return output
Beispiel #4
0
 def __init__(self, driver=None):
     if driver:
         self.driver = driver
     else:
         self.driver = PyMongoDriver('frustratio', 'users')
Beispiel #5
0
class UsersDataStore():
    def __init__(self, driver=None):
        if driver:
            self.driver = driver
        else:
            self.driver = PyMongoDriver('frustratio', 'users')

    def get_empty_user(self):
        return User()

    def get_protected_fields(self):
        user = User()
        return user.get_protected_fields()

    def create(self, user):
        response = self.driver.insert(user.id, user.to_dictionary())
        # TODO: Throw exception here instead.
        if response:
            return user
        else:
            return None

    def read(self, userID):
        '''Return an User instance given an ID.
        '''
        response = self.driver.get(userID)
        user = User()
        if response:
            user.from_dictionary(response)
        return user

    def read_by_field(self, field, id):
        response = self.driver.get_by_field(field, id)
        user = User()
        if response:
            user.from_dictionary(response)
        return user

    def read_multiple(self, userIDs, asDictionary=True):
        '''Return dictionary (default) or list of User instances.
        Use dictionary for easier indexing.
        Use list for order preservation.
        '''
        response = self.driver.get_multiple(userIDs, asDictionary)
        if asDictionary:
            output = {}
            for x in response:
                output[x] = User()
                output[x].from_dictionary(response[x])
        else:
            output = []
            for x in response:
                user = User()
                user.from_dictionary()
                output.append(user)
        return output

    def update_fields(self, userID, fields={}):
        '''Update a specific User in the data store given its fields.

        Very close to the data store implementation.
        '''
        protectedFields = self.get_protected_fields()
        for x in fields:
            if x in protectedFields:
                return None
        return self.driver.update(userID, fields)

    # TODO: Move this to a parent class or possibly a utility class.
    def _to_dictionary(self, objectsDictionary):
        output = {}
        for x in objectsDictionary:
            output[x] = objectsDictionary[x].to_dictionary()
        return output
class BroadcastsDataStore():

    def __init__(self, driver=None, cache=None):
        if driver:
            self.driver = driver
        else:
            self.driver = PyMongoDriver('frustratio', 'broadcasts')
        if cache:
            self.cache = cache
        else:
            self.cache = RedisDriver()

    def get_empty_broadcast(self):
        return Broadcast()

    def create(self, broadcast, appType=None):
        '''Store a newly created Broadcast instance.
        '''
        if appType:
            namespace = appType + '_GLOBAL'
        else:
            namespace = 'GLOBAL'
        broadcast.createdTime = int(time.time())
        head = self.read_head(namespace)
        if head:
            if namespace in broadcast.nextBroadcastIDs:
                broadcast.nextBroadcastIDs[namespace].append(head.globalID)
            else:
                broadcast.nextBroadcastIDs[namespace] = [head.globalID]
            response = self.driver.replace(
                namespace + '_HEAD',
                broadcast.to_dictionary()
            )
        else:
            response = self.driver.insert(
                namespace + '_HEAD',
                broadcast.to_dictionary()
            )
        response = self.driver.insert(
            broadcast.id,
            broadcast.to_dictionary()
        )
        # Push to cache.
        self.cache.create(
            broadcast.id,
            json.dumps(broadcast.to_dictionary())
        )
        self.cache.create(
            namespace + '_HEAD',
            json.dumps(broadcast.to_dictionary())
        )
        # TODO: Throw exception here instead.
        if response:
            return broadcast
        else:
            return None

    def read_head(self, namespace='GLOBAL'):
        '''Return the current head of the Broadcasts list.
        '''
        return self.read(namespace + '_HEAD')

    def read(self, id=None):
        '''Return a Broadcast instance given an ID.
        '''
        cacheResponse = self.cache.read(id)
        if cacheResponse:
            response = json.loads(cacheResponse)
        else:
            response = self.driver.get(id)
        if not response:
            return None
        broadcast = Broadcast()
        broadcast.from_dictionary(response)
        return broadcast

    def get_last(self, n, appType):
        output = []
        count = 0
        if appType:
            namespace = appType + '_GLOBAL'
        else:
            namespace = 'GLOBAL'
        node = self.read_head(namespace)
        #print(node.to_dictionary())
        while node and count < n:
            output.append(node)
            #print(node.to_dictionary())
            nextBroadcastIDs = node.nextBroadcastIDs.get(namespace)
            if nextBroadcastIDs and len(nextBroadcastIDs) >= 1:
                nextNodeID = node.nextBroadcastIDs.get(namespace)[0]
            else:
                nextNodeID = None
            node = self.read(nextNodeID)
            count += 1
        return output

    # TODO: Implement caching for this.
    def read_multiple(self, ids=[]):
        '''Return a list/dict of Broadcast instances given a list of IDs.
        '''
        if not ids:
            return {}
        response = self.driver.get_multiple(ids)
        output = {}
        for x in response:
            output[x] = Broadcast()
            output[x].from_dictionary(response[x])
        return output
Beispiel #7
0
 def __init__(self, driver=None):
     if driver:
         self.driver = driver
     else:
         self.driver = PyMongoDriver('frustratio', 'users')
Beispiel #8
0
class UsersDataStore():

    def __init__(self, driver=None):
        if driver:
            self.driver = driver
        else:
            self.driver = PyMongoDriver('frustratio', 'users')

    def get_empty_user(self):
        return User()

    def get_protected_fields(self):
        user = User()
        return user.get_protected_fields()

    def create(self, user):
        response = self.driver.insert(user.id, user.to_dictionary())
        # TODO: Throw exception here instead.
        if response:
            return user
        else:
            return None

    def read(self, userID):
        '''Return an User instance given an ID.
        '''
        response = self.driver.get(userID)
        user = User()
        if response:
            user.from_dictionary(response)
        return user

    def read_by_field(self, field, id):
        response = self.driver.get_by_field(field, id)
        user = User()
        if response:
            user.from_dictionary(response)
        return user

    def read_multiple(self, userIDs, asDictionary=True):
        '''Return dictionary (default) or list of User instances.
        Use dictionary for easier indexing.
        Use list for order preservation.
        '''
        response = self.driver.get_multiple(userIDs, asDictionary)
        if asDictionary:
            output = {}
            for x in response:
                output[x] = User()
                output[x].from_dictionary(response[x])
        else:
            output = []
            for x in response:
                user = User()
                user.from_dictionary()
                output.append(user)
        return output

    def update_fields(self, userID, fields={}):
        '''Update a specific User in the data store given its fields.

        Very close to the data store implementation.
        '''
        protectedFields = self.get_protected_fields()
        for x in fields:
            if x in protectedFields:
                return None
        return self.driver.update(userID, fields)

    # TODO: Move this to a parent class or possibly a utility class.
    def _to_dictionary(self, objectsDictionary):
        output = {}
        for x in objectsDictionary:
            output[x] = objectsDictionary[x].to_dictionary()
        return output