예제 #1
0
파일: jobs.py 프로젝트: ruanyuemin/pybossa
def webhook(url, payload=None, oid=None):
    """Post to a webhook."""
    from flask import current_app

    try:
        import json
        from pybossa.core import sentinel, webhook_repo

        headers = {"Content-type": "application/json", "Accept": "text/plain"}
        if oid:
            webhook = webhook_repo.get(oid)
        else:
            webhook = Webhook(project_id=payload["project_id"], payload=payload)
        if url:
            response = requests.post(url, data=json.dumps(payload), headers=headers)
            webhook.response = response.text
            webhook.response_status_code = response.status_code
        else:
            raise requests.exceptions.ConnectionError("Not URL")
        if oid:
            webhook_repo.update(webhook)
            webhook = webhook_repo.get(oid)
        else:
            webhook_repo.save(webhook)
    except requests.exceptions.ConnectionError:
        webhook.response = "Connection Error"
        webhook.response_status_code = None
        webhook_repo.save(webhook)
    if current_app.config.get("SSE"):
        publish_channel(sentinel, payload["project_short_name"], data=webhook.dictize(), type="webhook", private=True)
    return webhook
예제 #2
0
def webhook(url, payload=None, oid=None):
    """Post to a webhook."""
    from flask import current_app
    try:
        import json
        from pybossa.core import sentinel, webhook_repo
        headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
        if oid:
            webhook = webhook_repo.get(oid)
        else:
            webhook = Webhook(project_id=payload['project_id'],
                              payload=payload)
        if url:
            response = requests.post(url, data=json.dumps(payload), headers=headers)
            webhook.response = response.text
            webhook.response_status_code = response.status_code
        else:
            raise requests.exceptions.ConnectionError('Not URL')
        if oid:
            webhook_repo.update(webhook)
            webhook = webhook_repo.get(oid)
        else:
            webhook_repo.save(webhook)
    except requests.exceptions.ConnectionError:
        webhook.response = 'Connection Error'
        webhook.response_status_code = None
        webhook_repo.save(webhook)
    if current_app.config.get('SSE'):
        publish_channel(sentinel, payload['project_short_name'],
                        data=webhook.dictize(), type='webhook',
                        private=True)
    return webhook
예제 #3
0
def webhook(url, payload=None, oid=None, rerun=False):
    """Post to a webhook."""
    from flask import current_app
    from readability.readability import Document
    try:
        import json
        from pybossa.core import sentinel, webhook_repo, project_repo
        project = project_repo.get(payload['project_id'])
        headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
        if oid:
            webhook = webhook_repo.get(oid)
        else:
            webhook = Webhook(project_id=payload['project_id'],
                              payload=payload)
        if url:
            params = dict()
            if rerun:
                params['rerun'] = True
            response = requests.post(url,
                                     params=params,
                                     data=json.dumps(payload),
                                     headers=headers)
            webhook.response = Document(response.text).summary()
            webhook.response_status_code = response.status_code
        else:
            raise requests.exceptions.ConnectionError('Not URL')
        if oid:
            webhook_repo.update(webhook)
            webhook = webhook_repo.get(oid)
        else:
            webhook_repo.save(webhook)
    except requests.exceptions.ConnectionError:
        webhook.response = 'Connection Error'
        webhook.response_status_code = None
        webhook_repo.save(webhook)
    finally:
        if project.published and webhook.response_status_code != 200 and current_app.config.get(
                'ADMINS'):
            subject = "Broken: %s webhook failed" % project.name
            body = 'Sorry, but the webhook failed'
            mail_dict = dict(recipients=current_app.config.get('ADMINS'),
                             subject=subject,
                             body=body,
                             html=webhook.response)
            send_mail(mail_dict)
    if current_app.config.get('SSE'):
        publish_channel(sentinel,
                        payload['project_short_name'],
                        data=webhook.dictize(),
                        type='webhook',
                        private=True)
    return webhook
예제 #4
0
 def test_webhook_handler_post_oid_404(self):
     """Test WEBHOOK post oid 404 works."""
     self.register()
     user = user_repo.get(1)
     project = ProjectFactory.create(owner=user)
     task = TaskFactory.create(project=project, n_answers=1)
     AnonymousTaskRunFactory.create(project=project, task=task)
     payload = self.payload(project, task)
     webhook = Webhook(project_id=project.id, payload=payload,
                       response='OK', response_status_code=200)
     webhook_repo.save(webhook)
     url = "/project/%s/webhook/%s" % (project.short_name, 9999)
     res = self.app.post(url)
     assert res.status_code == 404, res.status_code
