feat: adds ability to disable posting in discussions indefinitely (#32171)
* feat: adds ability to disable posting in discussions indefinitely * test: fixed ffailing test cases * test: added new model field in test cases * refactor: removes unnecessary migrations * refactor: removed previous migrations and adds new field in discussions api * refactor: added docstring and changed method name --------- Co-authored-by: ayesha waris <73840786+ayeshoali@users.noreply.github.com>
This commit is contained in:
@@ -40,7 +40,12 @@ from lms.djangoapps.courseware.exceptions import CourseAccessRedirect
|
||||
from lms.djangoapps.discussion.toggles import ENABLE_DISCUSSIONS_MFE, ENABLE_LEARNERS_TAB_IN_DISCUSSIONS_MFE
|
||||
from lms.djangoapps.discussion.toggles_utils import reported_content_email_notification_enabled
|
||||
from lms.djangoapps.discussion.views import is_privileged_user
|
||||
from openedx.core.djangoapps.discussions.models import DiscussionsConfiguration, DiscussionTopicLink, Provider
|
||||
from openedx.core.djangoapps.discussions.models import (
|
||||
DiscussionsConfiguration,
|
||||
DiscussionTopicLink,
|
||||
Provider,
|
||||
PostingRestriction
|
||||
)
|
||||
from openedx.core.djangoapps.discussions.utils import get_accessible_discussion_xblocks
|
||||
from openedx.core.djangoapps.django_comment_common import comment_client
|
||||
from openedx.core.djangoapps.django_comment_common.comment_client.comment import Comment
|
||||
@@ -319,14 +324,38 @@ def get_course(request, course_key):
|
||||
"""
|
||||
return dt.isoformat().replace('+00:00', 'Z')
|
||||
|
||||
def is_posting_allowed(posting_restrictions, blackout_schedules):
|
||||
"""
|
||||
Check if posting is allowed based on the given posting restrictions and blackout schedules.
|
||||
|
||||
Args:
|
||||
posting_restrictions (str): Values would be "disabled", "scheduled" or "enabled".
|
||||
blackout_schedules (List[Dict[str, datetime]]): The list of blackout schedules
|
||||
|
||||
Returns:
|
||||
bool: True if posting is allowed, False otherwise.
|
||||
"""
|
||||
now = datetime.now(UTC)
|
||||
if posting_restrictions == PostingRestriction.DISABLED:
|
||||
return True
|
||||
elif posting_restrictions == PostingRestriction.SCHEDULED:
|
||||
return not any(schedule["start"] <= now <= schedule["end"] for schedule in blackout_schedules)
|
||||
else:
|
||||
return False
|
||||
|
||||
course = _get_course(course_key, request.user)
|
||||
user_roles = get_user_role_names(request.user, course_key)
|
||||
course_config = DiscussionsConfiguration.get(course_key)
|
||||
EDIT_REASON_CODES = getattr(settings, "DISCUSSION_MODERATION_EDIT_REASON_CODES", {})
|
||||
CLOSE_REASON_CODES = getattr(settings, "DISCUSSION_MODERATION_CLOSE_REASON_CODES", {})
|
||||
is_posting_enabled = is_posting_allowed(
|
||||
course_config.posting_restrictions,
|
||||
course.get_discussion_blackout_datetimes()
|
||||
)
|
||||
|
||||
return {
|
||||
"id": str(course_key),
|
||||
"is_posting_enabled": is_posting_enabled,
|
||||
"blackouts": [
|
||||
{
|
||||
"start": _format_datetime(blackout["start"]),
|
||||
|
||||
@@ -190,6 +190,7 @@ class GetCourseTest(ForumsEnableMixin, UrlResetMixin, SharedModuleStoreTestCase)
|
||||
def test_basic(self):
|
||||
assert get_course(self.request, self.course.id) == {
|
||||
'id': str(self.course.id),
|
||||
'is_posting_enabled': True,
|
||||
'blackouts': [],
|
||||
'thread_list_url': 'http://testserver/api/discussion/v1/threads/?course_id=course-v1%3Ax%2By%2Bz',
|
||||
'following_thread_list_url':
|
||||
|
||||
@@ -517,6 +517,7 @@ class CourseViewTest(DiscussionAPIViewTestMixin, ModuleStoreTestCase):
|
||||
200,
|
||||
{
|
||||
"id": str(self.course.id),
|
||||
"is_posting_enabled": True,
|
||||
"blackouts": [],
|
||||
"thread_list_url": "http://testserver/api/discussion/v1/threads/?course_id=course-v1%3Ax%2By%2Bz",
|
||||
"following_thread_list_url": (
|
||||
|
||||
Reference in New Issue
Block a user