Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,7 @@ describe('ContentNode methods', () => {
parent: parent.id,
lft: 1,
node_id: expect.not.stringMatching(new RegExp(`${node.node_id}|${parent.node_id}`)),
original_channel_id: node.original_channel_id || node.channel_id,
original_source_node_id: node.original_source_node_id,
source_channel_id: node.channel_id,
source_node_id: node.node_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,7 @@ class Resource extends mix(APIResource, IndexedDBResource) {
// Only fetch new updates if we've finished syncing the changes table
db[CHANGES_TABLE].where('table')
.equals(this.tableName)
.filter(c => !c.synced)
Comment thread
ozer550 marked this conversation as resolved.
.limit(1)
.toArray()
.then(pendingChanges => {
Expand Down Expand Up @@ -1392,6 +1393,7 @@ export const ContentNode = new TreeResource({
// Placeholder node_id, we should receive the new value from backend result
node_id: uuid4(),
original_source_node_id: node.original_source_node_id || node.node_id,
original_channel_id: node.original_channel_id || node.channel_id,
source_channel_id: node.channel_id,
source_node_id: node.node_id,
channel_id: parent.channel_id,
Expand Down
16 changes: 0 additions & 16 deletions contentcuration/contentcuration/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import functools
import hashlib
import json
import logging
Expand All @@ -8,7 +7,6 @@
from datetime import datetime

import pytz
from celery import states
from django.conf import settings
from django.contrib.auth.base_user import AbstractBaseUser
from django.contrib.auth.base_user import BaseUserManager
Expand Down Expand Up @@ -1106,20 +1104,6 @@ class Meta:
unique_together = ['tag_name', 'channel']


def delegate_manager(method):
"""
Delegate method calls to base manager, if exists.
"""

@functools.wraps(method)
def wrapped(self, *args, **kwargs):
if self._base_manager:
return getattr(self._base_manager, method.__name__)(*args, **kwargs)
return method(self, *args, **kwargs)

return wrapped


class License(models.Model):
"""
Normalize the license of ContentNode model
Expand Down
100 changes: 100 additions & 0 deletions contentcuration/contentcuration/tests/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from .testdata import create_temp_file
from contentcuration.models import AssessmentItem
from contentcuration.models import Channel
from contentcuration.models import ContentTag
from contentcuration.utils.publish import mark_all_nodes_as_published
from contentcuration.utils.sync import sync_channel

Expand Down Expand Up @@ -156,3 +157,102 @@ def test_sync_assessment_item_add(self):
self.assertEqual(target_ai.files.filter(checksum=db_file.checksum).count(), 1)

self.assertTrue(self.derivative_channel.has_changes())

def test_sync_tags_add(self):
"""
Test that calling sync_node_tags successfully syncs a tag added to the original node to
the copied node.
"""

self.assertFalse(self.derivative_channel.has_changes())

contentnode = (
self.channel.main_tree.get_descendants()
.exclude(kind_id=content_kinds.TOPIC)
.first()
)

target_child = self.derivative_channel.main_tree.get_descendants().get(
source_node_id=contentnode.node_id
)

self.assertIsNotNone(target_child)
self.assertEqual(
target_child.tags.count(), contentnode.tags.count()
)

tag = ContentTag.objects.create(tag_name="tagname")

contentnode.tags.add(tag)

self.assertNotEqual(
target_child.tags.count(), contentnode.tags.count()
)

sync_channel(self.derivative_channel, sync_tags=True)
self.derivative_channel.main_tree.refresh_from_db()

self.assertEqual(
target_child.tags.count(), contentnode.tags.count()
)

self.assertEqual(
target_child.tags.filter(
tag_name=tag.tag_name
).count(),
1,
)

self.assertTrue(self.derivative_channel.has_changes())

def test_sync_tags_add_multiple_tags(self):
"""
Test that calling sync_node_tags does not raise an error when there
are multiple tags with the same tag_name and null channel_id.
"""

self.assertFalse(self.derivative_channel.has_changes())

contentnode = (
self.channel.main_tree.get_descendants()
.exclude(kind_id=content_kinds.TOPIC)
.first()
)

target_child = self.derivative_channel.main_tree.get_descendants().get(
source_node_id=contentnode.node_id
)

self.assertIsNotNone(target_child)
self.assertEqual(
target_child.tags.count(), contentnode.tags.count()
)

# Create the same tag twice
ContentTag.objects.create(tag_name="tagname")

tag = ContentTag.objects.create(tag_name="tagname")

contentnode.tags.add(tag)

self.assertNotEqual(
target_child.tags.count(), contentnode.tags.count()
)
try:
sync_channel(self.derivative_channel, sync_tags=True)
except Exception as e:
self.fail("Could not run sync_channel without raising exception: {}".format(e))
self.derivative_channel.main_tree.refresh_from_db()

self.assertEqual(
target_child.tags.count(), contentnode.tags.count()
)

self.assertEqual(
target_child.tags.filter(
tag_name=tag.tag_name
).count(),
1,
)

self.assertTrue(self.derivative_channel.has_changes())
6 changes: 4 additions & 2 deletions contentcuration/contentcuration/utils/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,11 @@ def sync_node_tags(node, original):
for tag in original.tags.exclude(
tag_name__in=node.tags.values_list("tag_name", flat=True)
):
new_tag, _ = ContentTag.objects.get_or_create(
new_tag = ContentTag.objects.filter(
tag_name=tag.tag_name, channel_id=None,
)
).first()
if not new_tag:
new_tag = ContentTag.objects.create(tag_name=tag.tag_name, channel_id=None)
node.tags.add(new_tag)
node.changed = True

Expand Down