Skip to content

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

Apply changes after review

Maciej Korycinski41d91492e0c3
diff --git a/saleor/account/migrations/0032_bot.py b/saleor/account/migrations/0032_bot.py
index 611e1b8140..40b5823642 100644
--- a/saleor/account/migrations/0032_bot.py
+++ b/saleor/account/migrations/0032_bot.py
@@ -1,8 +1,11 @@
-# Generated by Django 2.2.4 on 2019-08-27 13:55
+# Generated by Django 2.2.4 on 2019-08-29 08:13
+import django.contrib.postgres.fields.jsonb
import oauthlib.common
from django.db import migrations, models
+import saleor.core.utils.json_serializer
+
class Migration(migrations.Migration):
@@ -24,7 +27,25 @@ class Migration(migrations.Migration):
verbose_name="ID",
),
),
- ("name", models.CharField(max_length=128)),
+ (
+ "private_meta",
+ django.contrib.postgres.fields.jsonb.JSONField(
+ blank=True,
+ default=dict,
+ encoder=saleor.core.utils.json_serializer.CustomJsonEncoder,
+ null=True,
+ ),
+ ),
+ (
+ "meta",
+ django.contrib.postgres.fields.jsonb.JSONField(
+ blank=True,
+ default=dict,
+ encoder=saleor.core.utils.json_serializer.CustomJsonEncoder,
+ null=True,
+ ),
+ ),
+ ("name", models.CharField(max_length=60)),
(
"auth_token",
models.CharField(
diff --git a/saleor/account/models.py b/saleor/account/models.py
index 4789c11b47..9e4b170521 100644
--- a/saleor/account/models.py
+++ b/saleor/account/models.py
@@ -1,4 +1,5 @@
import uuid
+from typing import Set
from django.conf import settings
from django.contrib.auth.models import (
@@ -191,8 +192,8 @@ class User(PermissionsMixin, ModelWithMetadata, AbstractBaseUser):
return self.email
-class Bot(models.Model):
- name = models.CharField(max_length=128)
+class Bot(ModelWithMetadata):
+ name = models.CharField(max_length=60)
auth_token = models.CharField(default=generate_token, unique=True, max_length=30)
created = models.DateTimeField(auto_now_add=True)
is_active = models.BooleanField(default=True)
@@ -210,7 +211,7 @@ class Bot(models.Model):
("manage_bots", pgettext_lazy("Permission description", "Manage bots")),
)
- def _get_permissions(self):
+ def _get_permissions(self) -> Set[str]:
"""Return the permissions of the bot."""
if not self.is_active:
return set()
@@ -218,12 +219,18 @@ class Bot(models.Model):
if not hasattr(self, perm_cache_name):
perms = self.permissions.all()
perms = perms.values_list("content_type__app_label", "codename").order_by()
- setattr(self, perm_cache_name, {"%s.%s" % (ct, name) for ct, name in perms})
+ setattr(self, perm_cache_name, {f"{ct}.{name}" for ct, name in perms})
return getattr(self, perm_cache_name)
def has_perms(self, perm_list):
"""Return True if the bot has each of the specified permissions."""
- return all(self.has_perm(perm) for perm in perm_list)
+ wanted_perms = set(perm_list)
+ actual_perms = self._get_permissions()
+
+ if not self.is_active:
+ return False
+
+ return (wanted_perms & actual_perms) == wanted_perms
def has_perm(self, perm):
"""Return True if the bot has the specified permission."""
diff --git a/saleor/graphql/account/filters.py b/saleor/graphql/account/filters.py
index d16e1d9e7f..4391db843b 100644
--- a/saleor/graphql/account/filters.py
+++ b/saleor/graphql/account/filters.py
@@ -70,7 +70,7 @@ def filter_search(qs, _, value):
def filter_bot_search(qs, _, value):
- search_fields = ("name", "is_active")
+ search_fields = ("name",)
if value:
qs = filter_by_query_param(qs, value, search_fields)
return qs
@@ -104,10 +104,11 @@ class CustomerFilter(django_filters.FilterSet):
class BotUserFilter(django_filters.FilterSet):
search = django_filters.CharFilter(method=filter_bot_search)
+ is_active = django_filters.BooleanFilter()
class Meta:
model = Bot
- fields = ["search"]
+ fields = ["search", "is_active"]
class StaffUserFilter(django_filters.FilterSet):
diff --git a/saleor/graphql/account/mutations/bot.py b/saleor/graphql/account/mutations/bot.py
index d876861a38..c81ed914a9 100644
--- a/saleor/graphql/account/mutations/bot.py
+++ b/saleor/graphql/account/mutations/bot.py
@@ -4,7 +4,12 @@ from saleor.core.permissions import get_permissions
from ....account import models
from ...core.enums import PermissionEnum
-from ...core.mutations import ModelDeleteMutation, ModelMutation
+from ...core.mutations import (
+ ClearMetaBaseMutation,
+ ModelDeleteMutation,
+ ModelMutation,
+ UpdateMetaBaseMutation,
+)
class BotInput(graphene.InputObjectType):
@@ -77,3 +82,19 @@ class BotDelete(ModelDeleteMutation):
description = "Deletes a bot"
model = models.Bot
permissions = ("account.manage_bots",)
+
+
+class BotUpdatePrivateMeta(UpdateMetaBaseMutation):
+ class Meta:
+ description = "Updates private metadata for bot."
+ permissions = ("account.manage_bots",)
+ model = models.Bot
+ public = False
+
+
+class BotClearStoredPrivateMeta(ClearMetaBaseMutation):
+ class Meta:
+ description = "Clear stored metadata value."
+ model = models.Bot
+ permissions = ("account.manage_bots",)
+ public = False
diff --git a/saleor/graphql/account/schema.py b/saleor/graphql/account/schema.py
index a6cd3d63fd..0ab5515541 100644
--- a/saleor/graphql/account/schema.py
+++ b/saleor/graphql/account/schema.py
@@ -25,7 +25,13 @@ from .mutations.base import (
UserClearStoredMeta,
UserUpdateMeta,
)
-from .mutations.bot import BotCreate, BotDelete, BotUpdate
+from .mutations.bot import (
+ BotClearStoredPrivateMeta,
+ BotCreate,
+ BotDelete,
+ BotUpdate,
+ BotUpdatePrivateMeta,
+)
from .mutations.deprecated_account import (
CustomerAddressCreate,
CustomerPasswordReset,
@@ -199,5 +205,8 @@ class AccountMutations(graphene.ObjectType):
bot_update = BotUpdate.Field()
bot_delete = BotDelete.Field()
+ bot_update_private_metadata = BotUpdatePrivateMeta.Field()
+ bot_clear_stored_private_metadata = BotClearStoredPrivateMeta.Field()
+
# Staff deprecated mutation
password_reset = PasswordReset.Field()
diff --git a/saleor/graphql/account/types.py b/saleor/graphql/account/types.py
index f794a9b4ac..4a39db7c50 100644
--- a/saleor/graphql/account/types.py
+++ b/saleor/graphql/account/types.py
@@ -154,7 +154,7 @@ class CustomerEvent(CountableDjangoObjectType):
return None
-class Bot(CountableDjangoObjectType):
+class Bot(MetadataObjectType, CountableDjangoObjectType):
permissions = graphene.List(
PermissionDisplay, description="List of bot's permissions."
)
@@ -180,6 +180,10 @@ class Bot(CountableDjangoObjectType):
def resolve_auth_token(root: models.Bot, _info, **_kwargs):
return "*" * 6 + root.auth_token[-4:]
+ @staticmethod
+ def resolve_meta(root, _info):
+ return resolve_meta(root, _info)
+
class User(MetadataObjectType, CountableDjangoObjectType):
addresses = gql_optimizer.field(
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 23ade96b79..86217a4d54 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -412,10 +412,17 @@ type Bot implements Node {
authToken: String!
created: DateTime
isActive: Boolean
+ privateMeta: [MetaStore]!
+ meta: [MetaStore]!
permissions: [PermissionDisplay]
name: String
}
+type BotClearStoredPrivateMeta {
+ errors: [Error!]
+ bot: Bot
+}
+
type BotCountableConnection {
pageInfo: PageInfo!
edges: [BotCountableEdge!]!
@@ -449,8 +456,14 @@ type BotUpdate {
bot: Bot
}
+type BotUpdatePrivateMeta {
+ errors: [Error!]
+ bot: Bot
+}
+
input BotUserInput {
search: String
+ isActive: Boolean
}
input CatalogueInput {
@@ -2032,6 +2045,8 @@ type Mutations {
botCreate(input: BotInput!): BotCreate
botUpdate(id: ID!, input: BotInput!): BotUpdate
botDelete(id: ID!): BotDelete
+ botUpdatePrivateMetadata(id: ID!, input: MetaInput!): BotUpdatePrivateMeta
+ botClearStoredPrivateMetadata(id: ID!, input: MetaPath!): BotClearStoredPrivateMeta
passwordReset(email: String!): PasswordReset
}
diff --git a/tests/api/test_account_bot.py b/tests/api/test_account_bot.py
index 8cbbebc1a0..116dd33cfc 100644
--- a/tests/api/test_account_bot.py
+++ b/tests/api/test_account_bot.py
@@ -157,7 +157,9 @@ def query_bots_with_filter():
return query
[email protected]("bot_filter, count", (({"search": "Sample"}, 1), ({}, 2)))
+ "bot_filter, count", (({"search": "Sample"}, 1), ({"isActive": False}, 1), ({}, 2))
+)
def test_bots_query(
query_bots_with_filter,
staff_api_client,
@@ -166,7 +168,9 @@ def test_bots_query(
bot_filter,
count,
):
- Bot.objects.create(name="Simple bot")
+ second_bot = Bot.objects.create(name="Simple bot")
+ second_bot.is_active = False
+ second_bot.save()
variables = {"filter": bot_filter}
response = staff_api_client.post_graphql(