Skip to content

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

Resolve public meta

Jakub Draganek971171a4f27f
diff --git a/saleor/account/migrations/0029_user_meta.py b/saleor/account/migrations/0029_user_meta.py
new file mode 100644
index 0000000000..ea3a8128e4
--- /dev/null
+++ b/saleor/account/migrations/0029_user_meta.py
@@ -0,0 +1,22 @@
+# Generated by Django 2.2.3 on 2019-07-16 08:17
+
+import django.contrib.postgres.fields.jsonb
+from django.db import migrations
+import saleor.core.utils.json_serializer
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [("account", "0028_user_private_meta")]
+
+ operations = [
+ migrations.AddField(
+ model_name="user",
+ name="meta",
+ field=django.contrib.postgres.fields.jsonb.JSONField(
+ blank=True,
+ default=dict,
+ encoder=saleor.core.utils.json_serializer.CustomJsonEncoder,
+ ),
+ )
+ ]
diff --git a/saleor/core/models.py b/saleor/core/models.py
index a018b1bf28..4f1d6e2d37 100644
--- a/saleor/core/models.py
+++ b/saleor/core/models.py
@@ -71,6 +71,7 @@ class PublishableModel(models.Model):
class ModelWithMetadata(models.Model):
private_meta = JSONField(blank=True, default=dict, encoder=CustomJsonEncoder)
+ meta = JSONField(blank=True, default=dict, encoder=CustomJsonEncoder)
class Meta:
abstract = True
@@ -83,5 +84,16 @@ class ModelWithMetadata(models.Model):
self.private_meta[namespace] = {}
self.private_meta[namespace][str(client)] = item
- def clear_stored_meta_for_client(self, namespace: str, client: str):
+ def clear_stored_private_meta_for_client(self, namespace: str, client: str):
self.private_meta.get(namespace, {}).pop(client, None)
+
+ def get_meta(self, namespace: str, client: str) -> dict:
+ return self.meta.get(namespace, {}).get(client, {})
+
+ def store_meta(self, namespace: str, client: str, item: dict):
+ if namespace not in self.private_meta:
+ self.meta[namespace] = {}
+ self.meta[namespace][str(client)] = item
+
+ def clear_stored_meta_for_client(self, namespace: str, client: str):
+ self.meta.get(namespace, {}).pop(client, None)
diff --git a/saleor/graphql/account/mutations.py b/saleor/graphql/account/mutations.py
index 3badcf12af..5317c5f2b6 100644
--- a/saleor/graphql/account/mutations.py
+++ b/saleor/graphql/account/mutations.py
@@ -843,7 +843,9 @@ class UserClearStoredMeta(BaseMutation):
)
cleared_value = stored_data.pop(metadata.key, None)
if not stored_data:
- user.clear_stored_meta_for_client(metadata.namespace, metadata.client_name)
+ user.clear_stored_private_meta_for_client(
+ metadata.namespace, metadata.client_name
+ )
user.save()
elif cleared_value is not None:
user.store_private_meta(
diff --git a/saleor/graphql/account/types.py b/saleor/graphql/account/types.py
index 9c4ee37e39..87f055cfb7 100644
--- a/saleor/graphql/account/types.py
+++ b/saleor/graphql/account/types.py
@@ -289,7 +289,13 @@ class User(CountableDjangoObjectType):
@staticmethod
def resolve_meta(root: models.User, _info):
- return []
+ return sorted(
+ [
+ {"namespace": namespace, "metadata": data}
+ for namespace, data in root.meta.items()
+ ],
+ key=itemgetter("namespace"),
+ )
class ChoiceValue(graphene.ObjectType):
diff --git a/tests/api/test_account_meta.py b/tests/api/test_account_meta.py
index 507bdc62e9..3299321867 100644
--- a/tests/api/test_account_meta.py
+++ b/tests/api/test_account_meta.py
@@ -5,19 +5,30 @@ import pytest
from tests.api.utils import get_graphql_content
-META_NAMESPACE = "TEST_NAMESPACE"
+PRIVATE_META_NAMESPACE = "TEST_NAMESPACE"
+PUBLIC_META_NAMESPACE = "PUBLIC_NAMESPACE"
META_CLIENT = "TEST_PLUGIN"
-KEY = "name"
-VALUE = "Bond"
+PRIVATE_KEY = "name"
+PRIVATE_VALUE = "Bond"
+
+PUBLIC_KEY = "purpose"
+PUBLIC_VALUE = "42"
@pytest.fixture
def customer_with_meta(customer_user):
customer_user.store_private_meta(
- namespace=META_NAMESPACE, client=META_CLIENT, item={KEY: VALUE}
+ namespace=PRIVATE_META_NAMESPACE,
+ client=META_CLIENT,
+ item={PRIVATE_KEY: PRIVATE_VALUE},
+ )
+ customer_user.store_meta(
+ namespace=PUBLIC_META_NAMESPACE,
+ client=META_CLIENT,
+ item={PUBLIC_KEY: PUBLIC_VALUE},
)
customer_user.save()
return customer_user
@@ -52,9 +63,12 @@ def test_get_private_meta(
)
meta = get_graphql_content(response)["data"]["user"]["privateMeta"][0]
- assert meta["namespace"] == META_NAMESPACE
+ assert meta["namespace"] == PRIVATE_META_NAMESPACE
assert meta["clients"] == [
- {"metadata": [{"key": KEY, "value": VALUE}], "name": META_CLIENT}
+ {
+ "metadata": [{"key": PRIVATE_KEY, "value": PRIVATE_VALUE}],
+ "name": META_CLIENT,
+ }
]
@@ -115,9 +129,9 @@ def test_update_metadata_through_mutation(
variables = {
"id": user_id,
"input": {
- "namespace": META_NAMESPACE,
+ "namespace": PRIVATE_META_NAMESPACE,
"clientName": META_CLIENT,
- "key": KEY,
+ "key": PRIVATE_KEY,
"value": NEW_VALUE,
},
}
@@ -128,9 +142,9 @@ def test_update_metadata_through_mutation(
"privateMeta"
][0]
- assert meta["namespace"] == META_NAMESPACE
+ assert meta["namespace"] == PRIVATE_META_NAMESPACE
assert meta["clients"] == [
- {"metadata": [{"key": KEY, "value": NEW_VALUE}], "name": META_CLIENT}
+ {"metadata": [{"key": PRIVATE_KEY, "value": NEW_VALUE}], "name": META_CLIENT}
]
@@ -143,7 +157,7 @@ def test_add_new_key_value_pair_to_metadata_using_mutation(
variables = {
"id": user_id,
"input": {
- "namespace": META_NAMESPACE,
+ "namespace": PRIVATE_META_NAMESPACE,
"clientName": META_CLIENT,
"key": NEW_KEY,
"value": NEW_VALUE,
@@ -158,9 +172,9 @@ def test_add_new_key_value_pair_to_metadata_using_mutation(
expected_metadata = [
{"key": NEW_KEY, "value": NEW_VALUE},
- {"key": KEY, "value": VALUE},
+ {"key": PRIVATE_KEY, "value": PRIVATE_VALUE},
]
- assert meta["namespace"] == META_NAMESPACE
+ assert meta["namespace"] == PRIVATE_META_NAMESPACE
assert meta["clients"] == [{"metadata": expected_metadata, "name": META_CLIENT}]
@@ -193,7 +207,11 @@ def test_clear_metadata_through_mutation(
user_id = graphene.Node.to_global_id("User", customer_with_meta.id)
variables = {
"id": user_id,
- "input": {"namespace": META_NAMESPACE, "clientName": META_CLIENT, "key": KEY},
+ "input": {
+ "namespace": PRIVATE_META_NAMESPACE,
+ "clientName": META_CLIENT,
+ "key": PRIVATE_KEY,
+ },
}
response = staff_api_client.post_graphql(
CLEAR_METADATA_MUTATION, variables, permissions=[permission_manage_users]
@@ -202,7 +220,7 @@ def test_clear_metadata_through_mutation(
"privateMeta"
][0]
- assert meta["namespace"] == META_NAMESPACE
+ assert meta["namespace"] == PRIVATE_META_NAMESPACE
assert meta["clients"] == []
@@ -213,7 +231,11 @@ def test_clear_silentyle_metadata_from_nonexistent_client(
WRONG_CLIENT = "WONG"
variables = {
"id": user_id,
- "input": {"namespace": META_NAMESPACE, "clientName": WRONG_CLIENT, "key": KEY},
+ "input": {
+ "namespace": PRIVATE_META_NAMESPACE,
+ "clientName": WRONG_CLIENT,
+ "key": PRIVATE_KEY,
+ },
}
response = staff_api_client.post_graphql(
CLEAR_METADATA_MUTATION, variables, permissions=[permission_manage_users]
@@ -222,9 +244,12 @@ def test_clear_silentyle_metadata_from_nonexistent_client(
"privateMeta"
][0]
- assert meta["namespace"] == META_NAMESPACE
+ assert meta["namespace"] == PRIVATE_META_NAMESPACE
assert meta["clients"] == [
- {"metadata": [{"key": KEY, "value": VALUE}], "name": META_CLIENT}
+ {
+ "metadata": [{"key": PRIVATE_KEY, "value": PRIVATE_VALUE}],
+ "name": META_CLIENT,
+ }
]
@@ -251,4 +276,9 @@ def test_access_users_public_metadata(user_api_client, customer_with_meta):
response = user_api_client.post_graphql(MY_PUBLIC_META_QUERY)
data = json.loads(response.content.decode("utf8"))
assert "errors" not in data
- assert data["data"]["me"]["meta"] == []
+
+ meta = get_graphql_content(response)["data"]["me"]["meta"][0]
+ assert meta["namespace"] == PUBLIC_META_NAMESPACE
+ assert meta["clients"] == [
+ {"metadata": [{"key": PUBLIC_KEY, "value": PUBLIC_VALUE}], "name": META_CLIENT}
+ ]