python, woocommerce: how to add categories with translations? - python

I need a python snippet to create categories with translation.
Python 3.8 with woocommerce package.
Wordpress with woocommerce plugin in version 7.3
WPML Multilingual CMS: 4.5.14
I have this snippet in python:
from woocommerce import API
# create an instance of the API class
wcapi = API(
url="FQDN",
consumer_key="yourconsumerkey",
consumer_secret="yourconsumersecret",
wp_api=True,
version="wc/v3"
)
# create a dictionary with the category data for the main language
category_data_en = {
"name": "Category Name in English",
"parent": 0,
"meta_data": [
{
"key": "_wpml_language",
"value": "en"
},
{
"key": "_wpml_translation_status",
"value": "0"
},
{
"key": "_wpml_element_type",
"value": "tax_category"
}
]
}
# create the category using the WooCommerce API for the main language
new_category_en = wcapi.post("products/categories", category_data_en).json()
# create a dictionary with the category data for the translation
category_data_pl = {
"name": "Category Name in Polish",
"meta_data": [
{
"key": "_wpml_language",
"value": "pl"
},
{
"key": "_wpml_translation_of",
"value": new_category_en.get("id")
},
{
"key": "_wpml_translation_status",
"value": "1"
},
{
"key": "_wpml_element_type",
"value": "tax_category"
}
]
}
# create the translation using the WooCommerce API
new_category_pl = wcapi.post("products/categories", category_data_pl).json()
and it creates two categories in the English language on my website. What am I doing wrong?
I can see that in my WPML plugin setting there is the info:
WooCommerce Multilingual Not installed
Is it necessary to install it to get those categories created properly?

Related

How to use Here Api to get Max speed limit of road using latitude and longitude in python environment

We are new to Here Api,
Our team is working on one transportation project in which we required to get max speed limit of road using vehicle (latitude ,longitude).
From last few days we are trying to figure out which api should we use in HereApi to achieve what we want.
Regarding this documentation of HERE Route Matching 8: https://developer.here.com/documentation/route-matching/api-reference.html
Send please POST request like:
https://routematching.hereapi.com/v8/match/routelinks?apikey=LXX1Axs75efnlFAlgbPxVekPDR0Hz6rTcRHQMT0EvQs&routeMatch=1&mode=fastest;car;traffic:disabled;&attributes=SPEED_LIMITS_FCn(*),LINK_ATTRIBUTE_FCn(*),TRAFFIC_PATTERN_FCn(*),TRUCK_SPEED_LIMITS_FCn(*),SPEED_LIMITS_VAR_FCn(*),SPEED_LIMITS_COND_FCn(*)
With in body:
LATITUDE,LONGITUDE
37.4201866,15.049515
See please attached the Postman collection for test of HERE Rote Match API v8 https://developer.here.com/documentation/route-matching/dev_guide/index.html :
{
"info": {
"_postman_id": "2563b7cc-2d62-4485-bb64-2f9561318aa2",
"name": "RMEspeed_limit",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"_exporter_id": "1051680"
},
"item": [
{
"name": "speed_limit",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "LATITUDE,LONGITUDE\r\n37.4201866,15.049515"
},
"url": {
"raw": "https://routematching.hereapi.com/v8/match/routelinks?apikey=LXX1Axs75efnlFAlgbPxVekPDR0Hz6rTcRHQMT0EvQs&routeMatch=1&mode=fastest;car;traffic:disabled;&attributes=SPEED_LIMITS_FCn(*),LINK_ATTRIBUTE_FCn(*),TRAFFIC_PATTERN_FCn(*),TRUCK_SPEED_LIMITS_FCn(*),SPEED_LIMITS_VAR_FCn(*),SPEED_LIMITS_COND_FCn(*) ",
"protocol": "https",
"host": [
"routematching",
"hereapi",
"com"
],
"path": [
"v8",
"match",
"routelinks"
],
"query": [
{
"key": "apikey",
"value": "LXX1Axs75efnlFAlgbPxVekPDR0Hz6rTcRHQMT0EvQs"
},
{
"key": "routeMatch",
"value": "1"
},
{
"key": "mode",
"value": "fastest;car;traffic:disabled;"
},
{
"key": "attributes",
"value": "SPEED_LIMITS_FCn(*),LINK_ATTRIBUTE_FCn(*),TRAFFIC_PATTERN_FCn(*),TRUCK_SPEED_LIMITS_FCn(*),SPEED_LIMITS_VAR_FCn(*),SPEED_LIMITS_COND_FCn(*) "
}
]
}
},
"response": []
}
]
}
An apikey and coordinate you specify your own for sure.
About all layers attributes you can see this example: https://demo.support.here.com/pde/maps?url_root=pde.api.here.com
In Python you should use normal POST request using some http libraries developed for Python:
https://www.geeksforgeeks.org/get-post-requests-using-python/

replace nested document array mongodb with python

