Exemple #1
0
    def on_failed(self, host, results, ignore_errors=False):

        results2 = results.copy()
        results2.pop('invocation', None)

        item = results2.get('item', None)
        parsed = results2.get('parsed', True)
        module_msg = ''
        if not parsed:
            module_msg  = results2.pop('msg', None)
        stderr = results2.pop('stderr', None)
        stdout = results2.pop('stdout', None)
        returned_msg = results2.pop('msg', None)

        if item:
            msg = "failed: [%s] => (item=%s) => %s" % (host, item, utils.jsonify(results2))
        else:
            msg = "failed: [%s] => %s" % (host, utils.jsonify(results2))
        print stringc(msg, 'red')

        if stderr:
            print stringc("stderr: %s" % stderr, 'red')
        if stdout:
            print stringc("stdout: %s" % stdout, 'red')
        if returned_msg:
            print stringc("msg: %s" % returned_msg, 'red')
        if not parsed and module_msg:
            print stringc("invalid output was: %s" % module_msg, 'red')
        if ignore_errors:
            print stringc("...ignoring", 'cyan')
        super(PlaybookRunnerCallbacks, self).on_failed(host, results, ignore_errors=ignore_errors)
Exemple #2
0
    def on_ok(self, host, host_result):
        
        item = host_result.get('item', None)

        host_result2 = host_result.copy()
        host_result2.pop('invocation', None)
        verbose_always = host_result2.pop('verbose_always', None)
        changed = host_result.get('changed', False)
        ok_or_changed = 'ok'
        if changed:
            ok_or_changed = 'changed'

        # show verbose output for non-setup module results if --verbose is used
        msg = ''
        if (not self.verbose or host_result2.get("verbose_override",None) is not
                None) and verbose_always is None:
            if item:
                msg = "%s: [%s] => (item=%s)" % (ok_or_changed, host, item)
            else:
                if 'ansible_job_id' not in host_result or 'finished' in host_result:
                    msg = "%s: [%s]" % (ok_or_changed, host)
        else:
            # verbose ...
            if item:
                msg = "%s: [%s] => (item=%s) => %s" % (ok_or_changed, host, item, utils.jsonify(host_result2))
            else:
                if 'ansible_job_id' not in host_result or 'finished' in host_result2:
                    msg = "%s: [%s] => %s" % (ok_or_changed, host, utils.jsonify(host_result2))

        if msg != '':
            if not changed:
                display(msg, color='green', runner=self.runner)
            else:
                display(msg, color='yellow', runner=self.runner)
        super(PlaybookRunnerCallbacks, self).on_ok(host, host_result)
Exemple #3
0
    def get(self, action=None):
        args = self.parser.parse_args()
        path = args.get('path')

        if action is not None:
            if action == 'getDefaultPath':
                return jsonify({
                    'defaultPath': os.path.abspath(os.path.join(os.path.curdir, 'spiders'))
                })

            elif action == 'get_file':
                file_data = get_file_content(path)
                file_data['status'] = 'ok'
                return jsonify(file_data)

            else:
                return {}

        folders = []
        files = []
        for _path in os.listdir(path):
            if os.path.isfile(os.path.join(path, _path)):
                files.append(_path)
            elif os.path.isdir(os.path.join(path, _path)):
                folders.append(_path)
        return jsonify({
            'status': 'ok',
            'files': sorted(files),
            'folders': sorted(folders),
        })
    def on_ok(self, host, host_result):

        item = host_result.get("item", None)

        host_result2 = host_result.copy()
        host_result2.pop("invocation", None)
        verbose_always = host_result2.pop("verbose_always", None)
        changed = host_result.get("changed", False)
        ok_or_changed = "ok"
        if changed:
            ok_or_changed = "changed"

        # show verbose output for non-setup module results if --verbose is used
        msg = ""
        if (not self.verbose or host_result2.get("verbose_override", None) is not None) and verbose_always is None:
            if item:
                msg = "%s: [%s] => (item=%s)" % (ok_or_changed, host, item)
            else:
                if "ansible_job_id" not in host_result or "finished" in host_result:
                    msg = "%s: [%s]" % (ok_or_changed, host)
        else:
            # verbose ...
            if item:
                msg = "%s: [%s] => (item=%s) => %s" % (ok_or_changed, host, item, utils.jsonify(host_result2))
            else:
                if "ansible_job_id" not in host_result or "finished" in host_result2:
                    msg = "%s: [%s] => %s" % (ok_or_changed, host, utils.jsonify(host_result2))

        if msg != "":
            if not changed:
                display(msg, color="green", runner=self.runner)
            else:
                display(msg, color="yellow", runner=self.runner)
        super(PlaybookRunnerCallbacks, self).on_ok(host, host_result)
Exemple #5
0
    def get(self, id: str = None, action: str = None) -> (dict, tuple):
        """
        GET method of DeployAPI.
        :param id: deploy_id
        :param action: action
        """
        # action by id
        if action is not None:
            if not hasattr(self, action):
                return {
                           'status': 'ok',
                           'code': 400,
                           'error': 'action "%s" invalid' % action
                       }, 400
            return getattr(self, action)(id)

        # get one node
        elif id is not None:
            return jsonify(db_manager.get('deploys', id=id))

        # get a list of items
        else:
            items = db_manager.list('deploys', {})
            deploys = []
            for item in items:
                spider_id = item['spider_id']
                spider = db_manager.get('spiders', id=str(spider_id))
                if spider is None:
                    db_manager.remove('deploys', {'spider_id':spider_id})
                item['spider_name'] = spider['name']
                deploys.append(item)
            return {
                'status': 'ok',
                'items': jsonify(deploys)
            }
