Create menu

This commit is contained in:
Valentin CZERYBA 2023-07-24 22:43:52 +02:00
parent 72fbe0a364
commit 941776a7c1

View File

@ -257,7 +257,9 @@ class WPMenu:
idMenu = self._getIdFromReverse(menu[i]["title"], menu[i]["href"])
if idMenu["id"] != 0:
menu[i] = {"id":idMenu["id"], "type": idMenu["type"], "title":menu[i]["title"], "link":idMenu["link"], "children": menu[i]["children"]}
self._createMenu(menu)
exit(0)
navigation = ''
for i in menu:
self._logger.info("{0} Menu parent {1} with id {2} and type {3} and link {4}".format(self._name, i["title"], i["id"], i["type"], i["link"]))
@ -286,16 +288,19 @@ class WPMenu:
result = page.json()
self._logger.info("{0} : Get content navigation : {1}".format(self._name, len(result)))
for i in result:
self._logger.debug("{0} : Debug get item navigation : {1}".format(self._name, i))
if i["title"]["rendered"] == title:
exist = True
self._logger.info("{0} : {1} is exist".format(self._name, title))
if exist is False:
try:
self._logger.info("{0} Create navigation to wordpress for {1}".format(self._name, self._wordpress))
data = {"title": "Menu {0}".format(self._wordpress), "content": content}
data = {"title": title, "content": content}
page = self._request_wordpress.post("{1}://{0}/wp-json/wp/v2/navigation".format(self._wordpress, self._protocol_wordpress), auth=self._basic, headers=self._headers_json, data=json.dumps(data))
if page.status_code == 201:
self._logger.info("{0} : Navigation Menu {1} is created".format(self._name, self._wordpress))
self._logger.debug("{0} : Navigation Menu {1} create debug : ".format(self._name, page.content))
else:
self._logger.error("{0} : Post navigation didn't get due status code : {1}".format(self._name, page.status_code))
self._logger.debug("{0} : {1}".format(self._name, page.content))
@ -312,4 +317,52 @@ class WPMenu:
self._logger.error("{0} : Connection error for get url {1} : {2}".format(self._name, "{1}://{0}/wp-json/wp/v2/navigation".format(self._wordpress, self._protocol_wordpress), err))
exit(1)
except Exception as err:
self._logger.error("{0} : Exception error for get url {1} : {2}".format(self._name, "{1}://{0}/wp-json/wp/v2/navigation".format(self._wordpress, self._protocol_wordpress), err))
self._logger.error("{0} : Exception error for get url {1} : {2}".format(self._name, "{1}://{0}/wp-json/wp/v2/navigation".format(self._wordpress, self._protocol_wordpress), err))
def _createMenu(self, menu):
title = "Menu {0}".format(self._wordpress)
self._logger.info("{0} : Create menu from API Wordpress : {1}".format(self._name, title))
try:
params = {"search": title}
page = self._request_wordpress.get("{1}://{0}/wp-json/wp/v2/menus".format(self._wordpress, self._protocol_wordpress), auth=self._basic, params=params)
if page.status_code == 200:
result = page.json()
self._logger.info("{0} : Get content menus : {1}".format(self._name, len(result)))
idMenu = 0
if len(result) == 0:
self._logger.info("{0} : Create menu : {1}".format(self._name, title))
data = {"name": title}
try:
page = self._request_wordpress.post("{1}://{0}/wp-json/wp/v2/menus".format(self._wordpress, self._protocol_wordpress), auth=self._basic, headers=self._headers_json, data=json.dumps(data))
if page.status_code == 201:
result = page.json()
self._logger.debug("{0} : Get menus : {1}".format(self._name, result))
if len(result) > 0:
idMenu = result["id"]
else:
self._logger.error("{0} : Post menu didn't get due status code : {1}".format(self._name, page.status_code))
self._logger.debug("{0} : {1}".format(self._name, page.content))
except ConnectionError as err:
self._logger.error("{0} : Connection error for create menu {1} : {2}".format(self._name, "{1}://{0}/wp-json/wp/v2/menus".format(self._wordpress, self._protocol_wordpress), err))
exit(1)
except Exception as err:
self._logger.error("{0} : Exception error for get menu {1} : {2}".format(self._name, "{1}://{0}/wp-json/wp/v2/menus".format(self._wordpress, self._protocol_wordpress), err))
else:
self._logger.debug("{0} : Get menus : {1}".format(self._name, result))
for i in result:
self._logger.debug("{0} : List menus : {1}".format(self._name, i))
if i["name"] == title:
idMenu = i["id"]
self._logger.info("{0} : Get ID menus : {1}".format(self._name, idMenu))
else:
self._logger.error("{0} : Get menu didn't get due status code : {1}".format(self._name, page.status_code))
self._logger.debug("{0} : {1}".format(self._name, page.content))
except ConnectionError as err:
self._logger.error("{0} : Connection error for get menu {1} : {2}".format(self._name, "{1}://{0}/wp-json/wp/v2/menus".format(self._wordpress, self._protocol_wordpress), err))
exit(1)
except Exception as err:
self._logger.error("{0} : Exception error for get menu {1} : {2}".format(self._name, "{1}://{0}/wp-json/wp/v2/menus".format(self._wordpress, self._protocol_wordpress), err))