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!'})
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!'})
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)
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'})
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'})
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)
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)
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'])
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 })
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})
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)
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!'})
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!'})
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)
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)
def get_jobs(payload, namespace): db = get_db(payload) result = db.namespaces.find_one({'ident': namespace}) return to_json(result['jobs'])
def get_credentials(payload): result = client[payload['database']].account.find_one() return to_json({'username': result['username'] if result else None})
def get_actions(): actions = client.configuration.actions.find() return to_json(actions)
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)
def get_actions(): actions = client.general.actions.find() return to_json(actions)