kopia lustrzana https://gitlab.com/jaywink/federation
Add Post.tags property
Retrieves a set of unique tags found in the post.merge-requests/130/head
rodzic
94d8aad530
commit
8fe7347dec
|
@ -68,6 +68,11 @@ class Post(GUIDMixin, HandleMixin, PublicMixin, CreatedAtMixin, BaseEntity):
|
|||
super(Post, self).__init__(*args, **kwargs)
|
||||
self._required += ["raw_content"]
|
||||
|
||||
@property
|
||||
def tags(self):
|
||||
"""Returns a `set` of unique tags contained in `raw_content`."""
|
||||
return set({word.strip("#") for word in self.raw_content.split() if word.startswith("#")})
|
||||
|
||||
|
||||
class Image(GUIDMixin, HandleMixin, PublicMixin, CreatedAtMixin, BaseEntity):
|
||||
"""Reflects a single image, possibly linked to another object."""
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
from federation.tests.factories.entities import TaggedPostFactory
|
||||
|
||||
|
||||
class TestPostEntityTags(object):
|
||||
|
||||
def test_post_entity_returns_list_of_tags(self):
|
||||
post = TaggedPostFactory()
|
||||
assert post.tags == {"tagone", "tagtwo", "tagthree"}
|
|
@ -0,0 +1,32 @@
|
|||
from random import shuffle
|
||||
import factory
|
||||
from factory import fuzzy
|
||||
|
||||
from federation.entities.base import Post
|
||||
|
||||
|
||||
class GUIDMixinFactory(factory.Factory):
|
||||
guid = fuzzy.FuzzyText(length=32)
|
||||
|
||||
|
||||
class HandleMixinFactory(factory.Factory):
|
||||
handle = fuzzy.FuzzyText(length=8, suffix="@example.com")
|
||||
|
||||
|
||||
class PostFactory(GUIDMixinFactory, HandleMixinFactory, factory.Factory):
|
||||
class Meta:
|
||||
model = Post
|
||||
|
||||
raw_content = fuzzy.FuzzyText(length=300)
|
||||
|
||||
|
||||
class TaggedPostFactory(PostFactory):
|
||||
|
||||
@factory.lazy_attribute
|
||||
def raw_content(self):
|
||||
parts = []
|
||||
for tag in ["tagone", "tagtwo", "tagthree", "tagthree"]: # Yes, three is twice for fun
|
||||
parts.append(fuzzy.FuzzyText(length=50).fuzz())
|
||||
parts.append("#%s" % tag)
|
||||
shuffle(parts)
|
||||
return " ".join(parts)
|
3
setup.py
3
setup.py
|
@ -19,8 +19,9 @@ setup(
|
|||
"python-dateutil==2.4.2",
|
||||
"python-xrd==0.1",
|
||||
],
|
||||
test_require=[
|
||||
tests_require=[
|
||||
"pytest==2.7.2",
|
||||
"factory_boy==2.5.2",
|
||||
],
|
||||
include_package_data=True,
|
||||
)
|
||||
|
|
Ładowanie…
Reference in New Issue