Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Commit

Permalink
Revert "Revert "Merge pull request #692 from OkunaOrg/feature/257-tre…
Browse files Browse the repository at this point in the history
…nding-communities-n-posts-rehaul""

This reverts commit 05ae2a4.
  • Loading branch information
uiboy committed May 15, 2020
1 parent 51bd225 commit f067e4c
Show file tree
Hide file tree
Showing 29 changed files with 1,119 additions and 167 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@ The change log for the API server for Okuna.

## Table of contents

- [Release 0.0.66](#release-0.0.66)
- [Release 0.0.63](#release-0.0.63)
- [Release 0.0.59](#release-0.0.59)


## Release 0.0.66

- Introduce activity score in posts and communities, add jobs for the same
- Refactor trending posts and trending communities to use activity score


## Release 0.0.63

- Improve performance for linked users API
Expand Down
4 changes: 2 additions & 2 deletions openbook/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
from openbook_posts.views.post_media.views import PostMedia
from openbook_posts.views.post_reaction.views import PostReactionItem
from openbook_posts.views.post_reactions.views import PostReactions, PostReactionsEmojiCount, PostReactionEmojiGroups
from openbook_posts.views.posts.views import Posts, TrendingPosts, TopPosts, TrendingPostsNew, \
from openbook_posts.views.posts.views import Posts, TrendingPosts, TopPosts, TrendingPostsLegacy, \
ProfilePostsExcludedCommunities, SearchProfilePostsExcludedCommunities, TopPostsExcludedCommunities, \
SearchTopPostsExcludedCommunities, ProfilePostsExcludedCommunity, TopPostsExcludedCommunity
from openbook_importer.views import ImportItem
Expand Down Expand Up @@ -233,7 +233,7 @@
path('<uuid:post_uuid>/', include(post_patterns)),
path('', Posts.as_view(), name='posts'),
path('trending/', TrendingPosts.as_view(), name='trending-posts'),
path('trending/new/', TrendingPostsNew.as_view(), name='trending-posts-new'),
path('trending/new/', TrendingPostsLegacy.as_view(), name='trending-posts-new'),
path('emojis/groups/', PostReactionEmojiGroups.as_view(), name='posts-emoji-groups'),
path('profile/', include(posts_profile_patterns)),
path('top/', include(posts_top_patterns)),
Expand Down
11 changes: 8 additions & 3 deletions openbook_auth/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1991,13 +1991,18 @@ def get_trending_posts(self, max_id=None, min_id=None):
Post = get_post_model()
return Post.get_trending_posts_for_user_with_id(user_id=self.pk, max_id=max_id, min_id=min_id)

def get_trending_posts_old(self):
def get_trending_posts_legacy(self):
Post = get_post_model()
return Post.get_trending_posts_old_for_user_with_id(user_id=self.pk)
return Post.get_trending_posts_for_user_with_id_legacy(user_id=self.pk)

def get_trending_communities(self, category_name=None):
Community = get_community_model()
return Community.get_trending_communities_for_user_with_id(user_id=self.pk, category_name=category_name)
return Community.get_trending_communities_for_user_with_id(user_id=self.pk,
category_name=category_name)

def get_trending_communities_by_members(self, category_name=None):
Community = get_community_model()
return Community.get_trending_communities_by_members_for_user_with_id(user_id=self.pk, category_name=category_name)

def search_communities_with_query(self, query, excluded_from_profile_posts):
Community = get_community_model()
Expand Down
2 changes: 0 additions & 2 deletions openbook_auth/tests/views/test_authenticated_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -846,8 +846,6 @@ def test_cannot_set_invalid_language(self):
'language_id': 99999
}, **headers)

print(response)

self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
user.refresh_from_db()
self.assertTrue(user.language.id, language.id)
Expand Down
11 changes: 10 additions & 1 deletion openbook_common/tests/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from unittest.mock import patch

from rest_framework.test import APITestCase
from rest_framework.test import APITestCase, APITransactionTestCase


class OpenbookAPITestCase(APITestCase):
Expand All @@ -10,3 +10,12 @@ def setUp(self):

def tearDown(self):
self.patcher.stop()


class OpenbookAPITransactionTestCase(APITransactionTestCase):
def setUp(self):
self.patcher = patch('openbook_notifications.helpers._send_notification_to_user')
self.mock_foo = self.patcher.start()

def tearDown(self):
self.patcher.stop()
22 changes: 22 additions & 0 deletions openbook_communities/migrations/0034_trendingcommunity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 2.2.5 on 2020-02-03 14:10

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('openbook_communities', '0033_auto_20191209_1337'),
]

