Skip to content

Commit ddd197e

Browse files
authored
Update discussion bulk user post count/delete from MongoDB to forum MySQL API (#38454)
* chore: remove mongodb usage
1 parent 891b473 commit ddd197e

4 files changed

Lines changed: 14 additions & 85 deletions

File tree

lms/djangoapps/discussion/rest_api/tasks.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from django.contrib.auth import get_user_model
88
from edx_django_utils.monitoring import set_code_owner_attribute
99
from eventtracking import tracker
10+
from forum import api as forum_api
1011
from opaque_keys.edx.locator import CourseKey
1112

1213
from common.djangoapps.student.roles import CourseInstructorRole, CourseStaffRole
@@ -99,8 +100,12 @@ def delete_course_post_for_user(user_id, username, course_ids, event_data=None):
99100
"""
100101
event_data = event_data or {}
101102
log.info(f"<<Bulk Delete>> Deleting all posts for {username} in course {course_ids}")
102-
threads_deleted = Thread.delete_user_threads(user_id, course_ids)
103-
comments_deleted = Comment.delete_user_comments(user_id, course_ids)
103+
threads_deleted = 0
104+
comments_deleted = 0
105+
for course_id in course_ids:
106+
result = forum_api.delete_user_posts(user_id=str(user_id), course_id=course_id)
107+
threads_deleted += result.get("thread_count", 0)
108+
comments_deleted += result.get("comment_count", 0)
104109
log.info(f"<<Bulk Delete>> Deleted {threads_deleted} posts and {comments_deleted} comments for {username} "
105110
f"in course {course_ids}")
106111
event_data.update({

lms/djangoapps/discussion/rest_api/views.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from drf_yasg import openapi
1212
from edx_rest_framework_extensions.auth.jwt.authentication import JwtAuthentication
1313
from edx_rest_framework_extensions.auth.session.authentication import SessionAuthenticationAllowInactiveUser
14+
from forum import api as forum_api
1415
from opaque_keys.edx.keys import CourseKey
1516
from rest_framework import permissions, status
1617
from rest_framework.authentication import SessionAuthentication
@@ -35,8 +36,6 @@
3536
from openedx.core.djangoapps.discussions.models import DiscussionsConfiguration, Provider
3637
from openedx.core.djangoapps.discussions.serializers import DiscussionSettingsSerializer
3738
from openedx.core.djangoapps.django_comment_common import comment_client
38-
from openedx.core.djangoapps.django_comment_common.comment_client.comment import Comment
39-
from openedx.core.djangoapps.django_comment_common.comment_client.thread import Thread
4039
from openedx.core.djangoapps.django_comment_common.models import CourseDiscussionSettings, Role
4140
from openedx.core.djangoapps.user_api.accounts.permissions import CanReplaceUsername, CanRetireUser
4241
from openedx.core.djangoapps.user_api.models import UserRetirementStatus
@@ -1571,7 +1570,6 @@ class BulkDeleteUserPosts(DeveloperErrorViewMixin, APIView):
15711570
def post(self, request, course_id):
15721571
"""
15731572
Implements the delete user posts endpoint.
1574-
TODO: Add support for MySQLBackend as well
15751573
"""
15761574
username = request.GET.get("username", None)
15771575
execute_task = request.GET.get("execute", "false").lower() == "true"
@@ -1595,8 +1593,12 @@ def post(self, request, course_id):
15951593
log.info(f"<<Bulk Delete>> {username} enrolled in {enrollments}")
15961594
log.info(f"<<Bulk Delete>> Posts for {username} in {course_ids} - for {course_or_org} {course_id}")
15971595

1598-
comment_count = Comment.get_user_comment_count(user.id, course_ids)
1599-
thread_count = Thread.get_user_threads_count(user.id, course_ids)
1596+
thread_count = 0
1597+
comment_count = 0
1598+
for cid in course_ids:
1599+
counts = forum_api.get_user_post_counts(user_id=str(user.id), course_id=cid)
1600+
thread_count += counts.get("thread_count", 0)
1601+
comment_count += counts.get("comment_count", 0)
16001602
log.info(f"<<Bulk Delete>> {username} in {course_ids} - Count thread {thread_count}, comment {comment_count}")
16011603

16021604
if execute_task:

openedx/core/djangoapps/django_comment_common/comment_client/comment.py

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
# pylint: disable=missing-docstring,protected-access
22
import logging
3-
import time
43

54
from bs4 import BeautifulSoup
65
from forum import api as forum_api
7-
from forum.backends.mongodb.comments import Comment as ForumComment
86

97
from openedx.core.djangoapps.django_comment_common.comment_client import models, settings
108

@@ -129,43 +127,6 @@ def retrieve_all(cls, params=None):
129127
per_page=params.get('per_page', 10),
130128
)
131129

132-
@classmethod
133-
def get_user_comment_count(cls, user_id, course_ids):
134-
"""
135-
Returns comments and responses count of user in the given course_ids.
136-
TODO: Add support for MySQL backend as well
137-
"""
138-
query_params = {
139-
"course_id": {"$in": course_ids},
140-
"author_id": str(user_id),
141-
"_type": "Comment"
142-
}
143-
return ForumComment()._collection.count_documents(query_params) # pylint: disable=protected-access
144-
145-
@classmethod
146-
def delete_user_comments(cls, user_id, course_ids):
147-
"""
148-
Deletes comments and responses of user in the given course_ids.
149-
TODO: Add support for MySQL backend as well
150-
"""
151-
start_time = time.time()
152-
query_params = {
153-
"course_id": {"$in": course_ids},
154-
"author_id": str(user_id),
155-
}
156-
comments_deleted = 0
157-
comments = ForumComment().get_list(**query_params)
158-
log.info(f"<<Bulk Delete>> Fetched comments for user {user_id} in {time.time() - start_time} seconds")
159-
for comment in comments:
160-
start_time = time.time()
161-
comment_id = comment.get("_id")
162-
course_id = comment.get("course_id")
163-
if comment_id:
164-
forum_api.delete_comment(comment_id, course_id=course_id)
165-
comments_deleted += 1
166-
log.info(f"<<Bulk Delete>> Deleted comment {comment_id} in {time.time() - start_time} seconds."
167-
f" Comment Found: {comment_id is not None}")
168-
return comments_deleted
169130

170131

171132
def _url_for_thread_comments(thread_id):

openedx/core/djangoapps/django_comment_common/comment_client/thread.py

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22

33

44
import logging
5-
import time
65

76
from eventtracking import tracker
87
from forum import api as forum_api
9-
from forum.backends.mongodb.threads import CommentThread as ForumThread
108

119
from . import models, settings, utils
1210

@@ -216,43 +214,6 @@ def un_pin(self, user, thread_id, course_id=None):
216214
)
217215
self._update_from_response(response)
218216

219-
@classmethod
220-
def get_user_threads_count(cls, user_id, course_ids):
221-
"""
222-
Returns threads count of user in the given course_ids.
223-
TODO: Add support for MySQL backend as well
224-
"""
225-
query_params = {
226-
"course_id": {"$in": course_ids},
227-
"author_id": str(user_id),
228-
"_type": "CommentThread"
229-
}
230-
return ForumThread()._collection.count_documents(query_params) # pylint: disable=protected-access
231-
232-
@classmethod
233-
def delete_user_threads(cls, user_id, course_ids):
234-
"""
235-
Deletes threads of user in the given course_ids.
236-
TODO: Add support for MySQL backend as well
237-
"""
238-
start_time = time.time()
239-
query_params = {
240-
"course_id": {"$in": course_ids},
241-
"author_id": str(user_id),
242-
}
243-
threads_deleted = 0
244-
threads = ForumThread().get_list(**query_params)
245-
log.info(f"<<Bulk Delete>> Fetched threads for user {user_id} in {time.time() - start_time} seconds")
246-
for thread in threads:
247-
start_time = time.time()
248-
thread_id = thread.get("_id")
249-
course_id = thread.get("course_id")
250-
if thread_id:
251-
forum_api.delete_thread(thread_id, course_id=course_id)
252-
threads_deleted += 1
253-
log.info(f"<<Bulk Delete>> Deleted thread {thread_id} in {time.time() - start_time} seconds."
254-
f" Thread Found: {thread_id is not None}")
255-
return threads_deleted
256217

257218

258219
def _clean_forum_params(params):

0 commit comments

Comments
 (0)