Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Rename metadata label to namespace

Jakub Draganek4d0caf83a140
diff --git a/saleor/core/models.py b/saleor/core/models.py
index ac0a3f7a9c..a018b1bf28 100644
--- a/saleor/core/models.py
+++ b/saleor/core/models.py
@@ -75,13 +75,13 @@ class ModelWithMetadata(models.Model):
class Meta:
abstract = True
- def get_private_meta(self, label: str, client: str) -> dict:
- return self.private_meta.get(label, {}).get(client, {})
+ def get_private_meta(self, namespace: str, client: str) -> dict:
+ return self.private_meta.get(namespace, {}).get(client, {})
- def store_private_meta(self, label: str, client: str, item: dict):
- if label not in self.private_meta:
- self.private_meta[label] = {}
- self.private_meta[label][str(client)] = item
+ def store_private_meta(self, namespace: str, client: str, item: dict):
+ if namespace not in self.private_meta:
+ self.private_meta[namespace] = {}
+ self.private_meta[namespace][str(client)] = item
- def clear_stored_meta_for_client(self, label: str, client: str):
- self.private_meta.get(label, {}).pop(client, None)
+ def clear_stored_meta_for_client(self, namespace: str, client: str):
+ self.private_meta.get(namespace, {}).pop(client, None)
diff --git a/saleor/graphql/account/mutations.py b/saleor/graphql/account/mutations.py
index 45ae028837..3badcf12af 100644
--- a/saleor/graphql/account/mutations.py
+++ b/saleor/graphql/account/mutations.py
@@ -807,10 +807,10 @@ class UserUpdatePrivateMeta(BaseMutation):
user = cls.get_node_or_error(info, user_id, field="user_id", only_type=User)
metadata = data.pop("input")
- stored_data = user.get_private_meta(metadata.store_label, metadata.client_name)
+ stored_data = user.get_private_meta(metadata.namespace, metadata.client_name)
stored_data[metadata.key] = metadata.value
user.store_private_meta(
- label=metadata.store_label, client=metadata.client_name, item=stored_data
+ namespace=metadata.namespace, client=metadata.client_name, item=stored_data
)
user.save()
@@ -839,17 +839,15 @@ class UserClearStoredMeta(BaseMutation):
metadata = data.pop("input")
stored_data = user.get_private_meta(
- label=metadata.store_label, client=metadata.client_name
+ namespace=metadata.namespace, client=metadata.client_name
)
cleared_value = stored_data.pop(metadata.key, None)
if not stored_data:
- user.clear_stored_meta_for_client(
- metadata.store_label, metadata.client_name
- )
+ user.clear_stored_meta_for_client(metadata.namespace, metadata.client_name)
user.save()
elif cleared_value is not None:
user.store_private_meta(
- label=metadata.store_label,
+ namespace=metadata.namespace,
client=metadata.client_name,
item=stored_data,
)
diff --git a/saleor/graphql/account/types.py b/saleor/graphql/account/types.py
index 988ada404c..30f75d3a9b 100644
--- a/saleor/graphql/account/types.py
+++ b/saleor/graphql/account/types.py
@@ -276,10 +276,10 @@ class User(CountableDjangoObjectType):
def resolve_private_meta(root: models.User, _info):
return sorted(
[
- {"label": label, "metadata": data}
- for label, data in root.private_meta.items()
+ {"namespace": namespace, "metadata": data}
+ for namespace, data in root.private_meta.items()
],
- key=itemgetter("label"),
+ key=itemgetter("namespace"),
)
diff --git a/saleor/graphql/account/types_meta.py b/saleor/graphql/account/types_meta.py
index a260db1d14..333103b848 100644
--- a/saleor/graphql/account/types_meta.py
+++ b/saleor/graphql/account/types_meta.py
@@ -23,7 +23,9 @@ class MetaClientStore(graphene.ObjectType):
class MetaStore(graphene.ObjectType):
- label = graphene.String(required=True, description="Name of metadata client group.")
+ namespace = graphene.String(
+ required=True, description="Name of metadata client group."
+ )
clients = graphene.List(
MetaClientStore,
required=True,
@@ -42,7 +44,7 @@ class MetaStore(graphene.ObjectType):
class MetaPath(graphene.InputObjectType):
- store_label = graphene.String(
+ namespace = graphene.String(
required=True, description="Name of metadata client group."
)
client_name = graphene.String(required=True, description="Metadata clients name.")
diff --git a/saleor/payment/utils.py b/saleor/payment/utils.py
index c1065a59bf..a8a89e6060 100644
--- a/saleor/payment/utils.py
+++ b/saleor/payment/utils.py
@@ -39,7 +39,7 @@ REQUIRED_GATEWAY_KEYS = {
"currency",
}
ALLOWED_GATEWAY_KINDS = {choices[0] for choices in TransactionKind.CHOICES}
-GATEWAYS_META_LABEL = "gateways"
+GATEWAYS_META_NAMESPACE = "gateways"
def list_enabled_gateways() -> List[str]:
@@ -513,22 +513,24 @@ def gateway_refund(payment, amount: Decimal = None) -> Transaction:
def fetch_customer_id(user, gateway):
"""Retrieves users customer_id stored for desired gateway"""
- key = prepare_label_name(gateway)
- gateway_config = user.get_private_meta(label=GATEWAYS_META_LABEL, client=key)
+ key = prepare_namespace_name(gateway)
+ gateway_config = user.get_private_meta(
+ namespace=GATEWAYS_META_NAMESPACE, client=key
+ )
return gateway_config.get("customer_id", None)
def store_customer_id(user, gateway, customer_id):
"""Stores customer_id in users private meta for desired gateway"""
user.store_private_meta(
- label=GATEWAYS_META_LABEL,
- client=prepare_label_name(gateway),
+ namespace=GATEWAYS_META_NAMESPACE,
+ client=prepare_namespace_name(gateway),
item={"customer_id": customer_id},
)
user.save(update_fields=["private_meta"])
-def prepare_label_name(s):
+def prepare_namespace_name(s):
return s.strip().upper()
diff --git a/tests/api/test_account_meta.py b/tests/api/test_account_meta.py
index cf839a9856..52342f5cc4 100644
--- a/tests/api/test_account_meta.py
+++ b/tests/api/test_account_meta.py
@@ -5,7 +5,7 @@ import pytest
from tests.api.utils import get_graphql_content
-META_LABEL = "TEST_LABEL"
+META_NAMESPACE = "TEST_NAMESPACE"
META_CLIENT = "TEST_PLUGIN"
KEY = "name"
@@ -15,7 +15,7 @@ VALUE = "Bond"
@pytest.fixture
def customer_with_meta(customer_user):
customer_user.store_private_meta(
- label=META_LABEL, client=META_CLIENT, item={KEY: VALUE}
+ namespace=META_NAMESPACE, client=META_CLIENT, item={KEY: VALUE}
)
customer_user.save()
return customer_user
@@ -26,7 +26,7 @@ GET_PRIVATE_META_QUERY = """
user(id: $id) {
email
privateMeta {
- label
+ namespace
clients {
name
metadata {
@@ -50,7 +50,7 @@ def test_get_private_meta(
)
meta = get_graphql_content(response)["data"]["user"]["privateMeta"][0]
- assert meta["label"] == META_LABEL
+ assert meta["namespace"] == META_NAMESPACE
assert meta["clients"] == [
{"metadata": [{"key": KEY, "value": VALUE}], "name": META_CLIENT}
]
@@ -61,7 +61,7 @@ MY_PRIVATE_META_QUERY = """
me {
email
privateMeta {
- label
+ namespace
clients {
name
metadata {
@@ -90,7 +90,7 @@ UPDATE_METADATA_MUTATION = """
) {
user {
privateMeta {
- label
+ namespace
clients {
name
metadata {
@@ -113,7 +113,7 @@ def test_update_metadata_through_mutation(
variables = {
"id": user_id,
"input": {
- "storeLabel": META_LABEL,
+ "namespace": META_NAMESPACE,
"clientName": META_CLIENT,
"key": KEY,
"value": NEW_VALUE,
@@ -126,7 +126,7 @@ def test_update_metadata_through_mutation(
"privateMeta"
][0]
- assert meta["label"] == META_LABEL
+ assert meta["namespace"] == META_NAMESPACE
assert meta["clients"] == [
{"metadata": [{"key": KEY, "value": NEW_VALUE}], "name": META_CLIENT}
]
@@ -141,7 +141,7 @@ def test_add_new_key_value_pair_to_metadata_using_mutation(
variables = {
"id": user_id,
"input": {
- "storeLabel": META_LABEL,
+ "namespace": META_NAMESPACE,
"clientName": META_CLIENT,
"key": NEW_KEY,
"value": NEW_VALUE,
@@ -158,7 +158,7 @@ def test_add_new_key_value_pair_to_metadata_using_mutation(
{"key": NEW_KEY, "value": NEW_VALUE},
{"key": KEY, "value": VALUE},
]
- assert meta["label"] == META_LABEL
+ assert meta["namespace"] == META_NAMESPACE
assert meta["clients"] == [{"metadata": expected_metadata, "name": META_CLIENT}]
@@ -170,7 +170,7 @@ CLEAR_METADATA_MUTATION = """
) {
user {
privateMeta {
- label
+ namespace
clients {
name
metadata {
@@ -191,7 +191,7 @@ def test_clear_metadata_through_mutation(
user_id = graphene.Node.to_global_id("User", customer_with_meta.id)
variables = {
"id": user_id,
- "input": {"storeLabel": META_LABEL, "clientName": META_CLIENT, "key": KEY},
+ "input": {"namespace": META_NAMESPACE, "clientName": META_CLIENT, "key": KEY},
}
response = staff_api_client.post_graphql(
CLEAR_METADATA_MUTATION, variables, permissions=[permission_manage_users]
@@ -200,7 +200,7 @@ def test_clear_metadata_through_mutation(
"privateMeta"
][0]
- assert meta["label"] == META_LABEL
+ assert meta["namespace"] == META_NAMESPACE
assert meta["clients"] == []
@@ -211,7 +211,7 @@ def test_clear_silentyle_metadata_from_nonexistent_client(
WRONG_CLIENT = "WONG"
variables = {
"id": user_id,
- "input": {"storeLabel": META_LABEL, "clientName": WRONG_CLIENT, "key": KEY},
+ "input": {"namespace": META_NAMESPACE, "clientName": WRONG_CLIENT, "key": KEY},
}
response = staff_api_client.post_graphql(
CLEAR_METADATA_MUTATION, variables, permissions=[permission_manage_users]
@@ -220,7 +220,7 @@ def test_clear_silentyle_metadata_from_nonexistent_client(
"privateMeta"
][0]
- assert meta["label"] == META_LABEL
+ assert meta["namespace"] == META_NAMESPACE
assert meta["clients"] == [
{"metadata": [{"key": KEY, "value": VALUE}], "name": META_CLIENT}
]