Exemple #6
0
    def get(self, id=None, action=None):
        # action by id
        if action is not None:
            if not hasattr(self, action):
                return {
                    'status': 'ok',
                    'code': 400,
                    'error': 'action "%s" invalid' % action
                }, 400
            return getattr(self, action)(id)

        # get one node
        elif id is not None:
            return jsonify(db_manager.get('deploys', id=id))

        # get a list of items
        else:
            items = db_manager.list('deploys', {})
            deploys = []
            for item in items:
                spider_id = item['spider_id']
                spider = db_manager.get('spiders', id=str(spider_id))
                item['spider_name'] = spider['name']
                deploys.append(item)
            return {'status': 'ok', 'items': jsonify(deploys)}
Exemple #7
0
    def on_failed(self, host, results, ignore_errors=False):

        results2 = results.copy()
        results2.pop("invocation", None)

        item = results2.get("item", None)
        parsed = results2.get("parsed", True)
        module_msg = ""
        if not parsed:
            module_msg = results2.pop("msg", None)
        stderr = results2.pop("stderr", None)
        stdout = results2.pop("stdout", None)
        returned_msg = results2.pop("msg", None)

        if item:
            msg = "failed: [%s] => (item=%s) => %s" % (host, item, utils.jsonify(results2))
        else:
            msg = "failed: [%s] => %s" % (host, utils.jsonify(results2))
        display(msg, color="red", runner=self.runner)

        if stderr:
            display("stderr: %s" % stderr, color="red", runner=self.runner)
        if stdout:
            display("stdout: %s" % stdout, color="red", runner=self.runner)
        if returned_msg:
            display("msg: %s" % returned_msg, color="red", runner=self.runner)
        if not parsed and module_msg:
            display("invalid output was: %s" % module_msg, color="red", runner=self.runner)
        if ignore_errors:
            display("...ignoring", color="cyan", runner=self.runner)
        super(PlaybookRunnerCallbacks, self).on_failed(host, results, ignore_errors=ignore_errors)
Exemple #8
0
    def get_results(self, id: str) -> (dict, tuple):
        """
        Get a list of results crawled in a given task.
        :param id: task_id
        """
        args = self.parser.parse_args()
        page_size = args.get('page_size') or 10
        page_num = args.get('page_num') or 1

        task = db_manager.get('tasks', id=id)
        spider = db_manager.get('spiders', id=task['spider_id'])
        col_name = spider.get('col')
        if not col_name:
            return []
        fields = get_spider_col_fields(col_name)
        items = db_manager.list(col_name, {'task_id': id},
                                skip=page_size * (page_num - 1),
                                limit=page_size)
        return {
            'status': 'ok',
            'fields': jsonify(fields),
            'total_count': db_manager.count(col_name, {'task_id': id}),
            'page_num': page_num,
            'page_size': page_size,
            'items': jsonify(items)
        }
Exemple #9
0
    def on_failed(self, host, results, ignore_errors=False):
        if self.runner.delegate_to:
            host = '%s -> %s' % (host, self.runner.delegate_to)

        results2 = results.copy()
        results2.pop('invocation', None)

        item = results2.get('item', None)
        parsed = results2.get('parsed', True)
        module_msg = ''
        if not parsed:
            module_msg  = results2.pop('msg', None)
        stderr = results2.pop('stderr', None)
        stdout = results2.pop('stdout', None)
        returned_msg = results2.pop('msg', None)

        if item:
            msg = "failed: [%s] => (item=%s) => %s" % (host, item, utils.jsonify(results2))
        else:
            msg = "failed: [%s] => %s" % (host, utils.jsonify(results2))
        # Display Cyan error message instead of Red when ignoring errors
        color = 'cyan' if ignore_errors else 'red'
        display(msg, color=color, runner=self.runner)

        if stderr:
            display("stderr: %s" % stderr, color='red', runner=self.runner)
        if stdout:
            display("stdout: %s" % stdout, color='red', runner=self.runner)
        if returned_msg:
            display("msg: %s" % returned_msg, color='red', runner=self.runner)
        if not parsed and module_msg:
            display(module_msg, color='red', runner=self.runner)
        if ignore_errors:
            display("...ignoring", color='cyan', runner=self.runner)
        super(PlaybookRunnerCallbacks, self).on_failed(host, results, ignore_errors=ignore_errors)
Exemple #10
0
def regular_generic_msg(hostname, result, oneline, caption):
    ''' output on the result of a module run that is not command '''

    if not oneline:
        return "%s | %s >> %s\n" % (hostname, caption, utils.jsonify(result,format=True))
    else:
        return "%s | %s >> %s\n" % (hostname, caption, utils.jsonify(result))
Exemple #11
0
def get_receipts():
    receipts = mongo.db.receipts
    if request.method == 'POST':
        '''Create new receipt'''
        receipt = request.json
        receipt['user'] = request.authorization['username']
        # TODO Validate input, add date if missing etc
        db_operation = receipts.insert(receipt)
        ac_add_new_words(receipt)
        return jsonify({'savedReceipt': receipt, 'dbOperation': db_operation})
