示例#1
0
文件: campaigns.py 项目: iofun/howler
    def put(self, campaign_uuid):
        '''
            Replace campaign
        '''
        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('put received struct {0}'.format(struct))

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

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

        result = yield self.replace_campaign(account, campaign_uuid, struct)

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

        self.set_status(200)
        self.finish({'message': 'replace completed successfully'})
示例#2
0
文件: contacts.py 项目: iofun/howler
    def patch(self, contact_uuid):
        '''
            Modify contact
        '''
        struct = yield check_json(self.request.body)

        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]

        if not account:
            # if not account we try to get the account from struct
            account = struct.get('account', None)

        result = yield self.modify_contact(account, contact_uuid, struct)

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

        self.set_status(200)
        self.finish({'message': 'update completed successfully'})
示例#3
0
文件: contacts.py 项目: iofun/howler
    def post(self):
        '''
            Create contact
        '''
        # post structure
        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

        # hello mr satan, where is booo?
        logging.info(struct.get('account', 'booo'))

        # settings database
        db = self.settings.get('db')

        # logging new contact structure
        logging.info('new contact structure {0}'.format(str(format_pass)))

        # request query arguments
        query_args = self.request.arguments

        # get account from new contact struct
        account = struct.get('account', None)

        # get the current frontend logged username
        username = self.get_current_username()

        # if the user don't provide an account we use the frontend username as last resort
        account = (query_args.get('account', [username])[0] if not account else account)

        # we use the front-end username as last resort
        # if not struct.get('account'):
        #     struct['account'] = account

        new_contact = yield self.new_contact(struct)

        if 'error' in new_contact:
            scheme = 'contact'
            reason = {'duplicates': [
                (scheme, 'account'),
                (scheme, 'phone_number')
            ]}
            message = yield self.let_it_crash(struct, scheme, new_contact, reason)

            logging.warning(message)

            self.set_status(400)
            self.finish(message)
            return

        self.set_status(201)
        self.finish({'uuid':new_contact})
示例#4
0
文件: campaigns.py 项目: iofun/howler
    def post(self):
        '''
            Create campaign
        '''
        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

        # setting database
        db = self.settings.get('db')

        # logging new contact structure
        logging.info('new campaign structure {0}'.format(str(struct)))

        # request query arguments
        query_args = self.request.arguments

        # get account from struct
        account = struct.get('account', None)

        # get the current gui username
        username = self.get_current_username()

        # 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)

        # we use the front-end username as last resort
        if not struct.get('account'):
            struct['account'] = account

        logging.warning(account)

        new_campaign = yield self.new_campaign(struct)

        if 'error' in new_campaign:
            model = 'Campaign'
            reason = {'duplicates': [(model, 'account')]}

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

            logging.warning(message)
            self.set_status(400)
            return

        self.set_status(201)
        self.finish({'uuid':new_campaign})
示例#5
0
文件: campaigns.py 项目: iofun/howler
    def patch(self, campaign_uuid):
        '''
            Modify campaign
        '''
        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]

        if not account:
            # if not account we try to get the account from struct
            account = struct.get('account', None)

        logging.warning('account {0} uuid {1}'.format(account, campaign_uuid))

        result = yield self.modify_campaign(account, campaign_uuid, struct)

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

        self.set_status(200)
        self.finish({'message': 'update completed successfully'})
示例#6
0
文件: outbound.py 项目: iofun/howler
    def post(self):
        '''
            Create outbound
        '''
        # post structure
        struct = yield check_json(self.request.body)
        
        # format pass validation
        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

        # settings database
        db = self.settings.get('db')

        # request query arguments
        query_args = self.request.arguments

        # get account from new contact struct
        account = struct.get('account', None)

        # get the current frontend logged username
        username = self.get_current_username()

        # if the user don't provide an account we use the frontend username as last resort
        account = (query_args.get('account', [username])[0] if not account else account)
        if not struct.get('account'):
            struct['account'] = account

        # acknowledge message
        ack_msg = 'Your call is important to us, please hold while we connect your call.'

        # can't wait for this shit to got some response
        got_response = []

        # yeah this is our f*****g structure
        # logging.info(struct)

        def handle_request(response):
            '''
                Request Async Handler
            '''
            # message become the final result
            message = {}

            if response.error:
                message['status'] = int(response.error.code)
                message['message'] = str(response.error)
            else:
                message['status'] = 202
                message['message'] = ack_msg

            got_response.append(message)

        try:
            http_client.fetch(
                'https://iofun.io/calls/', 
                headers={"Content-Type": "application/json"},
                method='POST',
                body=json.dumps(struct),
                callback=handle_request
            )
            while len(got_response) == 0:
                # Hey! don't be too careless with the time.
                yield gen.sleep(0.0010)
        except Exception, e:
            logging.exception(e)
            raise e