Skip to content

Commit

Permalink
move unconfusable_string to liberapay/utils/unconfusable.py
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinDelille committed Mar 21, 2019
1 parent 4883243 commit 9b496bc
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 22 deletions.
16 changes: 3 additions & 13 deletions liberapay/models/community.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

from postgres.orm import Model
from psycopg2 import IntegrityError
from confusable_homoglyphs import confusables

from liberapay.exceptions import CommunityAlreadyExists, InvalidCommunityName
from liberapay.utils.unconfusable import unconfusable_string


name_maxlength = 40
Expand Down Expand Up @@ -42,12 +42,13 @@ def create(cls, name, creator_id, lang='mul'):

try:
with cls.db.get_cursor() as cursor:
unconfusable_name = unconfusable_string(name)
all_names = cursor.all("""
SELECT name
FROM communities
""")
for existing_name in all_names:
if cls._unconfusable(name) == cls._unconfusable(existing_name):
if unconfusable_name == unconfusable_string(existing_name):
raise CommunityAlreadyExists

p_id = cursor.one("""
Expand Down Expand Up @@ -101,14 +102,3 @@ def check_membership_status(self, participant):
@property
def nsubscribers(self):
return self.participant.nsubscribers

@staticmethod
def _unconfusable(name):
unconfusable_name = ''
for c in name:
confusable = confusables.is_confusable(c, preferred_aliases=['COMMON', 'LATIN'])
if confusable:
# if the character is confusable we replace it with the first prefered alias
c = confusable[0]['homoglyphs'][0]['c']
unconfusable_name += c
return unconfusable_name
11 changes: 11 additions & 0 deletions liberapay/utils/unconfusable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from confusable_homoglyphs import confusables

def unconfusable_string(name):
unconfusable_name = ''
for c in name:
confusable = confusables.is_confusable(c, preferred_aliases=['COMMON', 'LATIN'])
if confusable:
# if the character is confusable we replace it with the first prefered alias
c = confusable[0]['homoglyphs'][0]['c']
unconfusable_name += c
return unconfusable_name
8 changes: 0 additions & 8 deletions tests/py/test_communities.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,6 @@ def test_create_community_already_taken_is_case_insensitive(self):
with self.assertRaises(CommunityAlreadyExists):
Community.create('TeSt', self.alice.id)

def test_unconfusable(self):
self.assertEqual('user2', Community._unconfusable('user2'))
self.assertEqual('alice', Community._unconfusable('alice'))
latin_string = 'AlaskaJazz'
mixed_string = 'ΑlaskaJazz'
self.assertNotEqual(latin_string, mixed_string)
self.assertEqual(latin_string, Community._unconfusable(mixed_string))

def test_create_community_already_taken_with_confusable_homoglyphs(self):
latin_string = 'AlaskaJazz'
mixed_string = 'ΑlaskaJazz'
Expand Down
13 changes: 12 additions & 1 deletion tests/py/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from liberapay import utils
from liberapay.i18n.currencies import Money, MoneyBasket
from liberapay.testing import Harness
from liberapay.utils import markdown, b64encode_s, b64decode_s, cbor
from liberapay.utils import markdown, b64encode_s, b64decode_s, cbor, unconfusable
from liberapay.wireup import CSP


Expand Down Expand Up @@ -244,3 +244,14 @@ def test_csp_handles_valueless_directives_correctly(self):
csp2 = CSP(csp)
assert csp == csp2
assert csp2.directives[b'upgrade-insecure-requests'] == b''

# Unconfusable
# ============

def test_unconfusable_string(self):
self.assertEqual('user2', unconfusable.unconfusable_string('user2'))
self.assertEqual('alice', unconfusable.unconfusable_string('alice'))
latin_string = 'AlaskaJazz'
mixed_string = 'ΑlaskaJazz'
self.assertNotEqual(latin_string, mixed_string)
self.assertEqual(latin_string, unconfusable.unconfusable_string(mixed_string))

0 comments on commit 9b496bc

Please sign in to comment.