Example #1
0
def update_job(payload, namespace, uuid):
    body = loads(request.data)
    uuid = ObjectId(uuid)

    if body['action'] == 'delete':
        db = get_db(payload)
        result = db.namespaces \
            .find_one_and_update({ 'ident': namespace },
                                 {
                                     '$pull': { 'jobs': { '_id': uuid } }
                                 }, return_document = ReturnDocument.AFTER)

        result = db.namespaces.find_one({'ident': namespace})['jobs']
        return to_json(result)

    if body['action'] == 'update':
        new_job = body['job']

        db = get_db(payload)
        result = db.namespaces \
            .find_one_and_update({ 'ident': namespace, 'jobs._id': uuid },
                                 {
                                     '$set': { 'jobs.$': new_job }
                                 }, return_document = ReturnDocument.AFTER)

        result = db.namespaces.find_one({'ident': namespace})['jobs']
        return to_json(result)

    return json.dumps({'error': 'could not find matching action!'})
Example #2
0
def update_jobs(payload, namespace):
    body = loads(request.data)

    if body['action'] == 'update':
        new_jobs = body['jobs']

        db = get_db(payload)
        result = db.namespaces \
            .find_one_and_update({ 'ident': namespace },
                                 {
                                     '$set': { 'jobs': new_jobs }
                                 }, return_document = ReturnDocument.AFTER)

        result = db.namespaces.find_one({'ident': namespace})['jobs']

        return to_json(result)

    if body['action'] == 'add':
        new_job = body['job']
        new_job['_id'] = ObjectId()

        db = get_db(payload)
        result = db.namespaces \
            .find_one_and_update({ 'ident': namespace },
                                 {
                                     '$push': { 'jobs': new_job }
                                 }, return_document = ReturnDocument.AFTER)

        result = db.namespaces.find_one({'ident': namespace})['jobs']
        return to_json(result)

    return to_json({'error': 'could not find matching action!'})
Example #3
0
def update_namespaces(payload):
    username = payload['username']
    body = json.loads(request.data)
    namespace = body['namespace']

    if body['action'] == 'add':
        db = client.configuration
        result = db.namespaces.find_one({
            'ident': namespace,
            'username': username
        })

        if result:
            return to_json({'error': 'ident already used!'})

        db.namespaces.insert_one({
            'ident': namespace['ident'],
            'username': username,
            'name': namespace['name'],
            'description': namespace['description'],
            'jobs': []
        })

    # return the given namespace to approve
    return to_json(namespace)
Example #4
0
def update_namespace(payload, namespace):
    body = json.loads(request.data)

    if body['action'] == 'delete':
        db = get_db(payload)
        db.namespaces.delete_one({'ident': namespace})

        return to_json({'done': True})

    return to_json({'error': 'action not found'})
Example #5
0
def update_namespace(payload, namespace):
    username = payload['username']
    body = json.loads(request.data)

    if body['action'] == 'delete':
        db = client.configuration
        db.namespaces.delete_one({'ident': namespace, 'username': username})

        return to_json({'done': True})

    return to_json({'error': 'action not found'})
Example #6
0
def get_proxy_config(payload):
    result = client[payload['database']].proxy.find_one()
    if not result:
        return to_json({
            'host': '',
            'port': '',
            'username': '',
            'password': ''
        })

    return to_json(result)
Example #7
0
def update_settings(payload):
    username = payload['username']
    body = json.loads(request.data)
    setting = body['data']

    table = client.configuration.settings

    if body['action'] == 'add':
        result = table.find_one({
            'ident': setting['ident'],
            'username': username
        })

        if result:
            return to_json({'error': 'Ident already used!'})

        table.insert_one({
            'ident': setting['ident'],
            'username': username,
            'name': setting['name'],
            'description': setting['description'],
            'params': []
        })

    elif body['action'] == 'delete':
        result = table.delete_one({
            'ident': setting['ident'],
            'username': username
        })
        return to_json({'done': True})

    elif body['action'] == 'update':
        # encode instagram credentials
        for param in setting['params']:
            if param['name'] != 'password' and param['name'] != 'username':
                continue

            param['value'] = coding_wrapper(param['value'], encode)

        result = table.find_one_and_update(
            {
                'ident': setting['ident'],
                'username': username
            }, {'$set': {
                'params': setting['params']
            }})
        return to_json({'done': True})

    # return the given setting to approve
    return to_json(setting)
Example #8
0
def get_jobs(payload, namespace):
    username = payload['username']
    db = client.configuration

    result = db.namespaces.find_one({'ident': namespace, 'username': username})

    return to_json(result['jobs'])
Example #9
0
def set_proxy_config(payload):
    body = json.loads(request.data)

    table = client[payload['database']].proxy
    table.delete_one({})
    table.insert_one(body)

    return to_json({ 'done': True })
Example #10
0
def set_credentials(payload):
    body = json.loads(request.data)

    table = client[payload['database']].account
    table.delete_one({})
    table.insert_one(body)

    return to_json({'done': True})
Example #11
0
def get_namespaces(payload):
    db = get_db(payload)
    namespaces = db.namespaces.find()
    namespaces = list(namespaces)

    for namespace in namespaces:
        del namespace['jobs']

    return to_json(namespaces)
