add private method for association id

This commit is contained in:
Valentin CZERYBA 2023-04-02 13:06:10 +02:00
parent 4054f41e9b
commit f07f8c040f

View File

@ -15,6 +15,13 @@ class WPimport:
with open(file, 'r') as f: with open(file, 'r') as f:
contents = f.read() contents = f.read()
self.insertWordpress(contents) self.insertWordpress(contents)
def _linkImgPost(self, title, list_img, post_id):
for i in list_img:
data = {"post": post_id}
r = requests.post("http://{0}/wp-json/wp/v2/media/{1}".format(self.wordpress, i["id"]), auth=self.basic, data=data)
if r.status_code == 200:
print("Association d'une image à l'article {0}".format(title))
def insertWordpress(self, content): def insertWordpress(self, content):
tags = [] tags = []
@ -156,6 +163,7 @@ class WPimport:
if page.status_code == 200: if page.status_code == 200:
result = page.json() result = page.json()
print("Article mis à jour : {0}".format(result["title"]["raw"])) print("Article mis à jour : {0}".format(result["title"]["raw"]))
self._linkImgPost(result["title"]["raw"], list_img, result["id"])
@ -169,12 +177,7 @@ class WPimport:
page = requests.post("http://{0}/wp-json/wp/v2/comments".format(self.wordpress), auth=self.basic, data=data) page = requests.post("http://{0}/wp-json/wp/v2/comments".format(self.wordpress), auth=self.basic, data=data)
if page.status_code == 201: if page.status_code == 201:
print("Commentaire ajoute pour {0}".format(result["title"]["raw"])) print("Commentaire ajoute pour {0}".format(result["title"]["raw"]))
for i in list_img: self._linkImgPost(result["title"]["raw"], list_img, result["id"])
data = {"post": result["id"]}
r = requests.post("http://{0}/wp-json/wp/v2/media/{1}".format(self.wordpress, i["id"]), auth=self.basic, data=data)
if r.status_code == 200:
print("Association d'une image à l'article {0}".format(result["title"]["raw"]))