Skip to content

Commit

Permalink
fix(api): fixed an issue where new scorers did not have a valid rule …
Browse files Browse the repository at this point in the history
…set (#257)
  • Loading branch information
lucianHymer authored May 31, 2023
1 parent 2b1b72c commit dd2c56d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 23 deletions.
41 changes: 21 additions & 20 deletions api/account/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,28 +287,29 @@ 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)

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 (
Expand All @@ -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,
Expand All @@ -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}
Expand Down
1 change: 1 addition & 0 deletions api/account/test/test_community.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down
9 changes: 6 additions & 3 deletions api/registry/api/v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
)

Expand Down
2 changes: 2 additions & 0 deletions api/scorer/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit dd2c56d

Please sign in to comment.