예제 #5
0
 def test_webhook_handler_post_oid_404(self):
     """Test WEBHOOK post oid 404 works."""
     self.register()
     self.signin()
     user = user_repo.get(1)
     project = ProjectFactory.create(owner=user)
     task = TaskFactory.create(project=project, n_answers=1)
     AnonymousTaskRunFactory.create(project=project, task=task)
     payload = self.payload(project, task)
     webhook = Webhook(project_id=project.id, payload=payload,
                       response='OK', response_status_code=200)
     webhook_repo.save(webhook)
     url = "/project/%s/webhook/%s" % (project.short_name, 9999)
     res = self.app.post(url)
     assert res.status_code == 404, res.status_code
예제 #6
0
파일: jobs.py 프로젝트: PyBossa/pybossa
def webhook(url, payload=None, oid=None, rerun=False):
    """Post to a webhook."""
    from flask import current_app
    from readability.readability import Document
    try:
        import json
        from pybossa.core import sentinel, webhook_repo, project_repo
        project = project_repo.get(payload['project_id'])
        headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
        if oid:
            webhook = webhook_repo.get(oid)
        else:
            webhook = Webhook(project_id=payload['project_id'],
                              payload=payload)
        if url:
            params = dict()
            if rerun:
                params['rerun'] = True
            response = requests.post(url, params=params,
                                     data=json.dumps(payload),
                                     headers=headers)
            webhook.response = Document(response.text).summary()
            webhook.response_status_code = response.status_code
        else:
            raise requests.exceptions.ConnectionError('Not URL')
        if oid:
            webhook_repo.update(webhook)
            webhook = webhook_repo.get(oid)
        else:
            webhook_repo.save(webhook)
    except requests.exceptions.ConnectionError:
        webhook.response = 'Connection Error'
        webhook.response_status_code = None
        webhook_repo.save(webhook)
    finally:
        if project.published and webhook.response_status_code != 200 and current_app.config.get('ADMINS'):
            subject = "Broken: %s webhook failed" % project.name
            body = 'Sorry, but the webhook failed'
            mail_dict = dict(recipients=current_app.config.get('ADMINS'),
                             subject=subject, body=body, html=webhook.response)
            send_mail(mail_dict)
    if current_app.config.get('SSE'):
        publish_channel(sentinel, payload['project_short_name'],
                        data=webhook.dictize(), type='webhook',
                        private=True)
    return webhook
예제 #7
0
 def test_webhook_handler_post_oid(self):
     """Test WEBHOOK post oid works."""
     self.register()
     user = user_repo.get(1)
     project = ProjectFactory.create(owner=user)
     task = TaskFactory.create(project=project, n_answers=1)
     AnonymousTaskRunFactory.create(project=project, task=task)
     payload = self.payload(project, task)
     webhook = Webhook(project_id=project.id, payload=payload,
                       response='OK', response_status_code=200)
     webhook_repo.save(webhook)
     webhook = webhook_repo.get(1)
     url = "/project/%s/webhook/%s" % (project.short_name, webhook.id)
     res = self.app.post(url)
     tmp = json.loads(res.data)
     assert res.status_code == 200, res.status_code
     assert tmp['payload']['project_short_name'] == project.short_name
     assert tmp['payload']['project_id'] == project.id
     assert tmp['payload']['task_id'] == task.id
예제 #8
0
 def test_webhook_handler_post_oid(self):
     """Test WEBHOOK post oid works."""
     self.register()
     self.signin()
     user = user_repo.get(1)
     project = ProjectFactory.create(owner=user)
     task = TaskFactory.create(project=project, n_answers=1)
     AnonymousTaskRunFactory.create(project=project, task=task)
     payload = self.payload(project, task)
     webhook = Webhook(project_id=project.id, payload=payload,
                       response='OK', response_status_code=200)
     webhook_repo.save(webhook)
     webhook = webhook_repo.get(1)
     url = "/project/%s/webhook/%s" % (project.short_name, webhook.id)
     res = self.app.post(url)
     tmp = json.loads(res.data)
     assert res.status_code == 200, res.status_code
     assert tmp['payload']['project_short_name'] == project.short_name
     assert tmp['payload']['project_id'] == project.id
     assert tmp['payload']['task_id'] == task.id
