def to_dict(self, image_cdn=None):
     body = self.body
     if image_cdn:
         body = utils.convert_to_cdn_path(image_cdn, body)
     body = markdown.markdown(body)
     return {
         'title': self.name,
         'body': body,
         'locale': utils.to_zendesk_locale(DEFAULT_LOCALE)
     }
 def to_dict(self, image_cdn=None):
     body = self.body
     if image_cdn:
         body = utils.convert_to_cdn_path(image_cdn, body)
     body = markdown.markdown(body)
     return {
         'title': self.name,
         'body': body,
         'locale': utils.to_zendesk_locale(DEFAULT_LOCALE)
     }
 def to_dict(self, image_cdn=None):
     body = self.body
     if image_cdn:
         body = utils.convert_to_cdn_path(image_cdn, body)
     body = markdown.markdown(body, extensions=['markdown.extensions.fenced_code', 'markdown.extensions.codehilite'],
                              extension_configs=_extension_configs)
     return {
         'title': self.name,
         'body': body,
         'locale': utils.to_zendesk_locale(DEFAULT_LOCALE)
     }
 def _push_item_translations(self, item):
     missing_locales = self.req.get_missing_locales(item)
     for translation in item.translations:
         locale = utils.to_zendesk_locale(translation.locale)
         data = {'translation': translation.to_dict(self.image_cdn)}
         if locale in missing_locales:
             print('New translation for locale {}'.format(translation.locale))
             self.req.post_translation(item, data)
         else:
             if self._has_content_changed(translation, item, locale):
                 print('Updating translation for locale {}'.format(translation.locale))
                 self.req.put_translation(item, locale, data)
             else:
                 print('Nothing changed for locale {}'.format(translation.locale))
 def to_dict(self, image_cdn=None):
     body = self.body
     if image_cdn:
         body = utils.convert_to_cdn_path(image_cdn, body)
     body = markdown.markdown(body,
                              extensions=[
                                  'markdown.extensions.fenced_code',
                                  'markdown.extensions.codehilite'
                              ],
                              extension_configs=_extension_configs)
     return {
         'title': self.name,
         'body': body,
         'locale': utils.to_zendesk_locale(DEFAULT_LOCALE)
     }
Beispiel #6
0
 def _push_item_translations(self, item):
     missing_locales = self.req.get_missing_locales(item)
     for translation in item.translations:
         locale = utils.to_zendesk_locale(translation.locale)
         data = {'translation': translation.to_dict(self.image_cdn)}
         if locale in missing_locales:
             print('New translation for locale {}'.format(
                 translation.locale))
             self.req.post_translation(item, data)
         else:
             if self._has_content_changed(translation, item, locale):
                 print('Updating translation for locale {}'.format(
                     translation.locale))
                 self.req.put_translation(item, locale, data)
             else:
                 print('Nothing changed for locale {}'.format(
                     translation.locale))
