Skip to content

Commit

Permalink
Merge pull request #4826 from bjester/load-channel-rev
Browse files Browse the repository at this point in the history
Use CTE for server rev query, add tests
  • Loading branch information
rtibbles authored Nov 15, 2024
2 parents e349e81 + 6cc44b4 commit f1caf61
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
15 changes: 15 additions & 0 deletions contentcuration/contentcuration/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
from django.dispatch import receiver
from django.utils import timezone
from django.utils.translation import gettext as _
from django_cte import CTEManager
from django_cte import With
from le_utils import proquint
from le_utils.constants import content_kinds
Expand Down Expand Up @@ -1060,6 +1061,18 @@ def mark_publishing(self, user):
self.main_tree.publishing = True
self.main_tree.save()

def get_server_rev(self):
changes_cte = With(
Change.objects.filter(channel=self).values("server_rev", "applied"),
)
return (
changes_cte.queryset()
.with_cte(changes_cte)
.filter(applied=True)
.values_list("server_rev", flat=True)
.order_by("-server_rev").first()
) or 0

@property
def deletion_history(self):
return self.history.filter(action=channel_history.DELETION)
Expand Down Expand Up @@ -2571,6 +2584,8 @@ class Change(models.Model):
# will be falsy.
unpublishable = models.BooleanField(null=True, blank=True, default=False)

objects = CTEManager()

@classmethod
def _create_from_change(
cls,
Expand Down
25 changes: 25 additions & 0 deletions contentcuration/contentcuration/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from contentcuration.constants import channel_history
from contentcuration.constants import user_history
from contentcuration.models import AssessmentItem
from contentcuration.models import Change
from contentcuration.models import Channel
from contentcuration.models import ChannelHistory
from contentcuration.models import ChannelSet
Expand All @@ -32,6 +33,7 @@
from contentcuration.models import UserHistory
from contentcuration.tests import testdata
from contentcuration.tests.base import StudioTestCase
from contentcuration.viewsets.sync.constants import DELETED


@pytest.fixture
Expand Down Expand Up @@ -251,6 +253,29 @@ def test_filter_edit_queryset__private_channel__anonymous(self):
queryset = Channel.filter_edit_queryset(self.base_queryset, user=self.anonymous_user)
self.assertQuerysetDoesNotContain(queryset, pk=channel.id)

def test_get_server_rev(self):
channel = testdata.channel()

def create_change(server_rev, applied):
return Change(
channel=channel,
server_rev=server_rev,
user=self.admin_user,
created_by=self.admin_user,
change_type=DELETED,
table=Channel.__name__,
applied=applied,
kwargs={},
)

Change.objects.bulk_create([
create_change(1, True),
create_change(2, True),
create_change(3, False),
])

self.assertEqual(channel.get_server_rev(), 2)


class ContentNodeTestCase(PermissionQuerysetTestCase):
@property
Expand Down
11 changes: 2 additions & 9 deletions contentcuration/contentcuration/views/base.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
import json
from builtins import str
from urllib.parse import urlsplit

from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.core.cache import cache
from django.core.exceptions import PermissionDenied
from django.db.models import Count
from django.db.models import Exists
from django.db.models import IntegerField
from django.db.models import OuterRef
from django.db.models import Subquery
from django.db.models import UUIDField
from django.db.models.functions import Cast
from django.http import HttpResponse
from django.http import HttpResponseBadRequest
from django.http import HttpResponseForbidden
from django.http import HttpResponseNotFound
from django.shortcuts import redirect
from django.shortcuts import render
from django.urls import is_valid_path
Expand All @@ -30,6 +25,7 @@
from django.views.decorators.http import require_POST
from django.views.i18n import LANGUAGE_QUERY_PARAMETER
from django_celery_results.models import TaskResult
from django_cte import With
from rest_framework.authentication import BasicAuthentication
from rest_framework.authentication import SessionAuthentication
from rest_framework.authentication import TokenAuthentication
Expand All @@ -41,21 +37,18 @@
from rest_framework.response import Response

from .json_dump import json_for_parse_from_data
from .json_dump import json_for_parse_from_serializer
from contentcuration.constants import channel_history
from contentcuration.decorators import browser_is_supported
from contentcuration.models import Change
from contentcuration.models import Channel
from contentcuration.models import ChannelHistory
from contentcuration.models import ChannelSet
from contentcuration.models import ContentKind
from contentcuration.models import CustomTaskMetadata
from contentcuration.models import DEFAULT_USER_PREFERENCES
from contentcuration.models import Language
from contentcuration.models import License
from contentcuration.serializers import SimplifiedChannelProbeCheckSerializer
from contentcuration.utils.messages import get_messages
from contentcuration.viewsets.channelset import PublicChannelSetSerializer

PUBLIC_CHANNELS_CACHE_DURATION = 30 # seconds
PUBLIC_CHANNELS_CACHE_KEYS = {
Expand Down Expand Up @@ -317,7 +310,7 @@ def channel(request, channel_id):
if channel.deleted:
channel_error = 'CHANNEL_EDIT_ERROR_CHANNEL_DELETED'
else:
channel_rev = Change.objects.filter(applied=True, channel=channel).values_list("server_rev", flat=True).order_by("-server_rev").first() or 0
channel_rev = channel.get_server_rev()

return render(
request,
Expand Down

0 comments on commit f1caf61

Please sign in to comment.