# 201 Created
# 403 Forbidden

    if request.method == 'GET':
        ''' List all receipts for request user.
        Pagination is set by limit and offset variables.
        '''
        limit = request.args.get('limit') or 1000
        offset = request.args.get('offset') or 0

        receipts = mongo.db.receipts
        user_receipts_cursor = receipts.find(
            {"user": request.authorization['username']})
        user_receipts = []
        for receipt in user_receipts_cursor:
            user_receipts.append(receipt)
        # Sort by date
        user_receipts.sort(key=lambda x: x['date'])

        return jsonify({'receipts':
                        user_receipts[offset:offset+limit],
                        'pagination': {
                            'total': len(user_receipts),
                            'from': offset,
                            'to': offset + limit}})
Exemple #12
0
def login():
    if request.method == "GET":
        user_id = current_user.get_id()
        if user_id:
            return redirect(conf.URL_PREFIX)
        else:
            return render_template('haoAdmin/auth/login.html')
    else:
        login_name = request.form.get("login_name")
        login_pwd = request.form.get("login_pwd", "")
        remember_me = request.form.get("rememberMe", None)
        remember_me = True if remember_me == "true" else False

        try:
            user = HUser.get(HUser.login_name == login_name,
                             HUser.login_pwd == utils.get_md5_s(login_pwd))
            if user:
                # 获取相关机构
                org = org_dal.get_org_by_user_id(user.pid)
                if org:
                    session["user_org"] = org
                # 获取相关角色
                role = role_dal.get_role_by_user_id(user.pid)
                if role:
                    session["user_role"] = role

                user.login_pwd = None
                login_user(user, remember_me)

                return jsonify(code=0)
        except DoesNotExist as e:
            log.error(e.message, exc_info=True)
            return jsonify(code=-1, msg="用户名或密码错误", ensure_ascii=False)
Exemple #13
0
    def on_failed(self, host, results, ignore_errors=False):


        results2 = results.copy()
        results2.pop('invocation', None)

        item = results2.get('item', None)
        parsed = results2.get('parsed', True)
        module_msg = ''
        if not parsed:
            module_msg  = results2.pop('msg', None)
        stderr = results2.pop('stderr', None)
        stdout = results2.pop('stdout', None)
        returned_msg = results2.pop('msg', None)

        if item:
            msg = "failed: [%s] => (item=%s) => %s" % (host, item, utils.jsonify(results2))
        else:
            msg = "failed: [%s] => %s" % (host, utils.jsonify(results2))
        display(msg, color='red', runner=self.runner)

        if stderr:
            display("stderr: %s" % stderr, color='red', runner=self.runner)
        if stdout:
            display("stdout: %s" % stdout, color='red', runner=self.runner)
        if returned_msg:
            display("msg: %s" % returned_msg, color='red', runner=self.runner)
        if not parsed and module_msg:
            display("invalid output was: %s" % module_msg, color='red', runner=self.runner)
        if ignore_errors:
            display("...ignoring", color='cyan', runner=self.runner)
        super(PlaybookRunnerCallbacks, self).on_failed(host, results, ignore_errors=ignore_errors)
Exemple #14
0
def receipt(id):
    receipts = mongo.db.receipts
    if request.method == 'GET':
        '''Get receipt specified by ID'''  # TODO
        return {'id': id, 'data': 'GET mocked'}

    if request.method == 'PUT':
        '''Update receipt data'''
        receipt = request.get_json()
        receipt['_id'] = ObjectId(receipt['_id'])
        query = receipts.save(receipt)
        # TODO A save receipt product names to autocomplete database
        ac_add_new_words(receipt)

        # add if not exists

        return jsonify(receipt)

    if request.method == 'DELETE':
        '''Delete receipt'''
        # TODO: other users receipts can now be removed by ID
        try:
            query = receipts.remove(ObjectId(id))
            if query[u'n'] is 0:
                abort(404, "Problem with Mongo remove function")
        except:
            abort(404)
        return jsonify({"query": str(query)}), 200
Exemple #15
0
    def on_failed(self, host, results, ignore_errors=False):
        if self.runner.delegate_to:
            host = '%s -> %s' % (host, self.runner.delegate_to)

        results2 = results.copy()
        results2.pop('invocation', None)

        item = results2.get('item', None)
        parsed = results2.get('parsed', True)
        module_msg = ''
        if not parsed:
            module_msg  = results2.pop('msg', None)
        stderr = results2.pop('stderr', None)
        stdout = results2.pop('stdout', None)
        returned_msg = results2.pop('msg', None)

        if item:
            msg = "failed: [%s] => (item=%s) => %s" % (host, item, utils.jsonify(results2))
        else:
            msg = "failed: [%s] => %s" % (host, utils.jsonify(results2))
        display(msg, color='red', runner=self.runner)

        if stderr:
            display("stderr: %s" % stderr, color='red', runner=self.runner)
        if stdout:
            display("stdout: %s" % stdout, color='red', runner=self.runner)
        if returned_msg:
            display("msg: %s" % returned_msg, color='red', runner=self.runner)
        if not parsed and module_msg:
            display(module_msg, color='red', runner=self.runner)
        if ignore_errors:
            display("...ignoring", color='cyan', runner=self.runner)
        super(PlaybookRunnerCallbacks, self).on_failed(host, results, ignore_errors=ignore_errors)
