Skip to content

Commit

Permalink
fix: refactored code
Browse files Browse the repository at this point in the history
  • Loading branch information
jawad-khan committed Aug 1, 2023
1 parent 5f9b9c2 commit 8fee8bf
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
5 changes: 2 additions & 3 deletions course_discovery/apps/course_metadata/data_loaders/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
)
from course_discovery.apps.course_metadata.toggles import BYPASS_LMS_DATA_LOADER__END_DATE_UPDATED_CHECK
from course_discovery.apps.course_metadata.utils import push_to_ecommerce_for_course_run, subtract_deadline_delta
from course_discovery.apps.core.utils import update_instance

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -611,9 +612,7 @@ def update_seat(self, course_run, product_body):
logger.info('Created seat for course with key [%s] and sku [%s].', course_run.key, sku)
else:
for seat in seats:
for key, value in defaults.items():
setattr(seat, key, value)
seat.save()
update_instance(seat, defaults, should_commit=True)

def validate_stockrecord(self, stockrecords, title, product_class):
"""
Expand Down
5 changes: 2 additions & 3 deletions course_discovery/apps/course_metadata/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
UploadToFieldNamePath, clean_query, custom_render_variations, push_to_ecommerce_for_course_run,
push_tracks_to_lms_for_course_run, set_official_state, subtract_deadline_delta
)
from course_discovery.apps.core.utils import update_instance
from course_discovery.apps.ietf_language_tags.models import LanguageTag
from course_discovery.apps.publisher.utils import VALID_CHARS_IN_COURSE_NUM_AND_ORG_KEY

Expand Down Expand Up @@ -2480,9 +2481,7 @@ def update_or_create_seat_helper(self, seat_type, prices, upgrade_deadline_overr
seats = [Seat.everything.create(course_run=self, type=seat_type, draft=True, **defaults)]
else:
for seat in seats:
for key, value in defaults.items():
setattr(seat, key, value)
seat.save()
update_instance(seat, defaults, should_commit=True)

return seats

Expand Down
22 changes: 18 additions & 4 deletions course_discovery/apps/course_metadata/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -743,12 +743,14 @@ def test_enrollable_seats(self):
honor_seat_type = factories.SeatTypeFactory.honor()
verified_seat = factories.SeatFactory(course_run=course_run, type=verified_seat_type, upgrade_deadline=None)
second_verified_seat = factories.SeatFactory(course_run=course_run, type=verified_seat_type,
upgrade_deadline=None)
upgrade_deadline=None, price=verified_seat.price)
verified_seat_with_diff_price = factories.SeatFactory(course_run=course_run, type=verified_seat_type,
upgrade_deadline=None, price=verified_seat.price + 10)
professional_seat = factories.SeatFactory(course_run=course_run, type=professional_seat_type,
upgrade_deadline=None)
honor_seat = factories.SeatFactory(course_run=course_run, type=honor_seat_type, upgrade_deadline=None)
assert course_run.enrollable_seats([verified_seat_type, professional_seat_type]) == \
[verified_seat, second_verified_seat, professional_seat]
[verified_seat, second_verified_seat, professional_seat, verified_seat_with_diff_price]

# The method should not care about the course run's start date.
course_run.start = datetime.datetime.now(pytz.UTC) + datetime.timedelta(days=1)
Expand Down Expand Up @@ -1573,14 +1575,24 @@ def test_verified_seat_upgrade_deadline_override(self):
draft=True,
type=verified_type,
upgrade_deadline=upgrade_deadline,
sku='123'
sku='123',
price=100
)
factories.SeatFactory(
course_run=self.course_run,
draft=True,
type=verified_type,
upgrade_deadline=upgrade_deadline,
sku='000',
price=100
)
factories.SeatFactory(
course_run=self.course_run,
draft=True,
type=verified_type,
upgrade_deadline=upgrade_deadline,
sku='000'
sku='111',
price=200
)

factories.CourseEntitlementFactory(course=self.course_run.course, mode=verified_type, draft=True)
Expand All @@ -1592,12 +1604,14 @@ def test_verified_seat_upgrade_deadline_override(self):

self._change_upgrade_deadline_on_draft_seat('123', official_run, verified_type, upgrade_deadline, new_deadline)
self._change_upgrade_deadline_on_draft_seat('000', official_run, verified_type, upgrade_deadline, new_deadline)
self._change_upgrade_deadline_on_draft_seat('111', official_run, verified_type, upgrade_deadline, new_deadline)

draft_run = CourseRun.everything.get(key=self.course_run.key, draft=True)
draft_run.update_or_create_official_version()

self._assert_verified_seat_deadline('123', new_deadline, draft_run, official_run, verified_type)
self._assert_verified_seat_deadline('000', new_deadline, draft_run, official_run, verified_type)
self._assert_verified_seat_deadline('111', new_deadline, draft_run, official_run, verified_type)

def _change_upgrade_deadline_on_draft_seat(self, sku, official_run, verified_type, upgrade_deadline, new_deadline):
draft_seat = Seat.everything.get(course_run=self.course_run, draft=True, type=verified_type, sku=sku)
Expand Down

0 comments on commit 8fee8bf

Please sign in to comment.