Add BaseEntity.username property

Returns username from handle or None.
merge-requests/132/head
Jason Robinson 2018-09-26 23:27:47 +03:00
rodzic a72153e9b4
commit 837f463848
2 zmienionych plików z 19 dodań i 0 usunięć

Wyświetl plik

@ -1,6 +1,7 @@
import datetime
import importlib
import warnings
from typing import Optional
from federation.entities.activitypub.enums import ActivityType
@ -115,6 +116,14 @@ class BaseEntity:
"""Implement in subclasses if needed."""
pass
@property
def username(self) -> Optional[str]:
if self.handle:
username_part = self.handle.rsplit('@', 1)
if username_part:
# Strip any remaining '@' if this is a Mastodon style handle
return username_part[0].strip('@')
class PublicMixin(BaseEntity):
public = False

Wyświetl plik

@ -18,6 +18,16 @@ class TestPostEntityTags:
assert post.tags == set()
class TestBaseEntity:
def test_username(self):
entity = Profile(handle='foobar@localhost.local')
assert entity.username == 'foobar'
entity = Profile(handle='@foobar@localhost.local')
assert entity.username == 'foobar'
entity = Profile()
assert entity.username is None
class TestBaseEntityCallsValidateMethods:
def test_entity_calls_attribute_validate_method(self):
post = PostFactory()