Beispiel #7
0
class ZendeskRequest(object):
    _default_url = 'https://{}/api/v2/help_center/' + utils.to_zendesk_locale(
        model.DEFAULT_LOCALE) + '/{}'
    _translations_url = 'https://{}/api/v2/help_center/{}'

    item_url = '{}/{}.json'
    items_url = '{}.json?per_page=100'
    items_in_group_url = '{}/{}/{}.json?per_page=100'

    translation_url = '{}/{}/translations/{}.json'
    translations_url = '{}/{}/translations.json?per_page=100'
    missing_translations_url = '{}/{}/translations/missing.json'

    def __init__(self, company_uri, user, password):
        super().__init__()
        self.company_uri = company_uri
        self.user = user
        self.password = password

    def _url_for(self, path):
        return self._default_url.format(self.company_uri, path)

    def _translation_url_for(self, path):
        return self._translations_url.format(self.company_uri, path)

    def _parse_response(self, response):
        if response.status_code == 404:
            raise RecordNotFoundError('Missing record for {}'.format(
                response.url))
        if response.status_code not in [200, 201]:
            logging.error(
                'getting data from %s failed. status was %s and message %s',
                response.url, response.status_code, response.text)
            return {}
        return response.json()

    def _send_request(self, request_fn, url, data):
        full_url = self._url_for(url)
        response = request_fn(full_url,
                              data=json.dumps(data),
                              auth=(self.user, self.password),
                              headers={'Content-type': 'application/json'},
                              verify=False)
        return self._parse_response(response)

    def _send_translation(self, request_fn, url, data):
        full_url = self._translation_url_for(url)
        response = request_fn(full_url,
                              data=json.dumps(data),
                              auth=(self.user, self.password),
                              headers={'Content-type': 'application/json'},
                              verify=False)
        return self._parse_response(response)

    def get_item(self, item):
        url = self.item_url.format(item.zendesk_group, item.zendesk_id)
        full_url = self._url_for(url)
        response = requests.get(full_url,
                                auth=(self.user, self.password),
                                verify=False)
        return self._parse_response(response).get(item.zendesk_name, {})

    def get_items(self, item, parent=None):
        if parent:
            url = self.items_in_group_url.format(parent.zendesk_group,
                                                 parent.zendesk_id,
                                                 item.zendesk_group)
        else:
            url = self.items_url.format(item.zendesk_group)
        full_url = self._url_for(url)
        response = requests.get(full_url,
                                auth=(self.user, self.password),
                                verify=False)
        return self._parse_response(response).get(item.zendesk_group, {})

    def get_missing_locales(self, item):
        url = self.missing_translations_url.format(item.zendesk_group,
                                                   item.zendesk_id)
        full_url = self._translation_url_for(url)
        response = requests.get(full_url,
                                auth=(self.user, self.password),
                                verify=False)
        return self._parse_response(response).get('locales', [])

    def get_translation(self, item, locale):
        url = self.translation_url.format(item.zendesk_group, item.zendesk_id,
                                          locale)
        full_url = self._translation_url_for(url)
        response = requests.get(full_url,
                                auth=(self.user, self.password),
                                verify=False)
        return self._parse_response(response).get('translation', {})

    def put(self, item, data):
        url = self.item_url.format(item.zendesk_group, item.zendesk_id)
        return self._send_request(requests.put, url,
                                  data).get(item.zendesk_name, {})

    def put_translation(self, item, locale, data):
        url = self.translation_url.format(item.zendesk_group, item.zendesk_id,
                                          locale)
        return self._send_translation(requests.put, url,
                                      data).get('translation', {})

    def post(self, item, data, parent=None):
        if parent:
            url = self.items_in_group_url.format(parent.zendesk_group,
                                                 parent.zendesk_id,
                                                 item.zendesk_group)
        else:
            url = self.items_url.format(item.zendesk_group)
        return self._send_request(requests.post, url,
                                  data).get(item.zendesk_name, {})

    def post_translation(self, item, data):
        url = self.translations_url.format(item.zendesk_group, item.zendesk_id)
        return self._send_translation(requests.post, url,
                                      data).get('translation', {})

    def delete(self, item):
        url = self.item_url.format(item.zendesk_group, item.zendesk_id)
        full_url = self._url_for(url)
        return self.raw_delete(full_url)

    def raw_delete(self, full_url):
        response = requests.delete(full_url,
                                   auth=(self.user, self.password),
                                   verify=False)
        return response.status_code == 200
 def to_dict(self, image_cdn=None):
     return {
         'name': self.name,
         'description': self.description,
         'locale': utils.to_zendesk_locale(DEFAULT_LOCALE)
     }
 def to_dict(self, image_cdn=None):
     return {
         'title': self.name,
         'body': self.description,
         'locale': utils.to_zendesk_locale(self.locale)
     }
 def to_dict(self, image_cdn=None):
     return {
         'name': self.name,
         'description': self.description,
         'locale': utils.to_zendesk_locale(DEFAULT_LOCALE)
     }
 def to_dict(self, image_cdn=None):
     return {
         'title': self.name,
         'body': self.description,
         'locale': utils.to_zendesk_locale(self.locale)
     }