Exemple #16
0
def regular_generic_msg(hostname, result, oneline, caption):
    ''' output on the result of a module run that is not command '''

    if not oneline:
        return "%s | %s >> %s\n" % (hostname, caption, utils.jsonify(result,format=True))
    else:
        return "%s | %s >> %s\n" % (hostname, caption, utils.jsonify(result))
Exemple #17
0
    def on_failed(self, host, results, ignore_errors=False):

        results2 = results.copy()
        results2.pop('invocation', None)

        item = results2.get('item', None)
        parsed = results2.get('parsed', True)
        module_msg = ''
        if not parsed:
            module_msg = results2.pop('msg', None)
        stderr = results2.pop('stderr', None)
        stdout = results2.pop('stdout', None)

        if item:
            msg = "failed: [%s] => (item=%s) => %s" % (host, item,
                                                       utils.jsonify(results2))
        else:
            msg = "failed: [%s] => %s" % (host, utils.jsonify(results2))
        print stringc(msg, 'red')

        if stderr:
            print stringc("stderr: %s" % stderr, 'red')
        if stdout:
            print stringc("stdout: %s" % stdout, 'red')
        if not parsed and module_msg:
            print stringc("invalid output was: %s" % module_msg, 'red')
        if ignore_errors:
            print stringc("...ignoring", 'yellow')
        super(PlaybookRunnerCallbacks,
              self).on_failed(host, results, ignore_errors=ignore_errors)
Exemple #18
0
    def on_ok(self, host, host_result):
        item = host_result.get('item', None)

        host_result2 = host_result.copy()
        host_result2.pop('invocation', None)

        # show verbose output for non-setup module results if --verbose is used
        msg = ''
        if not self.verbose or host_result2.get("verbose_override",None) is not None:
            if item:
                msg = "ok: [%s] => (item=%s)" % (host,item)
            else:
                if 'ansible_job_id' not in host_result or 'finished' in host_result:
                    msg = "ok: [%s]" % (host)
        else:
            # verbose ...
            if item:
                msg = "ok: [%s] => (item=%s) => %s" % (host, item, utils.jsonify(host_result2))
            else:
                if 'ansible_job_id' not in host_result or 'finished' in host_result2:
                    msg = "ok: [%s] => %s" % (host, utils.jsonify(host_result2))

        if msg != '':
            if not 'changed' in host_result2 or not host_result['changed']:
                print stringc(msg, 'green')
            else:
                print stringc(msg, 'yellow')
        super(PlaybookRunnerCallbacks, self).on_ok(host, host_result)
Exemple #19
0
    def on_ok(self, host, host_result):
        
        item = host_result.get('item', None)

        host_result2 = host_result.copy()
        host_result2.pop('invocation', None)
        verbose_always = host_result2.pop('verbose_always', None)
        changed = host_result.get('changed', False)
        ok_or_changed = 'ok'
        if changed:
            ok_or_changed = 'changed'

        # show verbose output for non-setup module results if --verbose is used
        msg = ''
        if (not self.verbose or host_result2.get("verbose_override",None) is not
                None) and verbose_always is None:
            if item:
                msg = "%s: [%s] => (item=%s)" % (ok_or_changed, host, item)
            else:
                if 'ansible_job_id' not in host_result or 'finished' in host_result:
                    msg = "%s: [%s]" % (ok_or_changed, host)
        else:
            # verbose ...
            if item:
                msg = "%s: [%s] => (item=%s) => %s" % (ok_or_changed, host, item, utils.jsonify(host_result2))
            else:
                if 'ansible_job_id' not in host_result or 'finished' in host_result2:
                    msg = "%s: [%s] => %s" % (ok_or_changed, host, utils.jsonify(host_result2))

        if msg != '':
            if not changed:
                display(msg, color='green', runner=self.runner)
            else:
                display(msg, color='yellow', runner=self.runner)
        super(PlaybookRunnerCallbacks, self).on_ok(host, host_result)
Exemple #20
0
 def delete(self, lab_id=None):
     try:
         db.delete_admin_lab(self.application.db, lab_id)
         utils.jsonify(self, True)
     except Exception as e:
         LOGGER.error(e)
         self.set_status(400)
         utils.jsonify(self, {'code': 'delete-failed', 'err': ''})
Exemple #21
0
 def delete(self, lab_id=None):
     try:
         db.delete_admin_lab(self.application.db, lab_id)
         utils.jsonify(self, True)
     except Exception as e:
         LOGGER.error(e)
         self.set_status(400)
         utils.jsonify(self, {'code': 'delete-failed', 'err': ''})
Exemple #22
0
 def post(self, lab_id=None):
     postdata = json.loads(self.request.body)
     lab, err = db.save_admin_lab(self.application.db, postdata)
     if lab:
         utils.jsonify(self, lab)
     else:
         self.set_status(400)
         utils.jsonify(self, {'code': 'save-failed', 'err': err})
Exemple #23
0
 def post(self, lab_id=None):
     postdata = json.loads(self.request.body)
     lab, err = db.save_admin_lab(self.application.db, postdata)
     if lab:
         utils.jsonify(self, lab)
     else:
         self.set_status(400)
         utils.jsonify(self, {'code': 'save-failed', 'err': err})
