Skip to content

Commit

Permalink
Revert "1869: Course program api performance improvements (#1872)"
Browse files Browse the repository at this point in the history
This reverts commit f16b217.
  • Loading branch information
collinpreston committed Sep 12, 2023
1 parent 912072c commit b506f4a
Show file tree
Hide file tree
Showing 21 changed files with 217 additions and 172 deletions.
3 changes: 1 addition & 2 deletions cms/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from django.core.exceptions import ValidationError
from django.core.serializers.json import DjangoJSONEncoder
from django.db import models
from django.utils.functional import cached_property
from django.forms import ChoiceField, DecimalField
from django.http import Http404
from django.template.response import TemplateResponse
Expand Down Expand Up @@ -1085,7 +1084,7 @@ class CoursePage(ProductPage):
)
]

@cached_property
@property
def product(self):
"""Gets the product associated with this page"""
return self.course
Expand Down
24 changes: 21 additions & 3 deletions cms/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class CoursePageSerializer(serializers.ModelSerializer):
description = serializers.SerializerMethodField()
current_price = serializers.SerializerMethodField()
instructors = serializers.SerializerMethodField()
live = serializers.SerializerMethodField()
effort = serializers.SerializerMethodField()
length = serializers.SerializerMethodField()

Expand Down Expand Up @@ -75,14 +76,28 @@ def get_financial_assistance_form_url(self, instance):
else ""
)

def get_next_run_id(self, instance):
"""Get next run id"""
run = instance.course.first_unexpired_run
return run.id if run is not None else None

def get_description(self, instance):
return bleach.clean(instance.description, tags=[], strip=True)

def get_current_price(self, instance):
next_run = self.get_next_run_id(instance)

if next_run is None:
return None

course_ct = ContentType.objects.get(app_label="courses", model="courserun")

relevant_product = (
instance.product.active_products.filter().order_by("-price").first()
if instance.product.active_products
else None
Product.objects.filter(
content_type=course_ct, object_id=next_run, is_active=True
)
.order_by("-price")
.first()
)
return relevant_product.price if relevant_product else None

Expand All @@ -105,6 +120,9 @@ def get_instructors(self, instance):

return returnable_members

def get_live(self, instance):
return instance.live

def get_effort(self, instance):
return (
bleach.clean(instance.effort, tags=[], strip=True)
Expand Down
2 changes: 2 additions & 0 deletions courses/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@
CourseRunCertificate,
CourseRunEnrollment,
CourseRunGrade,
Program,
ProgramCertificate,
ProgramEnrollment,
ProgramRequirement,
ProgramRequirementNodeType,
)
from courses.tasks import subscribe_edx_course_emails
from courses.utils import (
Expand Down
22 changes: 0 additions & 22 deletions courses/migrations/0043_course_program_live_index.py

This file was deleted.

17 changes: 7 additions & 10 deletions courses/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class Meta:
readable_id = models.CharField(
max_length=255, unique=True, validators=[validate_url_path_field]
)
live = models.BooleanField(default=False, db_index=True)
live = models.BooleanField(default=False)
program_type = models.CharField(
max_length=255,
default="Series",
Expand Down Expand Up @@ -295,7 +295,7 @@ def save(self, *args, **kwargs):
program=self, node_type=ProgramRequirementNodeType.PROGRAM_ROOT.value
)

@cached_property
@property
def courses(self):
"""
Returns the courses associated with this program via the requirements
Expand All @@ -315,7 +315,7 @@ def courses(self):
path__startswith=op.path,
node_type=ProgramRequirementNodeType.COURSE,
)
.select_related("course")
.select_related("course", "course__page")
.all()
)

Expand Down Expand Up @@ -436,7 +436,7 @@ class Meta:
readable_id = models.CharField(
max_length=255, unique=True, validators=[validate_url_path_field]
)
live = models.BooleanField(default=False, db_index=True)
live = models.BooleanField(default=False)
departments = models.ManyToManyField(Department, blank=True)
flexible_prices = GenericRelation(
"flexiblepricing.FlexiblePrice",
Expand All @@ -449,7 +449,7 @@ class Meta:
content_type_field="courseware_content_type",
)

@cached_property
@property
def course_number(self):
"""
Returns:
Expand Down Expand Up @@ -523,7 +523,6 @@ def programs(self):
ProgramRequirement.objects.filter(
node_type=ProgramRequirementNodeType.COURSE, course=self
)
.all()
.distinct("program_id")
.order_by("program_id")
.values_list("program_id", flat=True)
Expand Down Expand Up @@ -576,7 +575,7 @@ class CourseRun(TimestampedModel):

objects = CourseRunQuerySet.as_manager()
course = models.ForeignKey(
Course, on_delete=models.CASCADE, related_name="courseruns", db_index=True
Course, on_delete=models.CASCADE, related_name="courseruns"
)
# product = GenericRelation(Product, related_query_name="course_run")
title = models.CharField(
Expand Down Expand Up @@ -634,9 +633,7 @@ class CourseRun(TimestampedModel):

live = models.BooleanField(default=False)
is_self_paced = models.BooleanField(default=False)
products = GenericRelation(
"ecommerce.Product", related_query_name="courserunproducts"
)
products = GenericRelation("ecommerce.Product", related_query_name="courseruns")

class Meta:
unique_together = ("course", "run_tag")
Expand Down
Loading

0 comments on commit b506f4a

Please sign in to comment.