i have this document in mongodb
{
"_id": {
"$oid": "62644af0368cb0a46d7c2a95"
},
"insertionData": "23/04/2022 19:50:50",
"ipfsMetadata": {
"Name": "data.json",
"Hash": "Qmb3FWgyJHzJA7WCBX1phgkV93GiEQ9UDWUYffDqUCbe7E",
"Size": "431"
},
"metadata": {
"sessionDate": "20220415 17:42:55",
"dataSender": "user345",
"data": {
"height": "180",
"weight": "80"
},
"addtionalInformation": [
{
"name": "poolsize",
"value": "30m"
},
{
"name": "swimStyle",
"value": "mariposa"
},
{
"name": "modality",
"value": "swim"
},
{
"name": "gender-title",
"value": "schoolA"
}
]
},
"fileId": {
"$numberLong": "4"
}
}
I want to update nested array document, for instance the name with gender-tittle. This have value schoolA and i want to change to adult like the body. I give the parameter number of fileId in the post request and in body i pass this
post request : localhost/sessionUpdate/4
and body:
{
"name": "gender-title",
"value": "adultos"
}
flask
#app.route('/sessionUpdate/<string:a>', methods=['PUT'])
def sessionUpdate(a):
datas=request.json
r=str(datas['name'])
r2=str(datas['value'])
print(r,r2)
r3=collection.update_one({'fileId':a, 'metadata.addtionalInformation':r}, {'$set':{'metadata.addtionalInformation.$.value':r2}})
return str(r3),200
i'm getting the 200 but the document don't update with the new value.
As you are using positional operator $ to work with your array, make sure your select query is targeting array element. You can see in below query that it is targeting metadata.addtionalInformation array with the condition that name: "gender-title"
db.collection.update({
"fileId": 4,
"metadata.addtionalInformation.name": "gender-title"
},
{
"$set": {
"metadata.addtionalInformation.$.value": "junior"
}
})
Here is the Mongo playground for your reference.

Python BOTO3 script is not returning name inside the tag

I need to extract the name, instance id, state of AWS EC2 and export it to csv. By using the below code I got the instance id and state. The name is inside the tags and I am having multiple key-value in my tags like below:
"Tags": [
{
"Value": "ggggg",
"Key": "bbbb"
},
{
"Value": "rrrrrr",
"Key": "eeeee"
},
{
"Value": "uyyyutu",
"Key": "hhhhhh"
},
{
"Value": "xxxxxxx",
"Key": "NAME"
},
{
"Value": "GREEN",
"Key": "STATE"
},
{
"Value": "xxxxx",
"Key": "yyyyy"
}
]
How can I get the name from particular KEY=NAME. While using the following code I am getting all values in the tags it is not giving particular value.
import boto3
import csv
client = boto3.client('ec2')
response = client.describe_instances(
Filters=[
{
'Name':'tag:STATE','Values':['GREEN']
}
]
)
detail=[]
for Reservations in response["Reservations"]:
for Instances in Reservations["Instances"]:
detail.append({
'ID':Instances['InstanceId'],
'Status':Instances['State']['Name'],
'Name':Instances['Tags']['Key'=='Name']['Value']
})
header=['ID','Status','Name']
with open('EC2_Detail.csv','w') as file:
writer=csv.DictWriter(file, fieldnames=header)
writer.writeheader()
writer.writerows(detail)
Try:
[x for x in Instances['Tags'] if x['Key'] == 'NAME'][0]['Value']
This will break, if the tag name isn't defined for a specific instance.
tag_names = [x for x in Instances['Tags'] if x['Key'] == 'NAME']
if len(tag_names) > 0: name = tag_names[0]

tag ec2 instance with stackname

I want to tag the ec2 instances with key- somekey and otherkey- with the value as stackname. is this going to do the trick with this python code??
import os, sys, pprint #standard library imports
import yaml, boto3 #pip library imports
import lib.aws as aws
import config.hooks as hooks
def generate(source_data):
return yaml.dump(generate_map(source_data), default_flow_style=False)
def generate_resource(ami, source_data):
resource = {
"Type": "AWS::EC2::Instance",
"Properties": {
"ImageId": ami["ImageId"],
"InstanceType": ami["InstanceType"],
"PrivateIpAddress": ami["PrivateIpAddress"],
"KeyName": ami["KeyName"],
"SubnetId": { "Ref": "SubnetId" },
"SecurityGroupIds": { "Ref": "SecurityGroupId" },
"Tags": [
{ "Key": "Name", "Value": ami["Name"] },
{ "Key": "BootUpDependsOn", "Value": ami["BootUpDependsOn"]},
{ "Key": "somekey", "Value": "Fn::Sub": "${AWS::StackName}},
{ "Key": "otherkey", "Value": "Fn::Sub": "${AWS::StackName}},
{ "Key": "WaitTimeAfterBootUp", "Value": ami["WaitTimeAfterBootUp"]}
]
}
}
CloudFormation automatically tags resources with the following tags:
aws:cloudformation:logical-id
aws:cloudformation:stack-id
aws:cloudformation:stack-name
Thus maybe instead of duplicating the tag with AWS::StackName you could use those automatically provided.
Update
There is quotation mark missing in:
{ "Key": "otherkey", "Value": "Fn::Sub": "${AWS::StackName}},
it should be:
{ "Key": "otherkey", "Value": "Fn::Sub": "${AWS::StackName}"},
it maybe also should be:
{ "Key": "otherkey", "Value": {"Fn::Sub": "${AWS::StackName}"}},

How to create json object for mongodb

I'm building json object which I want to insert to an already existing collection in mongoldb with data using pymongo. The data looks like this:
[
{
"title": "PyMongo",
"publication_date": "2015-09-07 10:00:00",
"tags": ["python", "mongodb", "nosql"],
"author": {
"name": "David",
"author_info": {
"$oid": "1870981708ddb1a352189e25w"
},
},
"date": {
"$date": 1484071215000 },
}
]
I noticed author has objectid and date is a timestamp, how can I create values for author_info and date and insert into mongodb?
I have a function that builds the values like this:
def build_json(title, pubctndate,taglist,author):
json_ob = \
[ {
"title": title,
"publication_date": pubctndate,
"tags": tablets,
"author": {
"name": author,
"author_info": {
"$oid": " "
},
},
"date": {"$date": },
}]
return json_ob
which I intend to call this way json.dumps(build_json(title, pubctndate,taglist,author))
Please bear with me, I'm a complete beginner.

Categories