2016-04-14 23:39:37 +02:00
|
|
|
from __future__ import absolute_import
|
|
|
|
from __future__ import print_function
|
|
|
|
|
2016-06-04 16:52:18 +02:00
|
|
|
from typing import Any
|
|
|
|
|
2016-04-14 23:39:37 +02:00
|
|
|
import os
|
|
|
|
import ujson
|
|
|
|
from optparse import make_option
|
|
|
|
|
|
|
|
from django.test import Client
|
|
|
|
from django.conf import settings
|
2016-11-03 10:22:19 +01:00
|
|
|
from django.core.management.base import BaseCommand, CommandParser
|
2016-04-14 23:39:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
help = """
|
|
|
|
Create webhook message based on given fixture
|
|
|
|
Example:
|
|
|
|
./manage.py send_webhook_fixture_message \
|
|
|
|
--fixture=zerver/fixtures/integration/fixture.json \
|
|
|
|
'--url=/api/v1/external/integration?stream=stream_name&api_key=api_key'
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2016-11-03 10:22:19 +01:00
|
|
|
def add_arguments(self, parser):
|
|
|
|
# type: (CommandParser) -> None
|
|
|
|
parser.add_argument('-f', '--fixture',
|
|
|
|
dest='fixture',
|
|
|
|
type=str,
|
|
|
|
help='The path to the fixture you\'d like to send '
|
|
|
|
'into Zulip')
|
|
|
|
|
|
|
|
parser.add_argument('-u', '--url',
|
|
|
|
dest='url',
|
|
|
|
type=str,
|
|
|
|
help='The url on your Zulip server that you want '
|
|
|
|
'to post the fixture to')
|
2016-04-14 23:39:37 +02:00
|
|
|
|
|
|
|
def handle(self, **options):
|
2016-06-04 16:52:18 +02:00
|
|
|
# type: (*Any, **str) -> None
|
2016-04-14 23:39:37 +02:00
|
|
|
if options['fixture'] is None or options['url'] is None:
|
|
|
|
self.print_help('python manage.py', 'send_webhook_fixture_message')
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
full_fixture_path = os.path.join(settings.DEPLOY_ROOT, options['fixture'])
|
|
|
|
|
|
|
|
if not self._does_fixture_path_exist(full_fixture_path):
|
|
|
|
print('Fixture {} does not exist'.format(options['fixture']))
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
json = self._get_fixture_as_json(full_fixture_path)
|
|
|
|
client = Client()
|
|
|
|
client.post(options['url'], json, content_type="application/json")
|
|
|
|
|
|
|
|
def _does_fixture_path_exist(self, fixture_path):
|
2016-06-04 16:52:18 +02:00
|
|
|
# type: (str) -> bool
|
2016-04-14 23:39:37 +02:00
|
|
|
return os.path.exists(fixture_path)
|
|
|
|
|
|
|
|
def _get_fixture_as_json(self, fixture_path):
|
2016-06-04 16:52:18 +02:00
|
|
|
# type: (str) -> str
|
2016-04-14 23:39:37 +02:00
|
|
|
return ujson.dumps(ujson.loads(open(fixture_path).read()))
|