예제 #9
0
 def test_webhook_handler_all(self, q):
     """Test WEBHOOK requeing all works."""
     self.register()
     self.signin()
     user = user_repo.get(1)
     project = ProjectFactory.create(owner=user, webhook='server')
     task = TaskFactory.create(project=project, n_answers=1)
     AnonymousTaskRunFactory.create(project=project, task=task)
     payload = self.payload(project, task)
     wh1 = Webhook(project_id=project.id,
                   payload=payload,
                   response='error',
                   response_status_code=500)
     webhook_repo.save(wh1)
     wh2 = Webhook(project_id=project.id,
                   payload=payload,
                   response='ok',
                   response_status_code=200)
     webhook_repo.save(wh2)
     wh3 = Webhook(project_id=project.id,
                   payload=payload,
                   response='ok',
                   response_status_code=200)
     webhook_repo.save(wh3)
     whs = webhook_repo.filter_by(project_id=project.id)
     url = "/project/%s/webhook?all=true" % (project.short_name)
     res = self.app.get(url)
     assert res.status_code == 200, res.status_code
     calls = []
     for w in whs:
         calls.append(call(webhook, project.webhook, w.payload, w.id))
     q.assert_has_calls(calls)
예제 #10
0
    def test_webhook_handler_failed(self, q):
        """Test WEBHOOK requeing failed works."""
        self.register()
        self.signin()
        user = user_repo.get(1)
        project = ProjectFactory.create(owner=user, webhook='server')
        task = TaskFactory.create(project=project, n_answers=1)
        AnonymousTaskRunFactory.create(project=project, task=task)
        payload = self.payload(project, task)
        wh = Webhook(project_id=project.id,
                     payload=payload,
                     response='error',
                     response_status_code=500)
        webhook_repo.save(wh)
        wh2 = Webhook(project_id=project.id,
                      payload=payload,
                      response='ok',
                      response_status_code=200)
        webhook_repo.save(wh2)
        wh3 = Webhook(project_id=project.id,
                      payload=payload,
                      response='ok',
                      response_status_code=200)
        webhook_repo.save(wh3)

        wh = webhook_repo.get(1)
        url = "/project/%s/webhook?failed=true" % (project.short_name)
        res = self.app.get(url)
        assert res.status_code == 200, res.status_code
        q.assert_called_once_with(webhook, project.webhook, wh.payload, wh.id)
예제 #11
0
 def test_webhook_handler_all(self, q):
     """Test WEBHOOK requeing all works."""
     self.register()
     user = user_repo.get(1)
     project = ProjectFactory.create(owner=user, webhook='server')
     task = TaskFactory.create(project=project, n_answers=1)
     AnonymousTaskRunFactory.create(project=project, task=task)
     payload = self.payload(project, task)
     wh1 = Webhook(project_id=project.id, payload=payload,
                  response='error', response_status_code=500)
     webhook_repo.save(wh1)
     wh2 = Webhook(project_id=project.id, payload=payload,
                   response='ok', response_status_code=200)
     webhook_repo.save(wh2)
     wh3 = Webhook(project_id=project.id, payload=payload,
                   response='ok', response_status_code=200)
     webhook_repo.save(wh3)
     whs = webhook_repo.filter_by(project_id=project.id)
     url = "/project/%s/webhook?all=true" % (project.short_name)
     res = self.app.get(url)
     assert res.status_code == 200, res.status_code
     calls = []
     for w in whs:
         calls.append(call(webhook, project.webhook,
                           w.payload, w.id, True))
     q.assert_has_calls(calls)
예제 #12
0
    def test_webhook_handler_failed(self, q):
        """Test WEBHOOK requeing failed works."""
        self.register()
        user = user_repo.get(1)
        project = ProjectFactory.create(owner=user, webhook='server')
        task = TaskFactory.create(project=project, n_answers=1)
        AnonymousTaskRunFactory.create(project=project, task=task)
        payload = self.payload(project, task)
        wh = Webhook(project_id=project.id, payload=payload,
                     response='error', response_status_code=500)
        webhook_repo.save(wh)
        wh2 = Webhook(project_id=project.id, payload=payload,
                      response='ok', response_status_code=200)
        webhook_repo.save(wh2)
        wh3 = Webhook(project_id=project.id, payload=payload,
                      response='ok', response_status_code=200)
        webhook_repo.save(wh3)

        wh = webhook_repo.get(1)
        url = "/project/%s/webhook?failed=true" % (project.short_name)
        res = self.app.get(url)
        assert res.status_code == 200, res.status_code
        q.assert_called_once_with(webhook, project.webhook,
                                  wh.payload, wh.id, True)