Commit 0a7fdb31 by Ahsan Ulhaq

Remove history usage from course

This reverts commit 51e794926c354a938c7353594530548e86b1d712.
parent 8b9a953e
from __future__ import unicode_literals
import logging
from django.core.exceptions import ObjectDoesNotExist
from django.core.management.base import BaseCommand
from django.db import transaction
from ecommerce.courses.models import Course
logger = logging.getLogger(__name__)
class Command(BaseCommand):
help = 'Sync course history data with course created modified date.'
def handle(self, *args, **options):
courses = Course.objects.all()
with transaction.atomic():
for course in courses:
try:
course.created = course.history.earliest().history_date
course.modified = course.history.latest().history_date
course.save()
except ObjectDoesNotExist:
logger.warning(
'History object for course with course_id: %s does not exist',
course.id
)
course.created = None
course.modified = None
course.save()
# -*- coding: utf-8 -*-
# Generated by Django 1.10.7 on 2017-12-04 10:36
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('courses', '0005_auto_20170525_0131'),
]
operations = [
migrations.AddField(
model_name='course',
name='created',
field=models.DateTimeField(auto_now_add=True, null=True),
),
migrations.AddField(
model_name='course',
name='modified',
field=models.DateTimeField(auto_now=True, null=True),
),
migrations.AddField(
model_name='historicalcourse',
name='created',
field=models.DateTimeField(blank=True, editable=False, null=True),
),
migrations.AddField(
model_name='historicalcourse',
name='modified',
field=models.DateTimeField(blank=True, editable=False, null=True),
),
]
...@@ -40,6 +40,8 @@ class Course(models.Model): ...@@ -40,6 +40,8 @@ class Course(models.Model):
help_text=_('Last date/time on which verification for this product can be submitted.') help_text=_('Last date/time on which verification for this product can be submitted.')
) )
history = HistoricalRecords() history = HistoricalRecords()
created = models.DateTimeField(null=True, auto_now_add=True)
modified = models.DateTimeField(null=True, auto_now=True)
thumbnail_url = models.URLField(null=True, blank=True) thumbnail_url = models.URLField(null=True, blank=True)
def __unicode__(self): def __unicode__(self):
......
import datetime
from django.core.management import call_command
from testfixtures import LogCapture
from ecommerce.courses.models import Course
from ecommerce.courses.tests.factories import CourseFactory
from ecommerce.tests.testcases import TestCase
LOGGER_NAME = 'ecommerce.courses.management.commands.sync_history_with_course'
class SyncHistoryTests(TestCase):
def setUp(self):
super(SyncHistoryTests, self).setUp()
self.course = CourseFactory(id='edx/Demo_Course/DemoX', site=self.site)
def test_history_not_exist(self):
self.course.history.all().delete()
with LogCapture(LOGGER_NAME) as log:
call_command('sync_history_with_course')
log.check(
(
LOGGER_NAME,
'WARNING',
'History object for course with course_id: edx/Demo_Course/DemoX does not exist'
)
)
def test_sync_history_data(self):
self.course.created = self.course.created + datetime.timedelta(days=1)
self.course.save()
call_command('sync_history_with_course')
course = Course.objects.get(id='edx/Demo_Course/DemoX')
self.assertEqual(course.created, self.course.history.earliest().history_date)
...@@ -7,7 +7,6 @@ from decimal import Decimal ...@@ -7,7 +7,6 @@ from decimal import Decimal
import waffle import waffle
from dateutil.parser import parse from dateutil.parser import parse
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from django.core.exceptions import ObjectDoesNotExist
from django.db import transaction from django.db import transaction
from django.utils import timezone from django.utils import timezone
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
...@@ -298,10 +297,7 @@ class CourseSerializer(serializers.HyperlinkedModelSerializer): ...@@ -298,10 +297,7 @@ class CourseSerializer(serializers.HyperlinkedModelSerializer):
self.fields.pop('products', None) self.fields.pop('products', None)
def get_last_edited(self, obj): def get_last_edited(self, obj):
try: return obj.modified.strftime(ISO_8601_FORMAT) if obj.modified else None
return obj.history.latest().history_date.strftime(ISO_8601_FORMAT)
except ObjectDoesNotExist:
return None
def get_products_url(self, obj): def get_products_url(self, obj):
return reverse('api:v2:course-product-list', kwargs={'parent_lookup_course_id': obj.id}, return reverse('api:v2:course-product-list', kwargs={'parent_lookup_course_id': obj.id},
......
...@@ -6,7 +6,6 @@ import jwt ...@@ -6,7 +6,6 @@ import jwt
import mock import mock
from django.conf import settings from django.conf import settings
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from django.core.exceptions import ObjectDoesNotExist
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from oscar.core.loading import get_class, get_model from oscar.core.loading import get_class, get_model
...@@ -42,12 +41,7 @@ class CourseViewSetTests(ProductSerializerMixin, DiscoveryTestMixin, TestCase): ...@@ -42,12 +41,7 @@ class CourseViewSetTests(ProductSerializerMixin, DiscoveryTestMixin, TestCase):
products_url = self.get_full_url(reverse('api:v2:course-product-list', products_url = self.get_full_url(reverse('api:v2:course-product-list',
kwargs={'parent_lookup_course_id': course.id})) kwargs={'parent_lookup_course_id': course.id}))
last_edited = None last_edited = course.modified.strftime(ISO_8601_FORMAT)
try:
last_edited = course.history.latest().history_date.strftime(ISO_8601_FORMAT)
except ObjectDoesNotExist:
pass
enrollment_code = course.enrollment_code_product enrollment_code = course.enrollment_code_product
data = { data = {
...@@ -115,14 +109,6 @@ class CourseViewSetTests(ProductSerializerMixin, DiscoveryTestMixin, TestCase): ...@@ -115,14 +109,6 @@ class CourseViewSetTests(ProductSerializerMixin, DiscoveryTestMixin, TestCase):
response = self.client.get(self.list_path) response = self.client.get(self.list_path)
self.assertDictEqual(json.loads(response.content), {'count': 0, 'next': None, 'previous': None, 'results': []}) self.assertDictEqual(json.loads(response.content), {'count': 0, 'next': None, 'previous': None, 'results': []})
def test_list_without_history(self):
course = Course.objects.all()[0]
course.history.all().delete()
response = self.client.get(self.list_path)
self.assertEqual(response.status_code, 200)
self.assertListEqual(json.loads(response.content)['results'], [self.serialize_course(self.course)])
def test_create(self): def test_create(self):
""" Verify the view can create a new Course.""" """ Verify the view can create a new Course."""
Course.objects.all().delete() Course.objects.all().delete()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment