Exemple #1
0
def update_asset_values(asset_type, asset_filter, user_update_dict,
                        auth_level='user', multi=False):
    asset_type_info = get_asset_type(asset_type)
    response_dict = {'success': False,
                     'error': None,
                     'message': None,
                     'updated_keys': {},
                     'updated_asset_ids': []}
    # TEMP Auth check, this will Change
    if auth_level is 'admin':
        valid_keys = set(asset_type_info.get('managed_keys') +
                         asset_type_info.get('unmanaged_keys'))
        for k, v in user_update_dict.items():
            if k.split('.')[0] in valid_keys:
                response_dict['updated_keys'][k] = v
    else:
        valid_keys = set(asset_type_info.get('unmanaged_keys'))
        for k, v in user_update_dict.items():
            if k.split('.')[0] in valid_keys:
                response_dict['updated_keys'][k] = v
    payload = model_tools.item_stringify(response_dict['updated_keys'])

    # Get abell ids of matching assets
    assets_to_update = asset_find(
                        asset_type,
                        asset_filter,
                        specified_keys={'_id': 0, 'abell_id': 1})
    assets_to_update = assets_to_update.get('result')
    if not multi:
        if len(assets_to_update) is not 1:
            response_dict.update({'error': 400,
                                  'message': 'The filter did not return '
                                             'a single asset'})
            return response_dict

    db_response = ABELLDB.update_asset(asset_type, asset_filter, payload)
    if db_response.get('success'):
        for a in assets_to_update:
            asset_id = a.get('abell_id')
            response_dict['updated_asset_ids'].append(asset_id)
        response_dict.update({'success': True,
                              'message': db_response.get('result')})
    # TODO Probably convert to a bulk find and update call
    # TODO Log updated documents
    return response_dict
Exemple #2
0
 def stringified_attributes(self):
     return model_tools.item_stringify(self.fields)
Exemple #3
0
 def add_property_dict(self, property_dict):
     stringified_dict = model_tools.item_stringify(property_dict)
     for key, value in stringified_dict.iteritems():
         # self.add_property(key, value)
         setattr(self, key, value)
Exemple #4
0
 def __init__(self, asset_type, asset_info, abell_id, property_dict=None,
              user_level='user'):
     self.asset_type = str(asset_type)
     self.abell_id = str(abell_id)
     self.asset_info = model_tools.item_stringify(asset_info)
     self.fields = {}
Exemple #5
0
def update_asset_values(asset_type,
                        asset_filter,
                        user_update_dict,
                        auth_level='user',
                        multi=False,
                        upsert=False):
    response_dict = {
        'success': False,
        'error': None,
        'message': None,
        'updated_keys': {},
        'updated_asset_ids': []
    }
    ato = AT.get_asset_type(asset_type)
    if not ato:
        response_dict.update({
            'error': 404,
            'message': 'Type %s not found.' % asset_type
        })
        return response_dict

    # TEMP Auth check, this will Change)
    if auth_level == 'admin':
        valid_keys = ato.managed_keys.union(ato.unmanaged_keys)
        for k, v in user_update_dict.items():
            if k.split('.')[0] in valid_keys:
                response_dict['updated_keys'][k] = v
    else:
        valid_keys = ato.unmanaged_keys
        for k, v in user_update_dict.items():
            if k.split('.')[0] in valid_keys:
                response_dict['updated_keys'][k] = v
    payload = model_tools.item_stringify(response_dict['updated_keys'])

    # Get abell ids of matching assets
    assets_to_update = asset_find(asset_type,
                                  asset_filter,
                                  specified_keys={
                                      '_id': 0,
                                      'abell_id': 1
                                  })
    assets_to_update = assets_to_update.get('result')
    if not multi:
        # check for UPSERT if len is 0
        if upsert and len(assets_to_update) is 0:
            print('creating new asset')
            new_asset = AbellAsset(ato.asset_type,
                                   user_update_dict.get('abell_id'),
                                   user_update_dict, ato)
            r = new_asset.insert_asset()
            if r.get('success'):
                response_dict.update({
                    'success': True,
                    'new_assets': r.get('message')
                })
                return response_dict
            else:
                response_dict.update({
                    'error': 500,
                    'message': 'Error creating asset.'
                })
                return response_dict

        if len(assets_to_update) is not 1:
            response_dict.update({
                'error':
                400,
                'message':
                'The filter did not return '
                'a single asset'
            })
            return response_dict

    db_response = ABELLDB.update_asset(asset_type, asset_filter, payload)
    if db_response.get('success'):
        for a in assets_to_update:
            asset_id = a.get('abell_id')
            response_dict['updated_asset_ids'].append(asset_id)
        response_dict.update({
            'success': True,
            'message': db_response.get('result')
        })
    # TODO Probably convert to a bulk find and update call
    # TODO Log updated documents
    return response_dict
Exemple #6
0
 def update_keys(self, property_dict):
     stringified_dict = model_tools.item_stringify(property_dict)
     for key, value in stringified_dict.items():
         if key in self.fields:
             self.fields[key] = value