コード例 #1
0
 def test_proper_encoding(self):
     obj = {'countries': ['United Kingdom', 'France', 'España']}
     renderer = UnicodeJSONRenderer()
     content = renderer.render(obj, 'application/json')
     self.assertEqual(
         content,
         '{"countries": ["United Kingdom", "France", "España"]}'.encode(
             'utf-8'))
コード例 #2
0
def _send_request(webhook_id, url, key, data):
    serialized_data = UnicodeJSONRenderer().render(data)
    signature = _generate_signature(serialized_data, key)
    headers = {
        "X-TAIGA-WEBHOOK-SIGNATURE": signature,
        "Content-Type": "application/json"
    }
    request = requests.Request('POST', url, data=serialized_data, headers=headers)
    prepared_request = request.prepare()

    session = requests.Session()
    try:
        response = session.send(prepared_request)
        webhook_log = WebhookLog.objects.create(webhook_id=webhook_id, url=url,
                                                status=response.status_code,
                                                request_data=data,
                                                request_headers=dict(prepared_request.headers),
                                                response_data=response.content,
                                                response_headers=dict(response.headers),
                                                duration=response.elapsed.total_seconds())
    except RequestException as e:
        webhook_log = WebhookLog.objects.create(webhook_id=webhook_id, url=url, status=0,
                                                request_data=data,
                                                request_headers=dict(prepared_request.headers),
                                                response_data="error-in-request: {}".format(str(e)),
                                                response_headers={},
                                                duration=0)
    session.close()

    ids = [log.id for log in WebhookLog.objects.filter(webhook_id=webhook_id).order_by("-id")[10:]]
    WebhookLog.objects.filter(id__in=ids).delete()
    return webhook_log
コード例 #3
0
def _send_request(url, data):
    data["notify"] = True
    serialized_data = UnicodeJSONRenderer().render(data)
    headers = {
        'Content-type': 'application/json',
    }
    if settings.CELERY_ENABLED:
        requests.post(url, data=serialized_data, headers=headers)
        return
    try:
        requests.post(url, data=serialized_data, headers=headers)
    except Exception:
        logger.error("Error sending request to HipChat")
コード例 #4
0
def _send_request(url, data):
    data["username"] = getattr(settings, "SLACKHOOKS_USERNAME", "Taiga")
    data["icon_url"] = getattr(settings, "SLACKHOOKS_ICON",
                               "https://tree.taiga.io/images/favicon.png")

    serialized_data = UnicodeJSONRenderer().render(data)

    if settings.CELERY_ENABLED:
        requests.post(url, data=serialized_data)
        return

    try:
        requests.post(url, data=serialized_data)
    except Exception:
        logger.error("Error sending request to slack")
コード例 #5
0
ファイル: views.py プロジェクト: tfrancart/plan4learning
    def get_context_data(self, **kwargs):

        context = super(RecordEditView, self).get_context_data(**kwargs)

        if self.object:
            serializer = RecordSerializer(self.object)
            context['object_json'] = UnicodeJSONRenderer().render(
                serializer.data)
        else:
            context['object_json'] = "null"

        # We get the language, "fr" by default
        lang = "fr"
        if "lang" in self.request.GET:
            lang = self.request.GET["lang"]
        elif hasattr(self.request,
                     "LANGUAGE_CODE") and self.request.LANGUAGE_CODE in [
                         "fr", "en", "es"
                     ]:
            lang = self.request.LANGUAGE_CODE

        context['uri_labels'] = json.dumps(
            get_record_uri_labels(self.object, lang))

        # lang must be like "XX" in the sparql request
        lang = '"' + lang + '"'

        query_dicts = dict([(k,
                             dict([(QUERY_FIELDS_TRANSLATION[key],
                                    value.format(lang=lang))
                                   for key, value in v.items()]))
                            for k, v in settings.SPARQL_REF_QUERIES.items()])
        context['query_dicts'] = json.dumps(query_dicts)

        # Languages list used in drop down list
        context['languages_list'] = json.dumps(settings.LANGUAGES_LIST)

        context['is_create_view'] = json.dumps(self.is_create_view)

        return context
コード例 #6
0
 def test_proper_encoding(self):
     obj = {"countries": ["United Kingdom", "France", "España"]}
     renderer = UnicodeJSONRenderer()
     content = renderer.render(obj, "application/json")
     self.assertEqual(content, '{"countries": ["United Kingdom", "France", "España"]}'.encode("utf-8"))
コード例 #7
0
 def test_proper_encoding(self):
     obj = {'countries': ['United Kingdom', 'France', 'España']}
     renderer = UnicodeJSONRenderer()
     content = renderer.render(obj, 'application/json')
     self.assertEqual(content, '{"countries": ["United Kingdom", "France", "España"]}'.encode('utf-8'))