Skip to content

Commit

Permalink
feat: call python methods from forum v2
Browse files Browse the repository at this point in the history
- directly call python native APIs from forum v2 for pin, unpin thread,
commentables count_stats and get user's data by user_id
- add forum to the edx-platform requirements
  • Loading branch information
Muhammad Faraz Maqsood authored and Muhammad Faraz Maqsood committed Sep 16, 2024
1 parent 6a63cfc commit 9126017
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from edx_django_utils.monitoring import function_trace
from opaque_keys.edx.keys import CourseKey

from forum import api as forum_api
from openedx.core.djangoapps.django_comment_common.comment_client import settings
from openedx.core.djangoapps.django_comment_common.comment_client.utils import perform_request

Expand All @@ -29,17 +30,8 @@ def get_course_commentable_counts(course_key: CourseKey) -> Dict[str, Dict[str,
}
"""
url = f"{settings.PREFIX}/commentables/{course_key}/counts"
response = perform_request(
'get',
url,
metric_tags=[
f"course_key:{course_key}",
"function:get_course_commentable_counts",
],
metric_action='commentable_stats.retrieve',
)
return response
commentable_stats = forum_api.retrieve_commentables_stats(str(course_key))
return commentable_stats


@function_trace("get_course_user_stats")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from eventtracking import tracker

from . import models, settings, utils
from forum import api as forum_api

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -193,27 +194,11 @@ def unFlagAbuse(self, user, voteable, removeAll):
voteable._update_from_response(response)

def pin(self, user, thread_id):
url = _url_for_pin_thread(thread_id)
params = {'user_id': user.id}
response = utils.perform_request(
'put',
url,
params,
metric_tags=self._metric_tags,
metric_action='thread.pin'
)
self._update_from_response(response)
thread_data = forum_api.pin_thread(user.id, thread_id)
self._update_from_response(thread_data)

def un_pin(self, user, thread_id):
url = _url_for_un_pin_thread(thread_id)
params = {'user_id': user.id}
response = utils.perform_request(
'put',
url,
params,
metric_tags=self._metric_tags,
metric_action='thread.unpin'
)
response = forum_api.unpin_thread(user.id, thread_id)
self._update_from_response(response)


Expand All @@ -223,11 +208,3 @@ def _url_for_flag_abuse_thread(thread_id):

def _url_for_unflag_abuse_thread(thread_id):
return f"{settings.PREFIX}/threads/{thread_id}/abuse_unflag"


def _url_for_pin_thread(thread_id):
return f"{settings.PREFIX}/threads/{thread_id}/pin"


def _url_for_un_pin_thread(thread_id):
return f"{settings.PREFIX}/threads/{thread_id}/unpin"
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@


from . import models, settings, utils
from forum import api as forum_api


class User(models.Model):
Expand Down Expand Up @@ -141,35 +142,13 @@ def subscribed_threads(self, query_params=None):
)

def _retrieve(self, *args, **kwargs):
url = self.url(action='get', params=self.attributes)
retrieve_params = self.default_retrieve_params.copy()
retrieve_params.update(kwargs)
if self.attributes.get('course_id'):
retrieve_params['course_id'] = str(self.course_id)
if self.attributes.get('group_id'):
retrieve_params['group_id'] = self.group_id
try:
response = utils.perform_request(
'get',
url,
retrieve_params,
metric_action='model.retrieve',
metric_tags=self._metric_tags,
)
except utils.CommentClientRequestError as e:
if e.status_code == 404:
# attempt to gracefully recover from a previous failure
# to sync this user to the comments service.
self.save()
response = utils.perform_request(
'get',
url,
retrieve_params,
metric_action='model.retrieve',
metric_tags=self._metric_tags,
)
else:
raise
response = forum_api.retrieve_user(self.attributes["id"], retrieve_params)
self._update_from_response(response)

def retire(self, retired_username):
Expand Down
11 changes: 10 additions & 1 deletion requirements/edx/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ django==4.2.15
# enmerkar
# enmerkar-underscore
# event-tracking
# forum
# help-tokens
# jsonfield
# lti-consumer-xblock
Expand Down Expand Up @@ -382,6 +383,7 @@ djangorestframework==3.14.0
# edx-organizations
# edx-proctoring
# edx-submissions
# forum
# openedx-learning
# ora2
# super-csv
Expand Down Expand Up @@ -549,6 +551,7 @@ elasticsearch==7.13.4
# via
# -c requirements/edx/../common_constraints.txt
# edx-search
# forum
enmerkar==0.7.1
# via enmerkar-underscore
enmerkar-underscore==2.3.1
Expand All @@ -566,6 +569,8 @@ filelock==3.15.4
# via snowflake-connector-python
firebase-admin==6.5.0
# via edx-ace
forum @ git+https://github.com/edly-io/forum.git@feat/migrate_from_http_to_python_call
# via -r requirements/edx/github.in
frozenlist==1.4.1
# via
# aiohttp
Expand Down Expand Up @@ -802,7 +807,9 @@ openai==0.28.1
# -c requirements/edx/../constraints.txt
# edx-enterprise
openedx-atlas==0.6.1
# via -r requirements/edx/kernel.in
# via
# -r requirements/edx/kernel.in
# forum
openedx-calc==3.1.0
# via -r requirements/edx/kernel.in
openedx-django-pyfs==3.6.0
Expand Down Expand Up @@ -956,6 +963,7 @@ pymongo==4.4.0
# -r requirements/edx/paver.txt
# edx-opaque-keys
# event-tracking
# forum
# mongoengine
# openedx-mongodbproxy
pynacl==1.5.0
Expand Down Expand Up @@ -1062,6 +1070,7 @@ requests==2.32.3
# edx-drf-extensions
# edx-enterprise
# edx-rest-api-client
# forum
# geoip2
# google-api-core
# google-cloud-storage
Expand Down
10 changes: 10 additions & 0 deletions requirements/edx/development.txt
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ django==4.2.15
# enmerkar
# enmerkar-underscore
# event-tracking
# forum
# help-tokens
# jsonfield
# lti-consumer-xblock
Expand Down Expand Up @@ -615,6 +616,7 @@ djangorestframework==3.14.0
# edx-organizations
# edx-proctoring
# edx-submissions
# forum
# openedx-learning
# ora2
# super-csv
Expand Down Expand Up @@ -857,6 +859,7 @@ elasticsearch==7.13.4
# -r requirements/edx/doc.txt
# -r requirements/edx/testing.txt
# edx-search
# forum
enmerkar==0.7.1
# via
# -r requirements/edx/doc.txt
Expand Down Expand Up @@ -905,6 +908,10 @@ firebase-admin==6.5.0
# -r requirements/edx/doc.txt
# -r requirements/edx/testing.txt
# edx-ace
forum @ git+https://github.com/edly-io/forum.git@feat/migrate_from_http_to_python_call
# via
# -r requirements/edx/doc.txt
# -r requirements/edx/testing.txt
freezegun==1.5.1
# via -r requirements/edx/testing.txt
frozenlist==1.4.1
Expand Down Expand Up @@ -1341,6 +1348,7 @@ openedx-atlas==0.6.1
# via
# -r requirements/edx/doc.txt
# -r requirements/edx/testing.txt
# forum
openedx-calc==3.1.0
# via
# -r requirements/edx/doc.txt
Expand Down Expand Up @@ -1637,6 +1645,7 @@ pymongo==4.4.0
# -r requirements/edx/testing.txt
# edx-opaque-keys
# event-tracking
# forum
# mongoengine
# openedx-mongodbproxy
pynacl==1.5.0
Expand Down Expand Up @@ -1829,6 +1838,7 @@ requests==2.32.3
# edx-drf-extensions
# edx-enterprise
# edx-rest-api-client
# forum
# geoip2
# google-api-core
# google-cloud-storage
Expand Down
11 changes: 10 additions & 1 deletion requirements/edx/doc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ django==4.2.15
# enmerkar
# enmerkar-underscore
# event-tracking
# forum
# help-tokens
# jsonfield
# lti-consumer-xblock
Expand Down Expand Up @@ -452,6 +453,7 @@ djangorestframework==3.14.0
# edx-organizations
# edx-proctoring
# edx-submissions
# forum
# openedx-learning
# ora2
# super-csv
Expand Down Expand Up @@ -633,6 +635,7 @@ elasticsearch==7.13.4
# -c requirements/edx/../common_constraints.txt
# -r requirements/edx/base.txt
# edx-search
# forum
enmerkar==0.7.1
# via
# -r requirements/edx/base.txt
Expand All @@ -658,6 +661,8 @@ firebase-admin==6.5.0
# via
# -r requirements/edx/base.txt
# edx-ace
forum @ git+https://github.com/edly-io/forum.git@feat/migrate_from_http_to_python_call
# via -r requirements/edx/base.txt
frozenlist==1.4.1
# via
# -r requirements/edx/base.txt
Expand Down Expand Up @@ -960,7 +965,9 @@ openai==0.28.1
# -r requirements/edx/base.txt
# edx-enterprise
openedx-atlas==0.6.1
# via -r requirements/edx/base.txt
# via
# -r requirements/edx/base.txt
# forum
openedx-calc==3.1.0
# via -r requirements/edx/base.txt
openedx-django-pyfs==3.6.0
Expand Down Expand Up @@ -1150,6 +1157,7 @@ pymongo==4.4.0
# -r requirements/edx/base.txt
# edx-opaque-keys
# event-tracking
# forum
# mongoengine
# openedx-mongodbproxy
pynacl==1.5.0
Expand Down Expand Up @@ -1271,6 +1279,7 @@ requests==2.32.3
# edx-drf-extensions
# edx-enterprise
# edx-rest-api-client
# forum
# geoip2
# google-api-core
# google-cloud-storage
Expand Down
2 changes: 2 additions & 0 deletions requirements/edx/github.in
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,5 @@
# django42 support PR merged but new release is pending.
# https://github.com/openedx/edx-platform/issues/33431
-e git+https://github.com/anupdhabarde/edx-proctoring-proctortrack.git@31c6c9923a51c903ae83760ecbbac191363aa2a2#egg=edx_proctoring_proctortrack

git+https://github.com/edly-io/forum.git@feat/migrate_from_http_to_python_call#egg=forum
11 changes: 10 additions & 1 deletion requirements/edx/testing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ django==4.2.15
# enmerkar
# enmerkar-underscore
# event-tracking
# forum
# help-tokens
# jsonfield
# lti-consumer-xblock
Expand Down Expand Up @@ -481,6 +482,7 @@ djangorestframework==3.14.0
# edx-organizations
# edx-proctoring
# edx-submissions
# forum
# openedx-learning
# ora2
# super-csv
Expand Down Expand Up @@ -659,6 +661,7 @@ elasticsearch==7.13.4
# -c requirements/edx/../common_constraints.txt
# -r requirements/edx/base.txt
# edx-search
# forum
enmerkar==0.7.1
# via
# -r requirements/edx/base.txt
Expand Down Expand Up @@ -694,6 +697,8 @@ firebase-admin==6.5.0
# via
# -r requirements/edx/base.txt
# edx-ace
forum @ git+https://github.com/edly-io/forum.git@feat/migrate_from_http_to_python_call
# via -r requirements/edx/base.txt
freezegun==1.5.1
# via -r requirements/edx/testing.in
frozenlist==1.4.1
Expand Down Expand Up @@ -1011,7 +1016,9 @@ openai==0.28.1
# -r requirements/edx/base.txt
# edx-enterprise
openedx-atlas==0.6.1
# via -r requirements/edx/base.txt
# via
# -r requirements/edx/base.txt
# forum
openedx-calc==3.1.0
# via -r requirements/edx/base.txt
openedx-django-pyfs==3.6.0
Expand Down Expand Up @@ -1235,6 +1242,7 @@ pymongo==4.4.0
# -r requirements/edx/base.txt
# edx-opaque-keys
# event-tracking
# forum
# mongoengine
# openedx-mongodbproxy
pynacl==1.5.0
Expand Down Expand Up @@ -1388,6 +1396,7 @@ requests==2.32.3
# edx-drf-extensions
# edx-enterprise
# edx-rest-api-client
# forum
# geoip2
# google-api-core
# google-cloud-storage
Expand Down

0 comments on commit 9126017

Please sign in to comment.