Exemple #24
0
 def post(self):
     logindata = json.loads(self.request.body)
     user, err = db.login_user(self.application.db, logindata)
     if user:
         self.set_secure_cookie('username', user['username'])
         utils.jsonify(self, user)
     else:
         self.set_status(401)
         utils.jsonify(self, {'code': 'login-failed', 'err': err})
Exemple #25
0
    def get(self, id=None, action=None):
        args = self.parser.parse_args()

        # action by id
        if action is not None:
            if not hasattr(self, action):
                return {
                    'status': 'ok',
                    'code': 400,
                    'error': 'action "%s" invalid' % action
                }, 400
            return getattr(self, action)(id)

        # list items
        elif id is None:
            # filter
            cond = {}
            if args.get('filter') is not None:
                cond = args.filter
                # cond = json.loads(args.filter)

            # page number
            page = 1
            if args.get('page') is not None:
                page = args.page
                # page = int(args.page)

            # page size
            page_size = 10
            if args.get('page_size') is not None:
                page_size = args.page_size
                # page = int(args.page_size)

            # TODO: sort functionality

            # total count
            total_count = db_manager.count(col_name=self.col_name, cond=cond)

            # items
            items = db_manager.list(col_name=self.col_name,
                                    cond=cond,
                                    skip=(page - 1) * page_size,
                                    limit=page_size)

            # TODO: getting status for node

            return jsonify({
                'status': 'ok',
                'total_count': total_count,
                'page': page,
                'page_size': page_size,
                'items': items
            })

        # get item by id
        else:
            return jsonify(db_manager.get(col_name=self.col_name, id=id))
Exemple #26
0
 def post(self):
     logindata = json.loads(self.request.body)
     user, err = db.login_user(self.application.db, logindata)
     if user:
         self.set_secure_cookie('username', user['username'])
         utils.jsonify(self, user)
     else:
         self.set_status(401)
         utils.jsonify(self, {'code': 'login-failed', 'err': err})
Exemple #27
0
 def post(self):
     user = self.get_current_user()
     try:
         archive_path = db.save_uploaded_archive(self.request, user)
         job_id = db.submit_job(self.application, archive_path, user)
         #self.db.jobs.insert({})
         utils.jsonify(self, True)
     except Exception as e:
         LOGGER.error(e)
         utils.jsonify(self, {'code': 'upload failed'})
Exemple #28
0
 def post(self):
     user = self.get_current_user()
     try:
         archive_path = db.save_uploaded_archive(self.request, user)
         job_id = db.submit_job(self.application, archive_path, user)
         #self.db.jobs.insert({})
         utils.jsonify(self, True)
     except Exception as e:
         LOGGER.error(e)
         utils.jsonify(self, {'code': 'upload failed'})
Exemple #29
0
    def get(self, id=None, action=None):
        # action by id
        if action is not None:
            if not hasattr(self, action):
                return {
                           'status': 'ok',
                           'code': 400,
                           'error': 'action "%s" invalid' % action
                       }, 400
            return getattr(self, action)(id)

        # get one node
        elif id is not None:
            return jsonify(db_manager.get('spiders', id=id))

        # get a list of items
        else:
            items = []
            dirs = os.listdir(PROJECT_SOURCE_FILE_FOLDER)
            for _dir in dirs:
                if _dir in IGNORE_DIRS:
                    continue

                dir_path = os.path.join(PROJECT_SOURCE_FILE_FOLDER, _dir)
                dir_name = _dir
                spider = db_manager.get_one_by_key('spiders', key='src', value=dir_path)

                # new spider
                if spider is None:
                    stats = get_file_suffix_stats(dir_path)
                    lang = get_lang_by_stats(stats)
                    db_manager.save('spiders', {
                        'name': dir_name,
                        'src': dir_path,
                        'lang': lang,
                        'suffix_stats': stats,
                    })

                # existing spider
                else:
                    stats = get_file_suffix_stats(dir_path)
                    lang = get_lang_by_stats(stats)
                    db_manager.update_one('spiders', id=str(spider['_id']), values={
                        'lang': lang,
                        'suffix_stats': stats,
                    })

                # append spider
                items.append(spider)

            return jsonify({
                'status': 'ok',
                'items': items
            })
Exemple #30
0
 def put(self):
     print 'Downloading File FooFile.txt [%d%%]\r'
     putdata = json.loads(self.request.body)
     print putdata
     try:
         db.edit_admin_lab(self.application.db, putdata)
         utils.jsonify(self, True)
     except Exception:
         LOGGER.error('error', exc_info=True)
         self.set_status(400)
         utils.jsonify(self, {'code': 'delete-failed', 'err': ''})
Exemple #31
0
def main_api():
    headers: dict = request.headers
    data: Optional[dict]
    content_type: str = headers.get("content-type", "").lower()
    if content_type not in constants.SUPPORTED_REQUEST_CONTENT_TYPES:
        return jsonify({"error": "Invalid request content type"}, 400)
    if content_type == constants.JSON_CONTENT_TYPE:
        data = request.get_json()
    else:
        data = request.form
    ret = request_parser.parse_request(data, headers)
    return jsonify(ret)
