Esempio n. 1
0
    def delete_user_data(self, **kwargs):
        "Delete all the user-data"
        # We might get a UUID or a client_name/service_name/user_id tuple.
        uuid = kwargs.get('uuid')
        if not uuid:
            assert kwargs.get('client_name') != None
            assert kwargs.get('service_name') != None
            assert kwargs.get('user_id') != None

            authz = yield Authz.find(where=self.args_to_where(**kwargs), limit=1)
            if authz:
                uuid = authz.uuid
            else:
                defer.returnValue(False)

        yield UserData.deleteAll(where=['uuid = ?', uuid])
        yield GranularData.deleteAll(where=['uuid = ?', uuid])
        yield StreamCache.deleteAll(where=['uuid = ?', uuid])
        defer.returnValue(True)
Esempio n. 2
0
    def update_stream_cache(self, data, authorization):
        records_to_process = {
            'add': [], # should contain dicts
            'remove': [], # should contain db records
            'update': [], # should contain dicts
        }

        def categorize(db_data):
            db_data = list(flatten(db_data))
            db_ids = [record.item_id for record in db_data]
            new_ids = [datum['item_id'] for datum in data]

            # find records to add
            for new_datum in data:
                if not new_datum['item_id'] in db_ids:
                    records_to_process['add'].append(new_datum)

            # find records to delete
            for db_datum in db_data:
                if not db_datum.item_id in new_ids:
                    records_to_process['remove'].append(db_datum)

            # find records to update
            for db_datum in db_data:
                for new_datum in data:
                    new_json = new_datum['data']
                    old_json = json.loads(db_datum.data)

                    if db_datum.item_id == new_datum['item_id'] and new_json != old_json:

                        change = {
                            'old': db_datum,
                            'new': new_datum
                        }
                        records_to_process['update'].append(change)
            return

        def add(_):
            return defer.gatherResults([
                StreamCache(
                    uuid=authorization.uuid,
                    item_id=datum['item_id'],
                    timestamp=datum['timestamp'],
                    data=json.dumps(datum['data']),
                ).save()
            for datum in records_to_process['add']
         ])


        def update_single(change):
            # change is a dictionary that contains two keys, `new` and `old`.
            # `new` contains the dictionary with new data from the 3rd party api that we need to update *to*
            # `old` containst the database record that needs to be updated with new data

            new_data = change['new']
            db_record = change['old']

            db_record.data = json.dumps(new_data['data'])
            return db_record.save()

        def update(_):
            return defer.gatherResults([
                update_single(change)
                    for change in records_to_process['update']])


        def remove(_):
            return defer.gatherResults([
                record.delete()
                    for record in records_to_process['remove']])

        uuid = authorization.uuid
        return StreamCache.find(
            where=['uuid=?', uuid]
        ).addCallback(categorize).addCallback(add).addCallback(remove).addCallback(update)