Пример #1
0
def handle_file_upload_data(data):
    try:
        db.add_item("files",
                    {"filename": data['name'], "campaign": data['campaign'], "classifications": [],
                     "description": "None", "owner": data['owner'],
                     "is_classified": 0, "labels": data['labels']})
    except KeyError as e:
        return {"errorMsg": "invalid json format"}, -1
    return {"success": 1}, 0
Пример #2
0
def new_campaign(data):
    key = {'name': data['campName']}
    obj = {'name': data['campName'], 'owner': data['owner']}
    response = db.get_item("campaigns", key)
    if ('Item' in response):
        str = 'campaign ' + data['campName'] + ' already exists'
        return {'errorMsg': str}, -1

    db.add_item("campaigns", obj)
    return {}, 0
Пример #3
0
def save_classifications(data):
    file = data['files']
    new_classifications = []
    for classification in file['classifications']:
        key = {"name": classification['oldName'], "campaign": file['campaign']}
        response = db.del_item("classifications", key)
        response = db.add_item(
            "classifications", {
                "name": classification['name'],
                "campaign": file['campaign'],
                "owner": file['owner'],
                "examples": classification['examples'],
                "is_sensitive": classification['is_sensitive']
            })
        del classification['oldName']
    key = {"filename": file['filename'], "campaign": file['campaign']}
    response = db.update_item("files", key, 'set #classifications = :values ',
                              {"#classifications": "classifications"},
                              {":values": file['classifications']})
    return data, 0
Пример #4
0
def test_add():
    testDB = TDB()
    test = testDB.Table("classifications")
    db.add_item("classifications", test_obj_1, testDB)
    db.add_item("classifications", test_obj_2, testDB)
    db.add_item("classifications", test_obj_3, testDB)

    response = test.response(test_obj_1_key)
    assert ('Item' in response)
    assert (response['Item'] == test_obj_1)

    response = test.response(test_obj_2_key)
    assert ('Item' in response)
    assert (response['Item'] == test_obj_2)

    response = test.response(test_obj_3_key)
    assert ('Item' in response)
    assert (response['Item'] == test_obj_3)
Пример #5
0
def save_data(data, db_manager=None):
    try:
        for item in data:
            names = []
            if 'description' not in item:
                item['description'] = "Placeholder Description"

            for classification in item['classifications']:
                if classification is None:
                    continue
                if 'is_sensitive' not in classification:
                    classification['is_sensitive'] = 0
                key = {
                    "name": classification['name'],
                    "campaign": item['campaign']
                }
                response = db.get_item("classifications", key, db_manager)
                if 'Item' not in response:
                    response = db.add_item(
                        "classifications", {
                            "name": classification['name'],
                            "campaign": item['campaign'],
                            "owner": item['owner'],
                            "examples": classification['examples'],
                            "is_sensitive": classification['is_sensitive']
                        }, db_manager)
                else:
                    response = db.update_item(
                        "classifications", key,
                        'set #classifications = list_append(if_not_exists(#classifications, :empty_list), :values)',
                        {'#classifications': 'examples'}, {
                            ':values': classification['examples'],
                            ':empty_list': []
                        }, db_manager)
                names.append({
                    "name": classification['name'],
                    "is_sensitive": classification["is_sensitive"]
                })

            response = db.update_item("files", {
                "filename": item['filename'],
                'campaign': item['campaign']
            }, 'set #description = :description',
                                      {'#description': 'description'},
                                      {':description': item['description']},
                                      db_manager)
            response = db.update_item(
                "files", {
                    "filename": item['filename'],
                    'campaign': item['campaign']
                },
                'set #classifications = list_append(if_not_exists(#classifications, :empty_list), :values)',
                {'#classifications': 'classifications'}, {
                    ':values': names,
                    ':empty_list': []
                }, db_manager)
            response = db.update_item("files", {
                "filename": item['filename'],
                'campaign': item['campaign']
            }, 'set #is_classified = :value',
                                      {'#is_classified': 'is_classified'},
                                      {':value': 1}, db_manager)

    except KeyError as e:
        return {"errorMsg": "invalid json object: " + e.__str__()}, -1

    return {}, 0