Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions contentcuration/contentcuration/constants/locking.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
"""
TREE_LOCK = 1001
TASK_LOCK = 1002
KOLIBRI_PUBLIC_LOCK = 1003
35 changes: 34 additions & 1 deletion contentcuration/contentcuration/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from django.db import connection
from django.db import IntegrityError
from django.db import models
from django.db import transaction
from django.db.models import Count
from django.db.models import Exists
from django.db.models import F
Expand Down Expand Up @@ -56,6 +57,7 @@
from django_cte import CTEManager
from django_cte import CTEQuerySet
from django_cte import With
from kombu.exceptions import OperationalError as KombuOperationalError
from le_utils import proquint
from le_utils.constants import content_kinds
from le_utils.constants import exercises
Expand Down Expand Up @@ -90,6 +92,7 @@
from contentcuration.utils.cache import delete_public_channel_cache_keys
from contentcuration.utils.i18n import closest_supported_locale
from contentcuration.utils.parser import load_json_string
from contentcuration.utils.sentry import report_exception
from contentcuration.utils.urls import canonical_url
from contentcuration.viewsets.sync.constants import ALL_CHANGES
from contentcuration.viewsets.sync.constants import ALL_TABLES
Expand Down Expand Up @@ -1406,18 +1409,41 @@ def on_update(self): # noqa C901
channel=self, version=self.version
)

return ("public" in original_values or "deleted" in original_values) and (
self.public or original_values.get("public")
)

def _schedule_kolibri_public_sync(self):
from contentcuration.tasks import sync_channel_to_kolibri_public_task

channel_id = self.id

def send_task():
try:
sync_channel_to_kolibri_public_task.apply_async(
kwargs={"channel_id": channel_id}
)
except KombuOperationalError as e:
report_exception(e)

transaction.on_commit(send_task)

def save(self, *args, **kwargs):
self._actor_id = kwargs.pop("actor_id", None)
creating = self._state.adding
sync_kolibri_public = False
if creating:
if self._actor_id is None:
raise ValueError("No actor_id passed to save method")
self.on_create()
else:
self.on_update()
sync_kolibri_public = self.on_update()

super(Channel, self).save(*args, **kwargs)

if sync_kolibri_public:
self._schedule_kolibri_public_sync()

if creating:
self.history.create(
actor_id=self._actor_id, action=channel_history.CREATION
Expand Down Expand Up @@ -1482,6 +1508,7 @@ def make_public(self, bypass_signals=False):
Channel.objects.filter(id=self.id).update(public=True)
# clear the channel cache
delete_public_channel_cache_keys()
self._schedule_kolibri_public_sync()
else:
self.public = True
self.save()
Expand Down Expand Up @@ -3569,6 +3596,12 @@ class Meta:
]


@receiver(models.signals.post_delete, sender=Channel)
def sync_kolibri_public_on_channel_delete(sender, instance, **kwargs):
if instance.public:
instance._schedule_kolibri_public_sync()


@receiver(models.signals.post_delete, sender=File)
def auto_delete_file_on_delete(sender, instance, **kwargs):
"""
Expand Down
8 changes: 8 additions & 0 deletions contentcuration/contentcuration/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
from django.core.mail import EmailMessage
from django.template.loader import render_to_string
from django.utils.translation import override
from kolibri_public.utils.export_channel_to_kolibri_public import (
sync_channel_to_kolibri_public,
)

from contentcuration.celery import app
from contentcuration.models import Change
Expand Down Expand Up @@ -108,6 +111,11 @@ def deletetree_task(tree_id):
ContentNode.objects.filter(tree_id=tree_id).delete()


@app.task(name="sync_channel_to_kolibri_public_task")
def sync_channel_to_kolibri_public_task(channel_id):
sync_channel_to_kolibri_public(channel_id)


@app.task(name="getnodedetails_task")
def getnodedetails_task(node_id):
node = ContentNode.objects.get(pk=node_id)
Expand Down
17 changes: 17 additions & 0 deletions contentcuration/contentcuration/tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import mock
from celery import states
from celery.signals import before_task_publish
from django.urls import reverse
from django_celery_results.models import TaskResult
from search.models import ContentNodeFullTextSearch
Expand All @@ -28,6 +29,22 @@ def clear_tasks(except_task_id=None):
qs.update(status=states.REVOKED)


def record_published_tasks(testcase):
"""
:return: A list that collects `(task name, task kwargs)` for each task sent
to the broker until the test ends
"""
published = []

def record(sender=None, body=None, **kwargs):
published.append((sender, body[1]))

before_task_publish.connect(record, weak=False)
testcase.addCleanup(before_task_publish.disconnect, record)
testcase.addCleanup(app.control.purge)
return published


def mock_class_instance(target):
"""
Helper that returns a Mocked instance of the `target` class
Expand Down
55 changes: 53 additions & 2 deletions contentcuration/contentcuration/tests/test_channel_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from contentcuration.models import ChannelSet
from contentcuration.models import generate_storage_url
from contentcuration.models import SecretToken
from contentcuration.tests.helpers import record_published_tasks
from contentcuration.tests.utils import mixer


Expand All @@ -30,8 +31,58 @@ def test_channel_make_public_makes_the_current_channel_public(self):
c.make_public()
assert c.public

# TODO(aron): test the bypass_signals arg to make_public
#
def setUp(self):
super().setUp()
self.published = record_published_tasks(self)

def _published_syncs(self):
return [
kwargs
for name, kwargs in self.published
if name == "sync_channel_to_kolibri_public_task"
]

def test_make_public_publishes_sync_only_after_commit(self):
for bypass_signals in (False, True):
with self.subTest(bypass_signals=bypass_signals):
self.published.clear()
c = channel()

with self.captureOnCommitCallbacks() as callbacks:
c.make_public(bypass_signals=bypass_signals)

self.assertEqual(self._published_syncs(), [])
for callback in callbacks:
callback()
self.assertEqual(self._published_syncs(), [{"channel_id": c.id}])

def test_soft_deleting_private_channel_publishes_no_sync(self):
c = channel()

with self.captureOnCommitCallbacks(execute=True):
c.deleted = True
c.save(actor_id=self.admin_user.id)

self.assertEqual(self._published_syncs(), [])

def test_hard_deleting_private_channel_publishes_no_sync(self):
c = channel()

with self.captureOnCommitCallbacks(execute=True):
c.delete()

self.assertEqual(self._published_syncs(), [])

def test_renaming_public_channel_publishes_no_sync(self):
c = channel()
Channel.objects.filter(id=c.id).update(public=True)
c = Channel.objects.get(id=c.id)

with self.captureOnCommitCallbacks(execute=True):
c.name = "renamed"
c.save()

self.assertEqual(self._published_syncs(), [])


class ChannelTokenTestCase(StudioTestCase):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
from django.db.models import Q
from django.utils import timezone
from kolibri_public.models import ChannelMetadata
from kolibri_public.utils import export_channel_to_kolibri_public
from kolibri_public.utils.export_channel_to_kolibri_public import (
sync_channel_to_kolibri_public,
)

from contentcuration.models import Channel
from contentcuration.models import User
Expand Down Expand Up @@ -46,7 +48,7 @@ def handle(self, *args, **options):
count = 0
for channel_id in ids_to_export:
try:
export_channel_to_kolibri_public(channel_id)
sync_channel_to_kolibri_public(channel_id)
count += 1
except FileNotFoundError:
logger.warning(
Expand Down
Loading
Loading