Exemple #32
0
    def get(self, id=None, action=None):
        # action by id
        if action is not None:
            if not hasattr(self, action):
                return {
                    'status': 'ok',
                    'code': 400,
                    'error': 'action "%s" invalid' % action
                }, 400
            return getattr(self, action)(id)

        elif id is not None:
            task = db_manager.get('tasks', id=id)
            _task = db_manager.get('tasks_celery', id=task['_id'])
            _spider = db_manager.get('spiders', id=str(task['spider_id']))
            if _task:
                if not task.get('status'):
                    task['status'] = _task['status']
            task['result'] = _task['result']
            task['spider_name'] = _spider['name']
            try:
                with open(task['log_file_path']) as f:
                    task['log'] = f.read()
            except Exception as err:
                task['log'] = ''
            return jsonify(task)

        # list tasks
        args = self.parser.parse_args()
        page_size = args.get('page_size') or 10
        page_num = args.get('page_num') or 1
        tasks = db_manager.list('tasks', {},
                                limit=page_size,
                                skip=page_size * (page_num - 1),
                                sort_key='finish_ts')
        items = []
        for task in tasks:
            _task = db_manager.get('tasks_celery', id=task['_id'])
            _spider = db_manager.get('spiders', id=str(task['spider_id']))
            if _task:
                task['status'] = _task['status']
            else:
                task['status'] = TaskStatus.UNAVAILABLE
            task['spider_name'] = _spider['name']
            items.append(task)
        return {
            'status': 'ok',
            'total_count': db_manager.count('tasks', {}),
            'page_num': page_num,
            'page_size': page_size,
            'items': jsonify(items)
        }
Exemple #33
0
    def on_failed(self, host, results, ignore_errors=False):

        results2 = results.copy()
        results2.pop('invocation', None)

        item = results2.get('item', None)
        if item:
            msg = "failed: [%s] => (item=%s) => %s" % (host, item, utils.jsonify(results2))
        else:
            msg = "failed: [%s] => %s" % (host, utils.jsonify(results2))
        print stringc(msg, 'red')
        if ignore_errors:
            print stringc("...ignoring", 'yellow')
        super(PlaybookRunnerCallbacks, self).on_failed(host, results, ignore_errors=ignore_errors)
Exemple #34
0
 async def _handle_event(self):
     while True:
         event, kwargs = await self.events.get()
         print(f'Event is: {event}')
         print(f'Kwargs are: {kwargs}')
         data = {'op': 0, 't': event.value}
         if event in [EventType.BOARD_CREATE, EventType.BOARD_UPDATE]:
             data['d'] = jsonify(kwargs.get('board'), requester=self.user)
         elif event is EventType.BOARD_DELETE:
             data['d'] = {'uid': kwargs.get('board').uid, 'unavailable': False}
         elif event in [EventType.CHANNEL_CREATE, EventType.CHANNEL_UPDATE, EventType.CHANNEL_DELETE]:
             data['d'] = jsonify(kwargs.get('channel'))
         elif event in [EventType.MESSAGE_CREATE]:
             data['d'] = jsonify(kwargs.get('message'))
         await self.websocket.send(json.dumps(data))
Exemple #35
0
def create_new_dish():
    """Creates new dish in db.
    
    :returns: True on success, False and code 403 on failure 
    """
    userid = get_jwt_identity()  # decoded
    user = login_model.get_user(userid)  # object user
    if user.user_type == 'admin':
        name = request.json['name']
        food_type = request.json['food_type']
        calories = request.json['calories']
        res = dish.create_dish(name, food_type, calories)
        return jsonify(res)
    else:
        return jsonify(False), 403
Exemple #36
0
    def on_ok(self, host, host_result):
        delegate_to = self.runner.module_vars.get('delegate_to')
        if delegate_to:
            host = '%s -> %s' % (host, delegate_to)

        item = host_result.get('item', None)

        host_result2 = host_result.copy()
        host_result2.pop('invocation', None)
        verbose_always = host_result2.pop('verbose_always', False)
        changed = host_result.get('changed', False)
        ok_or_changed = 'ok'
        if changed:
            ok_or_changed = 'changed'

        # show verbose output for non-setup module results if --verbose is used
        msg = ''
        if (not self.verbose or host_result2.get("verbose_override", None)
                is not None) and not verbose_always:
            if item:
                msg = "%s: [%s] => (item=%s)" % (ok_or_changed, host, item)
            else:
                if 'ansible_job_id' not in host_result or 'finished' in host_result:
                    msg = "%s: [%s]" % (ok_or_changed, host)
        else:
            # verbose ...
            if item:
                msg = "%s: [%s] => (item=%s) => %s" % (
                    ok_or_changed, host, item,
                    utils.jsonify(host_result2, format=verbose_always))
            else:
                if 'ansible_job_id' not in host_result or 'finished' in host_result2:
                    msg = "%s: [%s] => %s" % (
                        ok_or_changed, host,
                        utils.jsonify(host_result2, format=verbose_always))

        if msg != '':
            if not changed:
                display(msg, color='green', runner=self.runner)
            else:
                display(msg, color='yellow', runner=self.runner)
        if constants.COMMAND_WARNINGS and 'warnings' in host_result2 and host_result2[
                'warnings']:
            for warning in host_result2['warnings']:
                display("warning: %s" % warning,
                        color='purple',
                        runner=self.runner)
        super(PlaybookRunnerCallbacks, self).on_ok(host, host_result)
Exemple #37
0
 def on_unreachable(self, host, res):
     if type(res) == dict:
         res = res.get("msg", "")
     display("%s | FAILED => %s" % (host, res), stderr=True, color="red", runner=self.runner)
     if self.options.tree:
         utils.write_tree_file(self.options.tree, host, utils.jsonify(dict(failed=True, msg=res), format=True))
     super(CliRunnerCallbacks, self).on_unreachable(host, res)
