예제 #1
0
    def patch(self, asterisk_uuid):
        '''
            Modify asterisk
        '''
        logging.info('request.arguments {0}'.format(self.request.arguments))
        logging.info('request.body {0}'.format(self.request.body))

        struct = yield check_json(self.request.body)

        logging.info('patch received struct {0}'.format(struct))

        format_pass = (True if not dict(struct).get('errors', False) else False)
        if not format_pass:
            self.set_status(400)
            self.finish({'JSON':format_pass})
            return

        account = self.request.arguments.get('account', [None])[0]

        logging.info('account {0} uuid {1} struct {2}'.format(account, asterisk_uuid, struct))

        result = yield self.modify_asterisk(account, asterisk_uuid, struct)

        if not result:
            self.set_status(400)
            system_error = errors.Error('missing')
            error = system_error.missing('asterisk', asterisk_uuid)
            self.finish(error)
            return

        self.set_status(200)
        self.finish({'message': 'update completed successfully'})
예제 #2
0
    def post(self):
        '''
            POST asterisks handler
        '''
        struct = yield check_json(self.request.body)
        db = self.settings['db']
        
        format_pass = (True if struct else False)
        if not format_pass:
            self.set_status(400)
            self.finish({'JSON':format_pass})
            return

        asterisk = yield self.new_asterisk(struct)
 
        if not asterisk:
            model = 'Asterisks'
            error = {'asterisk':False}
            reason = {'duplicates':[('Asterisk', 'uniqueid'), (model, 'uuid')]}

            message = yield self.let_it_crash(struct, model, error, reason)

            self.set_status(400)
            self.finish(message)
            return
        
        if 'accountcode' in struct:

            account = struct.get('accountcode')

            resource = {
                'account': account,
                'resource':'asterisks',
                'uuid':asterisk
            }

            exist = yield self.check_exist(account)

            logging.info('check if exist %s ' % exist)

            if exist:
                update = yield new_resource(db, resource)

                logging.info('update %s' % update)

                flag = yield self.set_assigned_flag(account, asterisk)

        logging.info('new spawned asterisk %s ' % asterisk)

        self.set_status(201)
        self.finish({'uuid':asterisk})
예제 #3
0
파일: calls.py 프로젝트: iofun/chaos
    def post(self):
        '''
            Create event
        '''
        struct = yield check_json(self.request.body)
        format_pass = (True if struct and not struct.get('errors') else False)
        if not format_pass:
            self.set_status(400)
            self.finish({'JSON':format_pass})
            return

        # query arguments
        query_args = self.request.arguments

        # get account from struct
        account = struct.get('account', None)
        # get the current gui username
        username = self.get_username_cookie()
        # if the user don't provide an account we use the username as last resort
        account = (query_args.get('account', [username])[0] if not account else account)

        # asterisk dialplan context
        context = 'fun-accounts'

        #test context
        contextdos = '{0}'.format(
            self.context
        )

        # asterisk dialplan extension
        extension = struct['extension']
        # call file priority
        priority = 1        
        # maximum number of retries by generated file, disable default.
        max_retries = 0
        # number of seconds to wait until try again.
        retry_time = 300
        # number of seconds to ring and way for answer.
        wait_time = 30
        # sip trunk, sip carrier, sip channel, etc.
        #channel = 'SIP/authority_1/' # change to dial local
        #channel = 'SIP/codemachine_1/' # change to dial local
        channel = 'SIP/fiorella_1/' # change to dial local
        
        #test channel
        
        # callerid
        caller_id = struct['caller_id']
        #callerid = struct['caller_id']
        # phone_number
        phone_number = struct['phone_number']
        # dial number
        dial_number = ''.join((channel, phone_number))
        # dial extension
        dial_extension = ''.join(('Local/', struct['extension'], '@', context))

        # call file structure
        call_struct = {
            'account': account,
            'caller_id': caller_id,
            'max_retries': max_retries,
            'retry_time': retry_time,
            'wait_time': wait_time,
            'context': context,
            'priority': priority,
            # additional fields?, please check this out!
            'phone_number': phone_number,
            #'caller_id': caller_id
            # 'keyword': keyword,
            # 'first_name': first_name,
            # 'last_name': last_name
        }
        logging.warning('handlers structs')
        logging.warning(call_struct)

        press_flag = struct.get('press_one', False)

        if press_flag == "True":
            # press_one flag enabled
            call_struct['channel'] = dial_number
            call_struct['extension'] = extension
        else:
            # not a press one normal behavior
            call_struct['channel'] = dial_extension
            call_struct['extension'] = phone_number

        call_file = yield self.spawn_call_file(call_struct)


        yield self.chown_file(call_file, self.settings['asterisk_user'], self.settings['asterisk_group'])
        yield self.move_tmp_file(call_file)

        message = 'The request has been fulfilled a new outbound call has been scheduled'

        self.set_status(202)

        self.finish({
            'status': 202,
            'message': message
        })