Beispiel #12
0
class ZendeskRequest(object):
    _default_url = 'https://{}/api/v2/help_center/' + utils.to_zendesk_locale(
        model.DEFAULT_LOCALE) + '/{}'
    _translations_url = 'https://{}/api/v2/help_center/{}'
    _users_url = 'https://{}/api/v2/users/{}'
    _search_url = 'https://{}/api/v2/search.json'
    _user_segments_url = 'https://{}/api/v2/help_center/user_segments/applicable.json'
    _permission_groups_url = 'https://{}/api/v2/guide/permission_groups.json'

    item_url = '{}/{}.json'
    items_url = '{}.json?per_page=100'
    items_in_group_url = '{}/{}/{}.json?per_page=100'

    attachment_url = 'articles/attachments/{}.json'
    translation_url = '{}/{}/translations/{}.json'

    user_url = '{}.json'

    def __init__(self, company_uri, user, password, public_uri=None):
        super().__init__()
        self.company_uri = company_uri
        self.user = user
        self.password = password
        self.public_uri = public_uri

    def _url_for(self, path):
        return self._default_url.format(self.company_uri, path)

    def _translation_url_for(self, path):
        return self._translations_url.format(self.company_uri, path)

    def _user_url_for(self, path):
        return self._users_url.format(self.company_uri, path)

    def _parse_response(self, response):
        if response.status_code == 404:
            raise RecordNotFoundError('Missing record for {}'.format(
                response.url))
        if response.status_code not in [200, 201]:
            logging.error(
                'getting data from %s failed. status was %s and message %s',
                response.url, response.status_code, response.text)
            return {}
        return response.json()

    def _send_request(self, request_fn, url, data):
        full_url = self._url_for(url)
        response = request_fn(full_url,
                              data=json.dumps(data),
                              auth=(self.user, self.password),
                              headers={'Content-type': 'application/json'},
                              verify=False)
        return self._parse_response(response)

    def _send_translation(self, request_fn, url, data):
        full_url = self._translation_url_for(url)
        response = request_fn(full_url,
                              data=json.dumps(data),
                              auth=(self.user, self.password),
                              headers={'Content-type': 'application/json'},
                              verify=False)
        return self._parse_response(response)

    def get_user(self, uid):
        full_url = self._user_url_for(self.user_url.format(uid))
        response = requests.get(full_url,
                                auth=(self.user, self.password),
                                verify=False)
        return self._parse_response(response).get('user', {})

    def search_user(self, query):
        full_url = self._search_url.format(self.company_uri)
        response = requests.get(full_url,
                                params={'query': query},
                                auth=(self.user, self.password),
                                verify=False)
        results = self._parse_response(response).get('results', [])
        if len(results) == 0:
            return False
        else:
            uid = results[0]['id']
            return self.get_user(uid)

    def get_user_segments(self):
        full_url = self._user_segments_url.format(self.company_uri)
        response = requests.get(full_url,
                                auth=(self.user, self.password),
                                verify=False)
        return self._parse_response(response).get('user_segments', [])

    def get_permission_groups(self):
        full_url = self._permission_groups_url.format(self.company_uri)
        response = requests.get(full_url,
                                auth=(self.user, self.password),
                                verify=False)
        return self._parse_response(response).get('permission_groups', [])

    def get_item(self, item):
        url = self.item_url.format(item.zendesk_group, item.zendesk_id)
        full_url = self._url_for(url)
        response = requests.get(full_url,
                                auth=(self.user, self.password),
                                verify=False)
        return self._parse_response(response).get(item.zendesk_name, {})

    def get_items(self, item, parent=None):
        if parent:
            url = self.items_in_group_url.format(parent.zendesk_group,
                                                 parent.zendesk_id,
                                                 item.zendesk_group)
        else:
            url = self.items_url.format(item.zendesk_group)
        full_url = self._url_for(url)
        response = requests.get(full_url,
                                auth=(self.user, self.password),
                                verify=False)
        return self._parse_response(response).get(
            item.zendesk_group_list_prefix + item.zendesk_group, {})

    def get_translation(self, item):
        url = self.translation_url.format(item.zendesk_group, item.zendesk_id,
                                          model.DEFAULT_LOCALE)
        full_url = self._translation_url_for(url)
        response = requests.get(full_url,
                                auth=(self.user, self.password),
                                verify=False)
        return self._parse_response(response).get('translation', {})

    def put(self, item, data):
        url = self.item_url.format(item.zendesk_group, item.zendesk_id)
        return self._send_request(requests.put, url,
                                  data).get(item.zendesk_name, {})

    def put_translation(self, item, data):
        url = self.translation_url.format(item.zendesk_group, item.zendesk_id,
                                          model.DEFAULT_LOCALE)
        return self._send_translation(requests.put, url,
                                      data).get('translation', {})

    def post(self, item, data, parent=None):
        if parent:
            url = self.items_in_group_url.format(parent.zendesk_group,
                                                 parent.zendesk_id,
                                                 item.zendesk_group)
        else:
            url = self.items_url.format(item.zendesk_group)
        return self._send_request(requests.post, url,
                                  data).get(item.zendesk_name, {})

    def post_attachment(self, attachment, attachment_filepath):
        full_url = self._url_for(attachment.new_item_url)
        response = requests.post(
            full_url,
            data={'inline': 'true'},
            files={'file': open(attachment_filepath, 'rb')},
            auth=(self.user, self.password),
            verify=False)
        return self._parse_response(response)

    def get_attachment(self, relative_path, path):
        os.makedirs(os.path.dirname(path), exist_ok=True)
        url = 'https://' + self.public_uri + relative_path
        response = requests.get(url,
                                stream=True,
                                auth=(self.user, self.password))
        if response.status_code == 200:
            with open(path, 'wb') as file:
                for chunk in response:
                    file.write(chunk)
            return True
        else:
            return False

    def delete(self, item):
        if isinstance(item, model.Attachment):
            url = self.attachment_url.format(item.zendesk_id)
        else:
            url = self.item_url.format(item.zendesk_group, item.zendesk_id)
        full_url = self._url_for(url)
        return self.raw_delete(full_url)

    def raw_delete(self, full_url):
        response = requests.delete(full_url,
                                   auth=(self.user, self.password),
                                   verify=False)
        return response.status_code == 200