Skip to content

Commit

Permalink
so hacky! will it pass the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zawan-ila committed May 2, 2024
1 parent a396cf8 commit 131b248
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
28 changes: 27 additions & 1 deletion course_discovery/apps/course_metadata/managers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.db import models
from django.db.models import Q

from course_discovery.apps.course_metadata.query import *

class DraftManager(models.Manager):
""" Model manager that hides draft rows unless you ask for them. """
Expand All @@ -24,3 +24,29 @@ def get_draft(self, **kwargs):
If a draft is not available, we give back the non-draft version.
"""
return self.filter_drafts(**kwargs).get()


class EverythingRunManagerWithoutRestriction(models.Manager):
def get_queryset(self):
return CourseRunQuerySet(self.model).filter(restricted_run__isnull=True)


class DraftRunManagerWithoutRestriction(DraftManager):
def get_queryset(self):
return super().get_queryset().filter(restricted_run__isnull=True)
def _with_drafts(self):
return super()._with_drafts().filter(restricted_run__isnull=True)

def filter_drafts(self, **kwargs):
"""
Acts like filter(), but prefers draft versions.
If a draft is not available, we give back the non-draft version.
"""
return self._with_drafts().filter(Q(draft=models.Value(1)) | Q(draft_version=None)).filter(**kwargs).filter(restricted_run__isnull=True)

def get_draft(self, **kwargs):
"""
Acts like get(), but prefers draft versions. (including raising exceptions like get does)
If a draft is not available, we give back the non-draft version.
"""
return self.filter_drafts(**kwargs).filter(restricted_run__isnull=True).get()

Check warning on line 52 in course_discovery/apps/course_metadata/managers.py

View check run for this annotation

Codecov / codecov/patch

course_discovery/apps/course_metadata/managers.py#L52

Added line #L52 was not covered by tests
33 changes: 33 additions & 0 deletions course_discovery/apps/course_metadata/middle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

import json
from django.http import *
from course_discovery.apps.course_metadata.models import *
from course_discovery.apps.course_metadata.managers import *

class MagicMiddleware:
def __init__(self, get_response):
self.get_response = get_response
# One-time configuration and initialization.

def __call__(self, request):
# Code to be executed for each request before
# the view (and later middleware) are called.

method = request.method


if method == 'GET' and not request.GET.get('restriction_type', None):
CourseRun._meta.local_managers.clear()
CourseRun.add_to_class('everything', EverythingRunManagerWithoutRestriction() )
CourseRun.add_to_class('objects', DraftRunManagerWithoutRestriction.from_queryset(CourseRunQuerySet)())

response = self.get_response(request)

# Code to be executed for each request/response after
# the view is called.
if method == 'GET':
CourseRun._meta.local_managers.clear()
CourseRun.add_to_class('everything', CourseRunQuerySet.as_manager() )
CourseRun.add_to_class('objects', DraftManager.from_queryset(CourseRunQuerySet)())

return response
7 changes: 6 additions & 1 deletion course_discovery/apps/course_metadata/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
)
from course_discovery.apps.course_metadata.constants import SUBDIRECTORY_SLUG_FORMAT_REGEX, PathwayType
from course_discovery.apps.course_metadata.fields import AutoSlugWithSlashesField, HtmlField, NullHtmlField
from course_discovery.apps.course_metadata.managers import DraftManager
from course_discovery.apps.course_metadata.managers import DraftManager, DraftRunManagerWithoutRestriction, EverythingRunManagerWithoutRestriction
from course_discovery.apps.course_metadata.people import MarketingSitePeople
from course_discovery.apps.course_metadata.publishers import (
CourseRunMarketingSitePublisher, ProgramMarketingSitePublisher
Expand Down Expand Up @@ -2193,6 +2193,9 @@ class CourseRun(ManageHistoryMixin, DraftModelMixin, CachedMixin, TimeStampedMod
everything = CourseRunQuerySet.as_manager()
objects = DraftManager.from_queryset(CourseRunQuerySet)()

# everything = EverythingRunManagerWithoutRestriction()
# objects = DraftRunManagerWithoutRestriction.from_queryset(CourseRunQuerySet)()

# Do not record the slug field in the history table because AutoSlugField is not compatible with
# django-simple-history. Background: https://github.com/openedx/course-discovery/pull/332
history = HistoricalRecords(excluded_fields=['slug'])
Expand Down Expand Up @@ -2259,6 +2262,8 @@ class Meta:
('key', 'draft'),
('uuid', 'draft'),
)
# base_manager_name='everything'
# default_manager_name='everything'

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand Down
3 changes: 3 additions & 0 deletions course_discovery/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from os.path import abspath, dirname, join
from sys import path

# from course_discovery.apps.course_metadata.middle import *

from corsheaders.defaults import default_headers as corsheaders_default_headers

here = lambda *x: join(abspath(dirname(__file__)), *x)
Expand Down Expand Up @@ -119,6 +121,7 @@
'edx_django_utils.cache.middleware.TieredCacheMiddleware',
'edx_rest_framework_extensions.middleware.RequestMetricsMiddleware',
'edx_rest_framework_extensions.auth.jwt.middleware.EnsureJWTAuthSettingsMiddleware',
'course_discovery.apps.course_metadata.middle.MagicMiddleware',
)

ROOT_URLCONF = 'course_discovery.urls'
Expand Down

0 comments on commit 131b248

Please sign in to comment.