-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrapi-fillInFromLocale.py
71 lines (59 loc) · 2.54 KB
/
strapi-fillInFromLocale.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import requests
import json
import sys, getopt
import os
from dotenv import load_dotenv
load_dotenv()
urlBase = os.getenv("STRAPI_URL_BASE")
token = os.getenv("STRAPI_API_KEY")
# ---------------
# --- duplicate
# ---------------
def fillInFromSource(contentType, id, source, target):
url = urlBase +"/"+contentType+"s/"+id
print(url)
parameters = {"locale" : source, "populate" : "*"}
headers = {"Authorization": "Bearer "+token}
response = requests.get(url,params=parameters, headers=headers)
data = response.json()
print(json.dumps(data,indent=4))
# --- manipulate the data
data["data"]["attributes"]["locale"]=target
del data["data"]["attributes"]["createdAt"]
del data["data"]["attributes"]["updatedAt"]
del data["data"]["attributes"]["publishedAt"]
# --- create the same data for new locale
url = urlBase +"/"+contentType+"s/"+id+"/localizations"
response = requests.post(url,headers=headers,json=data["data"]["attributes"])
print(json.dumps(response.json(),indent=4))
############
# --- MAIN
############
def main(argv):
opts, args = getopt.getopt(argv,"hc:i:s:t:",["contentType=","contentTypeId=","localeSource=","localeTarget="])
for opt, arg in opts:
if opt == '-h':
print ('''
strapi-fillInFromLocale is a Python script for Strapi (https://strapi.io/) to fill in the content of the Target locale with the content of the Source locale
for a Content Type with the ID you are providing
python strapi-fillInFromLocale.py --contentType (or -c) <contentType> --contentTypeId (or -i) <id> --localeSource (or -s) <locale> --localeTarget (or -t) <locale>
ex1: python strapi-fillInFromLocale.py --contentType article --contentTypeId 1 --localeSource fr --localeTarget en
ex2: python strapi-fillInFromLocale.py -c article -i 1 -s fr -t en ''')
sys.exit()
elif opt in ("-c", "--contentType"):
contentType = arg
elif opt in ("-i", "--contentTypeId"):
id = arg
elif opt in ("-s", "--localeSource"):
source = arg
elif opt in ("-t", "--localeTarget"):
target = arg
else:
print("Error: unknown option "+opt)
sys.exit(1)
fillInFromSource(contentType, id, source, target)
###############
# --- bootstrap to run as a script
###############
if __name__ == "__main__":
main(sys.argv[1:])