kopia lustrzana https://gitlab.com/jaywink/federation
Add BaseEntity.username property
Returns username from handle or None.merge-requests/132/head
rodzic
a72153e9b4
commit
837f463848
|
@ -1,6 +1,7 @@
|
||||||
import datetime
|
import datetime
|
||||||
import importlib
|
import importlib
|
||||||
import warnings
|
import warnings
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
from federation.entities.activitypub.enums import ActivityType
|
from federation.entities.activitypub.enums import ActivityType
|
||||||
|
|
||||||
|
@ -115,6 +116,14 @@ class BaseEntity:
|
||||||
"""Implement in subclasses if needed."""
|
"""Implement in subclasses if needed."""
|
||||||
pass
|
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):
|
class PublicMixin(BaseEntity):
|
||||||
public = False
|
public = False
|
||||||
|
|
|
@ -18,6 +18,16 @@ class TestPostEntityTags:
|
||||||
assert post.tags == set()
|
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:
|
class TestBaseEntityCallsValidateMethods:
|
||||||
def test_entity_calls_attribute_validate_method(self):
|
def test_entity_calls_attribute_validate_method(self):
|
||||||
post = PostFactory()
|
post = PostFactory()
|
||||||
|
|
Ładowanie…
Reference in New Issue