operations = [
migrations.CreateModel(
name='TrendingCommunity',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created', models.DateTimeField(db_index=True, editable=False)),
('community', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='trending_community', to='openbook_communities.Community')),
],
),
]
18 changes: 18 additions & 0 deletions openbook_communities/migrations/0035_community_activity_score.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.2.5 on 2020-02-10 12:50

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('openbook_communities', '0034_trendingcommunity'),
]

operations = [
migrations.AddField(
model_name='community',
name='activity_score',
field=models.FloatField(default=0.0),
),
]
16 changes: 16 additions & 0 deletions openbook_communities/migrations/0036_delete_trendingcommunity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Generated by Django 2.2.5 on 2020-02-22 11:49

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('openbook_communities', '0035_community_activity_score'),
]

operations = [
migrations.DeleteModel(
name='TrendingCommunity',
),
]
17 changes: 17 additions & 0 deletions openbook_communities/migrations/0037_auto_20200222_1344.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 2.2.5 on 2020-02-22 12:44

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('openbook_communities', '0036_delete_trendingcommunity'),
]

operations = [
migrations.AddIndex(
model_name='community',
index=models.Index(fields=['activity_score'], name='openbook_co_activit_07d4ba_idx'),
),
]
18 changes: 18 additions & 0 deletions openbook_communities/migrations/0038_auto_20200224_1615.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.2.5 on 2020-02-24 15:15

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('openbook_communities', '0037_auto_20200222_1344'),
]

operations = [
migrations.AlterField(
model_name='community',
name='activity_score',
field=models.DecimalField(decimal_places=10, default=0.0, max_digits=10),
),
]
39 changes: 37 additions & 2 deletions openbook_communities/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,13 @@ class Community(models.Model):
_('is deleted'),
default=False,
)
activity_score = models.DecimalField(default=0.0, decimal_places=10, max_digits=10)

class Meta:
verbose_name_plural = 'communities'
indexes = [
models.Index(fields=['activity_score']),
]

@classmethod
def is_user_with_username_invited_to_community_with_name(cls, username, community_name):
Expand Down Expand Up @@ -147,7 +151,12 @@ def get_new_user_suggested_communities(cls):
def get_trending_communities_for_user_with_id(cls, user_id, category_name=None):
trending_communities_query = cls._make_trending_communities_query(category_name=category_name)
trending_communities_query.add(~Q(banned_users__id=user_id), Q.AND)
return cls._get_trending_communities_with_query(query=trending_communities_query)

trending_communities = cls._get_trending_communities_with_query(query=trending_communities_query)
if trending_communities.count() == 0:
return cls.get_trending_communities_by_members_for_user_with_id(user_id, category_name=category_name)

return trending_communities

@classmethod
def get_trending_communities(cls, category_name=None):
Expand All @@ -156,11 +165,37 @@ def get_trending_communities(cls, category_name=None):

@classmethod
def _get_trending_communities_with_query(cls, query):
return cls.objects.filter(query).order_by('-activity_score')

@classmethod
def _make_trending_communities_query(cls, category_name=None):
trending_communities_query = Q(type=Community.COMMUNITY_TYPE_PUBLIC, is_deleted=False)
trending_communities_query.add(Q(activity_score__gte=settings.MIN_ACTIVITY_SCORE_FOR_COMMUNITY_TRENDING), Q.AND)
trending_communities_query.add(~Q(moderated_object__status=ModeratedObject.STATUS_APPROVED), Q.AND)

if category_name:
trending_communities_query.add(Q(categories__name=category_name), Q.AND)

return trending_communities_query

@classmethod
def get_trending_communities_by_members_for_user_with_id(cls, user_id, category_name=None):
trending_communities_query = cls._make_trending_communities_by_members_query(category_name=category_name)
trending_communities_query.add(~Q(banned_users__id=user_id), Q.AND)
return cls._get_trending_communities_by_members_with_query(query=trending_communities_query)

@classmethod
def get_trending_communities_by_members(cls, category_name=None):
trending_communities_query = cls._make_trending_communities_by_members_query(category_name=category_name)
return cls._get_trending_communities_by_members_with_query(query=trending_communities_query)

@classmethod
def _get_trending_communities_by_members_with_query(cls, query):
return cls.objects.annotate(Count('memberships')).filter(query).order_by(
'-memberships__count', '-created')

@classmethod
def _make_trending_communities_query(cls, category_name=None):
def _make_trending_communities_by_members_query(cls, category_name=None):
trending_communities_query = Q(type=cls.COMMUNITY_TYPE_PUBLIC, is_deleted=False)

if category_name:
Expand Down
Loading

0 comments on commit f067e4c

Please sign in to comment.