Exemple #38
0
    def post(self):
        """
        Handles process of new citizens insertion
        """
        json_data = request.get_json(force=True)  # force needed to handle wrong MIME types, probably useless

        if "citizens" not in json_data:
            logger.log(30, f"Got no citizens in json_data")
            return Response(status=400)
        if not len(json_data["citizens"]):
            logger.log(30, f"Got len of citizens < 1")
            return Response(status=400)
        if utils.broken_relatives(json_data["citizens"]):
            logger.log(30, f"Got broken relatives")
            return Response(status=400)
        for citizen in json_data["citizens"]:
            if not utils.datetime_correct(citizen["birth_date"]):
                logger.log(30, f"Got broken date {citizen['birth_date']}")
                return Response(status=400)

        import_id = utils.next_collection(db)
        logger.log(10, f"New collection is {import_id}")
        db[str(import_id)].insert_many(json_data["citizens"])
        logger.log(30, f"Processed /imports normally")
        return Response(
            response=utils.jsonify({
                "data": {
                    "import_id": import_id
                }
            }),
            status=201,
            mimetype="application/json"
        )
def create_contact_person():
    xero_tenant_id = get_xero_tenant_id()
    accounting_api = AccountingApi(api_client)

    contact_person = ContactPerson(
        first_name="John",
        last_name="Smith",
        email_address="*****@*****.**",
        include_in_emails=True,
    )
    contact = Contact(
        name="FooBar",
        first_name="Foo",
        last_name="Bar",
        email_address="*****@*****.**",
        contact_persons=[contact_person],
    )
    contacts = Contacts(contacts=[contact])
    try:
        created_contacts = accounting_api.create_contacts(
            xero_tenant_id, contacts=contacts)  # type: Contacts
    except AccountingBadRequestException as exception:
        sub_title = "Error: " + exception.reason
        code = jsonify(exception.error_data)
    else:
        sub_title = "Contact {} created.".format(
            getvalue(created_contacts, "contacts.0.name", ""))
        code = serialize_model(created_contacts)

    return render_template("code.html",
                           title="Create Contacts",
                           code=code,
                           sub_title=sub_title)
Exemple #40
0
 def on_async_failed(self, host, res, jid):
     display("<job %s> FAILED on %s => %s" %
             (jid, host, utils.jsonify(res, format=True)),
             color='red',
             stderr=True,
             runner=self.runner)
     super(CliRunnerCallbacks, self).on_async_failed(host, res, jid)
Exemple #41
0
def upload_receipt():
    ''' Receipt upload handler
    Save image file to folder, process with OCR,
    create receipt to DB and return data to client.
    Time is passed as isoformatted time string in json,
    eg. '2014-09-26T13:09:19.730800'
    '''
    image_file = request.files['file']
    if image_file and allowed_file(image_file.filename):
        filename = secure_filename(image_file.filename)
        if not os.path.exists(app.config['UPLOAD_FOLDER']):
            os.mkdir(app.config['UPLOAD_FOLDER'])

        imagepath = os.path.join(app.root_path + '/' +
                                 app.config['UPLOAD_FOLDER'], filename)
        image_file.save(imagepath)
        app.logger.debug("Upload OK, saved file " + imagepath)

        ocr_readings = optical_character_recognition(imagepath)[2]

        time_now = datetime.now()
        ocr_readings['date'] = time_now.isoformat()

        # TODO create a new receipt object to db and return it
        return jsonify(ocr_readings)
Exemple #42
0
 def process_exception(self, request, exception):
     if request.path.startswith('/api/'):
         status = getattr(exception, 'http_equiv', 500)
         return jsonify({
             'status': 'error',
             'message': exception.message,
         }, status=status)
Exemple #43
0
def api_uninstall_module(module_name):
    try:
        success = packaging.uninstall(module_name)
        pass
    except ValueError:
        abort(403)
    return jsonify({ 'success': success })
def upload_receipt():
    ''' Receipt upload handler
    Save image file to folder, process with OCR,
    create receipt to DB and return data to client. '''
    image_file = request.files['file']
    if image_file and allowed_file(image_file.filename):
        filename = secure_filename(image_file.filename)
        if not os.path.exists(app.config['UPLOAD_FOLDER']):
            os.mkdir(app.config['UPLOAD_FOLDER'])

        imagepath = os.path.join(app.root_path + '/' +
                                 app.config['UPLOAD_FOLDER'], filename)
        image_file.save(imagepath)
        app.logger.debug("Upload OK, saved file " + imagepath)

        ocr_readings = dict({'credit_card': True, 'total_sum': 4.68,
                             'shop_name': "Mock Market", 'products':
                             [{'price': 1.59, 'name': 'Elonen ruisevas 540g'},
                              {'price': 0.75, 'name': 'Pirkka hanaani'},
                              {'price': 1.59, 'name': 'Elonen ruisevas 540g'},
                              {'price': 0.75, 'name': 'Pirkka hanaani'}],
                             'date': 2014-10-12})

        # TODO create a new receipt object to db and return it
        return jsonify(ocr_readings)