Example #12
0
def update_jobs(payload, namespace):
    username = payload['username']
    body = loads(request.data)

    if body['action'] == 'update':
        new_jobs = body['jobs']

        db = client.configuration
        result = db.namespaces \
            .find_one_and_update({ 'ident': namespace, 'username': username },
                                 {
                                     '$set': { 'jobs': new_jobs }
                                 }, return_document = ReturnDocument.AFTER)

        # TODO change this
        # refetch all jobs to the gui can update everything
        result = db.namespaces.find_one({
            'ident': namespace,
            'username': username
        })['jobs']

        return to_json(result)

    if body['action'] == 'add':
        new_job = body['job']
        new_job['_id'] = ObjectId()

        db = client.configuration
        result = db.namespaces \
            .find_one_and_update({ 'ident': namespace, 'username': username },
                                 {
                                     '$push': { 'jobs': new_job }
                                 }, return_document = ReturnDocument.AFTER)

        # TODO change this
        # refetch all jobs to the gui can update everything
        result = db.namespaces.find_one({
            'ident': namespace,
            'username': username
        })['jobs']

        return to_json(result)

    return to_json({'error': 'could not find matching action!'})
Example #13
0
def update_job(payload, namespace, uuid):
    username = payload['username']
    body = loads(request.data)
    uuid = ObjectId(uuid)

    if body['action'] == 'delete':
        db = client.configuration
        result = db.namespaces \
            .find_one_and_update({ 'ident': namespace, 'username': username },
                                 {
                                     '$pull': { 'jobs': { '_id': uuid } }
                                 }, return_document = ReturnDocument.AFTER)

        # TODO change this
        # refetch all jobs to the gui can update everything
        result = db.namespaces.find_one({
            'ident': namespace,
            'username': username
        })['jobs']

        return to_json(result)

    if body['action'] == 'update':
        new_job = body['job']

        db = client.configuration
        result = db.namespaces \
            .find_one_and_update({ 'ident': namespace, 'username': username, 'jobs._id': uuid },
                                 {
                                     '$set': { 'jobs.$': new_job }
                                 }, return_document = ReturnDocument.AFTER)

        # TODO change this
        # refetch all jobs so the gui can update everything
        result = db.namespaces.find_one({
            'ident': namespace,
            'username': username
        })['jobs']

        return to_json(result)

    return json.dumps({'error': 'could not find matching action!'})
Example #14
0
def get_namespaces(payload):
    db = client.configuration

    # TODO do aggregation with project without 'jobs'
    namespaces = db.namespaces.find({'username': payload['username']})
    namespaces = list(namespaces)

    for namespace in namespaces:
        del namespace['jobs']

    return to_json(namespaces)
Example #15
0
def get_settings(payload):
    result = client.configuration.settings.find(
        {'username': payload['username']})
    result = json.loads(dumps(result))

    # decode instagram credentials
    for res in result:
        for param in res['params']:
            if param['name'] != 'username' and param['name'] != 'password':
                continue

            param['value'] = coding_wrapper(param['value'], decode)

    return to_json(result)
Example #16
0
def get_jobs(payload, namespace):
    db = get_db(payload)
    result = db.namespaces.find_one({'ident': namespace})

    return to_json(result['jobs'])
Example #17
0
def get_credentials(payload):
    result = client[payload['database']].account.find_one()

    return to_json({'username': result['username'] if result else None})
Example #18
0
def get_actions():
    actions = client.configuration.actions.find()
    return to_json(actions)
Example #19
0
def update_namespaces(payload):
    username = payload['username']
    body = json.loads(request.data)
    namespace = body['namespace']
    action = body['action']
    db = client.configuration

    if action == 'add':
        result = db.namespaces.find_one({'ident': namespace, 'username': username})

        if result:
            return to_json({'error': 'ident already used!'})

        db.namespaces.insert_one(
            {
                'ident': namespace['ident'],
                'username': username,
                'name': namespace['name'],
                'description': namespace['description'],
                'jobs': [],
            }
        )

    elif action == 'edit':
        db.namespaces.find_one_and_update(
            {'ident': namespace['oldIdent'], 'username': username},
            {
                '$set': {
                    'ident': namespace['ident'],
                    'name': namespace['name'],
                    'description': namespace['description'],
                }
            },
        )

    elif action == 'copy':
        result = db.namespaces.find_one({'ident': namespace, 'username': username})
        if not result:
            return to_json({'error': 'ident not found!'})

        old_namespace = json.loads(dumps(result))

        new_ident = old_namespace['ident']
        new_name = old_namespace['name']

        # generate new namespace name
        counter = 0
        max_counter = 20
        while counter < max_counter:
            counter += 1
            new_ident = new_ident + ' copy'
            new_name = new_name + ' copy'
            result = db.namespaces.find_one({'ident': new_ident, 'username': username})
            if not result:
                break

        if counter == max_counter:
            return to_json({'error': 'Too many copies!'})

        # adjust old jobs to new namespace
        for job in old_namespace['jobs']:
            job['_id'] = ObjectId()
            job['namespace'] = new_ident

        new_namespace = {
            'ident': new_ident,
            'username': username,
            'name': new_name,
            'description': old_namespace['description'],
            'jobs': old_namespace['jobs'],
        }

        db.namespaces.insert_one(new_namespace)
        return to_json(new_namespace)

    # return the given namespace to approve
    return to_json(namespace)
Example #20
0
def get_actions():
    actions = client.general.actions.find()
    return to_json(actions)