From dd2c56d513a9bb62bac3da6c7c9c1afda154ac8a Mon Sep 17 00:00:00 2001 From: Lucian Date: Wed, 31 May 2023 12:52:58 -0700 Subject: [PATCH] fix(api): fixed an issue where new scorers did not have a valid rule set (#257) --- api/account/api.py | 41 +++++++++++++++--------------- api/account/test/test_community.py | 1 + api/registry/api/v1.py | 9 ++++--- api/scorer/settings/base.py | 2 ++ 4 files changed, 30 insertions(+), 23 deletions(-) diff --git a/api/account/api.py b/api/account/api.py index 71eed0e21..bd7cd4aaa 100644 --- a/api/account/api.py +++ b/api/account/api.py @@ -287,17 +287,18 @@ class CommunitiesPayload(Schema): name: str description: str use_case: str - rule = Rules.LIFO + rule: str = Rules.LIFO.value scorer: str def create_community_for_account( account, - payload, + name, + description, limit, - default_scorer, - use_case="Sybil Protection", - rule=Rules.LIFO, + scorer, + use_case, + rule, external_scorer_id=None, ): account_communities = Community.objects.filter(account=account, deleted_at=None) @@ -305,10 +306,10 @@ def create_community_for_account( if account_communities.count() >= limit: raise TooManyCommunitiesException() - if account_communities.filter(name=payload.name).exists(): + if account_communities.filter(name=name).exists(): raise CommunityExistsException() - if len(payload.name) == 0: + if len(name) == 0: raise CommunityHasNoNameException() if ( @@ -318,14 +319,18 @@ def create_community_for_account( raise CommunityExistsExceptionExternalId() # Create the scorer - scorer = default_scorer() + if scorer == "WEIGHTED_BINARY": + scorer = BinaryWeightedScorer(type="WEIGHTED_BINARY") + else: + scorer = WeightedScorer() + scorer.save() # Create the community community = Community.objects.create( account=account, - name=payload.name, - description=payload.description, + name=name, + description=description, use_case=use_case, rule=rule, scorer=scorer, @@ -345,18 +350,14 @@ def create_community(request, payload: CommunitiesPayload): if len(payload.description) == 0: raise CommunityHasNoDescriptionException() - scorer_class = ( - BinaryWeightedScorer - if payload.scorer == "WEIGHTED_BINARY" - else WeightedScorer - ) create_community_for_account( account, - payload, - 5, - scorer_class, - use_case=payload.use_case, - rule=payload.rule, + payload.name, + payload.description, + settings.USER_COMMUNITY_CREATION_LIMIT, + payload.scorer, + payload.use_case, + payload.rule, ) return {"ok": True} diff --git a/api/account/test/test_community.py b/api/account/test/test_community.py index dc2c91ed5..6578ceb52 100644 --- a/api/account/test/test_community.py +++ b/api/account/test/test_community.py @@ -68,6 +68,7 @@ def test_create_community(self): self.assertEqual(c.use_case, mock_community_body["use_case"]) self.assertEqual(c.name, mock_community_body["name"]) self.assertEqual(c.description, mock_community_body["description"]) + self.assertEqual(c.rule, "LIFO") def test_create_community_with_no_name(self): """Test that creation of a community with no name fails""" diff --git a/api/registry/api/v1.py b/api/registry/api/v1.py index 862c4e38c..3481b736e 100644 --- a/api/registry/api/v1.py +++ b/api/registry/api/v1.py @@ -6,7 +6,7 @@ from account.api import UnauthorizedException, create_community_for_account # --- Deduplication Modules -from account.models import Account, Community, Nonce, WeightedScorer +from account.models import Account, Community, Nonce, Rules from ceramic_cache.models import CeramicCache from django.conf import settings from django.core.cache import cache @@ -375,9 +375,12 @@ def create_generic_scorer(request, payload: GenericCommunityPayload): community = create_community_for_account( account, - payload, + payload.name, + payload.description, settings.GENERIC_COMMUNITY_CREATION_LIMIT, - WeightedScorer, + use_case="Sybil Protection", + rule=Rules.LIFO.value, + scorer="WEIGHTED", external_scorer_id=payload.external_scorer_id, ) diff --git a/api/scorer/settings/base.py b/api/scorer/settings/base.py index 991e0f22c..b0bdd9701 100644 --- a/api/scorer/settings/base.py +++ b/api/scorer/settings/base.py @@ -41,6 +41,8 @@ "GENERIC_COMMUNITY_CREATION_LIMIT", default=5 ) +USER_COMMUNITY_CREATION_LIMIT = env.int("USER_COMMUNITY_CREATION_LIMIT", default=5) + FF_API_ANALYTICS = env("FF_API_ANALYTICS", default="Off") LOGGING_STRATEGY = env( "LOGGING_STRATEGY", default="default"