Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude courses which have the download button disabled from mirror drives #2282

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
5 changes: 5 additions & 0 deletions content_sync/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@
DEV_DRAFT_URL = "http://10.1.0.102:8044"
DEV_LIVE_URL = "http://10.1.0.102:8045"
DEV_TEST_URL = "http://10.1.0.102:8046"


# Publish Date Constants
PUBLISH_DATE_LIVE = "publish_date"
PUBLISH_DATE_DRAFT = "draft_publish_date"
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def __init__( # noqa: PLR0913
hugo_arg_overrides: Optional[str] = None,
):
vars = get_common_pipeline_vars() # noqa: A001
sites = list(get_publishable_sites(version))
sites = list(get_publishable_sites(version, is_offline=offline))
shuffle(sites)
self.sites = sites
self.version = version
Expand Down
12 changes: 7 additions & 5 deletions content_sync/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from content_sync.constants import VERSION_DRAFT, VERSION_LIVE, WEBSITE_LISTING_DIRPATH
from content_sync.decorators import single_task
from content_sync.models import ContentSyncState
from content_sync.utils import get_publishable_sites
from main.celery import app
from main.s3_utils import get_boto3_resource
from websites.api import (
Expand Down Expand Up @@ -460,13 +461,14 @@ def update_websites_in_root_website():
if settings.CONTENT_SYNC_BACKEND:
root_website = Website.objects.get(name=settings.ROOT_WEBSITE_NAME)
# Get all sites, minus any sites that have never been successfully published
sites = Website.objects.exclude(
Q(**{"draft_publish_date__isnull": True})
& Q(**{"publish_date__isnull": True})
)
sites = sites.exclude(Q(url_path__isnull=True))
# and have downloading disabled
sites = get_publishable_sites(is_offline=True)
# Exclude the root website
sites = sites.exclude(name=settings.ROOT_WEBSITE_NAME)

# Remove the content for unpublished or not downloadable sites
WebsiteContent.objects.exclude(website__in=sites).delete()

fields = [
"website",
"type",
Expand Down
33 changes: 26 additions & 7 deletions content_sync/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@
OFFLINE_START,
ONLINE_END,
ONLINE_START,
PUBLISH_DATE_DRAFT,
PUBLISH_DATE_LIVE,
START_TAG_PREFIX,
TARGET_OFFLINE,
VERSION_DRAFT,
VERSION_LIVE,
)
from main.s3_utils import get_boto3_resource
from main.utils import is_dev
from websites.constants import WEBSITE_CONTENT_FILETYPE
from websites.constants import CONTENT_TYPE_METADATA, WEBSITE_CONTENT_FILETYPE
from websites.models import Website, WebsiteContent
from websites.site_config_api import SiteConfig

Expand Down Expand Up @@ -158,24 +159,42 @@ def get_ocw_studio_api_url():
return "http://10.1.0.102:8043" if is_dev() else settings.SITE_BASE_URL


def get_publishable_sites(version: str):
def get_publishable_sites(version: str = "", *, is_offline: bool = False):
"""
Get a QuerySet of Website objects that are eligible for publishing

Args:
version(str): The version (draft/live) to check publish eligibility with
is_offline(bool): Is the build type offline or not
"""
publish_date_field = (
"publish_date" if version == VERSION_LIVE else "draft_publish_date"
)
publish_date_field_filter = {
f"{PUBLISH_DATE_DRAFT}__isnull": True,
f"{PUBLISH_DATE_LIVE}__isnull": True,
}

if version:
publish_date_field_filter.pop(
f"{PUBLISH_DATE_LIVE}__isnull"
if version == VERSION_DRAFT
else f"{PUBLISH_DATE_DRAFT}__isnull"
)

# Get all sites, minus any sites that have never been successfully published and test sites # noqa: E501
sites = (
Website.objects.exclude(
Q(**{f"{publish_date_field}__isnull": True}) | Q(url_path__isnull=True)
Q(**publish_date_field_filter) | Q(url_path__isnull=True)
)
.exclude(unpublish_status__isnull=False)
.exclude(name__in=settings.OCW_TEST_SITE_SLUGS)
)

if is_offline:
# filter sites with download disabled
download_disabled_sites = WebsiteContent.objects.filter(
type=CONTENT_TYPE_METADATA, metadata__hide_download=True
).values_list("website__short_id", flat=True)
sites = sites.exclude(short_id__in=download_disabled_sites)

return sites.prefetch_related("starter")


Expand Down
19 changes: 16 additions & 3 deletions content_sync/utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
)
from main.s3_utils import get_boto3_client
from ocw_import.conftest import MOCK_BUCKET_NAME, setup_s3
from websites.constants import CONTENT_TYPE_METADATA
from websites.factories import WebsiteContentFactory, WebsiteStarterFactory
from websites.site_config_api import ConfigItem, SiteConfig

Expand Down Expand Up @@ -257,7 +258,8 @@ def test_get_ocw_studio_api_url(settings, mocker, is_dev):


@pytest.mark.parametrize("version", [VERSION_DRAFT, VERSION_LIVE])
def test_get_publishable_sites(settings, mocker, mass_build_websites, version):
@pytest.mark.parametrize("offline", [False, True])
def test_get_publishable_sites(settings, mocker, mass_build_websites, version, offline):
"""get_publishable_sites should return a queryset of sites that have been published before"""
unpublished_site = mass_build_websites[0]
if version == VERSION_DRAFT:
Expand All @@ -270,8 +272,19 @@ def test_get_publishable_sites(settings, mocker, mass_build_websites, version):
test_site.name = test_site_slug
test_site.save()
assert len(mass_build_websites) == 7
publishable_sites = get_publishable_sites(version)
assert publishable_sites.count() == 4

publishable_sites_count = 4

if offline:
WebsiteContentFactory.create(
type=CONTENT_TYPE_METADATA,
metadata={"hide_download": True},
website=mass_build_websites[4],
)
publishable_sites_count = 3

publishable_sites = get_publishable_sites(version, is_offline=offline)
assert publishable_sites.count() == publishable_sites_count


@pytest.mark.parametrize("version", [VERSION_DRAFT, VERSION_LIVE])
Expand Down