Exemple #45
0
 def _on_any(self, host, result):
     result2 = result.copy()
     result2.pop('invocation', None)
     (msg, color) = host_report_msg(host, self.options.module_name, result2, self.options.one_line)
     display(msg, color=color, runner=self.runner)
     if self.options.tree:
         utils.write_tree_file(self.options.tree, host, utils.jsonify(result2,format=True))
Exemple #46
0
 async def mod_me(self, ctx):
     sent_data = ctx.sent_data
     user = ctx.user
     new_username = sent_data['username']
     user.username = new_username
     user.save()
     return ctx.respond(jsonify(user))
Exemple #47
0
def api_module_field_status(module_name, field_name):
    if not packaging.is_installed(module_name):
        abort(404)

    try:
        mod = use_module(module_name)
    except RuntimeError as e:
        app.logger.exception(e)
        abort(404)
    else:
        # field exists ?
        if not field_name in mod.fields_info:
            abort(400)

        # field is readable ?
        f = mod.fields_info[field_name]
        if 'readable' not in f or not f['readable']:
            abort(403)

        # field returns value ?
        value = getattr(mod, f['name'])()
        if value is None:
            abort(404)

        return jsonify(dict(zip(('time', 'value'), value)))
Exemple #48
0
def finish_ride(user, id):
    """
    Finishes the ride
    """
    data = request.json
    user_id = user['_id']
    location = data.get("location")
    db.rides.ensure_index([("current_location", GEO2D)])
    db.ride_routes.ensure_index([("current_location", GEO2D)])

    ride = db.rides.find_one({'_id': ObjectId(id)})
    assert ride
    assert ride['user_id'] == user_id

    db.ride_routes.insert({'current_location': location, 'created_at': datetime.utcnow(),
                           'ride_id': id})
    db.rides.update({'_id': ObjectId(id)}, {'$set':{'end_location': location, 'status': 'finished'}})

    d = calc_ride_distance(ride_id=id)

    response = jsonify({
        "ride_id": str(id),
        "distance": d['dis'],
        'elapsed_seconds': d['t'],
        "donate": calc_donate(kms=d['dis'])
    })
    response.status_code = 200
    return response
Exemple #49
0
 def _on_any(self, host, result):
     result2 = result.copy()
     result2.pop('invocation', None)
     (msg, color) = host_report_msg(host, self.options.module_name, result2, self.options.one_line)
     display(msg, color=color, runner=self.runner)
     if self.options.tree:
         utils.write_tree_file(self.options.tree, host, utils.jsonify(result2,format=True))
def get_orders():
    if 'status' in request.args:
        selected_orders = orders.get_all_with_status(request.args['status'])
    else:
        selected_orders = orders.get_all()
    response = { 'orders' : [o.to_admin_details() for o in selected_orders] }
    return jsonify(response)
Exemple #51
0
 def on_async_failed(self, host, res, jid):
     display(
         "<job %s> FAILED on %s => %s" % (jid, host, utils.jsonify(res, format=True)),
         color="red",
         stderr=True,
         runner=self.runner,
     )
     super(CliRunnerCallbacks, self).on_async_failed(host, res, jid)
Exemple #52
0
def model(request, cube_name):
    """ Get the model for the specified cube. """
    cube = get_cube(cube_name)
    return jsonify({
        'status': 'ok',
        'name': cube_name,
        'model': cube.model
    })
Exemple #53
0
def api_available_modules():
    available_modules = catalog.get_available_modules(detailed=True)
    for av in available_modules:
        try:
            av['rating'] = Rating.average(av[catalog.MODULE_NAME_ENTRY])
        except Rating.DoesNotExist:
            pass
    return jsonify(available_modules)
Exemple #54
0
def api_module_info(module_name):
    try:
        module_info = use_module(module_name).info
        module_info['name'] = path.realname(module_info['name']) # hack
        return jsonify(module_info)
    except (TypeError, RuntimeError) as e:
        app.logger.exception(e)
        abort(404)
Exemple #55
0
def status(request):
    """ General system status report :) """
    from babbage import __version__, __doc__
    return jsonify({
        'status': 'ok',
        'api': 'babbage',
        'message': __doc__,
        'version': __version__
    })
Exemple #56
0
def authenticate():
    token = request.json.get("token")

    user = db.users.find_one({"token": token})

    if not user:
        abort(401)

    return jsonify(user)
Exemple #57
0
def invite(request, sid, pid):
	user = request.user.student_profile
	other_student = StudentProfile.objects.get(pk=sid)
	project = Project.objects.get(pk=pid)
	if(user.get_relationship(other_student, project) == "None"):
		new_pairing = Pairing(student1=user, student2=other_student, project=project)
		new_pairing.save()
		return jsonify({'status':'success',
					'user_id':user.pk,
					'student_id':other_student.pk,
					'project_id':project.pk,
					'row_html':student_row_html(request, sid, pid),
				})
	else:
		return jsonify({'status':'RelationExists',
					'user_id':user.pk,
					'student_id':other_student.pk,
					'project_id':project.pk,
					'row_html':student_row_html(request, sid, pid),})
Exemple #58
0
 def on_unreachable(self, host, res):
     if type(res) == dict:
         res = res.get('msg','')
     print "%s | FAILED => %s" % (host, res)
     if self.options.tree:
         utils.write_tree_file(
             self.options.tree, host,
             utils.jsonify(dict(failed=True, msg=res),format=True)
         )
     super(CliRunnerCallbacks, self).on_unreachable(host, res)