add CourseTalk widget

Move tests to one test class
This commit is contained in:
erm0l0v
2015-05-05 15:46:06 +03:00
parent 91fd6dbea2
commit 4142438372
15 changed files with 213 additions and 7 deletions

View File

@@ -0,0 +1,8 @@
"""Manage coursetalk configuration. """
from django.contrib import admin
from config_models.admin import ConfigurationModelAdmin
from openedx.core.djangoapps.coursetalk.models import CourseTalkWidgetConfiguration
admin.site.register(CourseTalkWidgetConfiguration, ConfigurationModelAdmin)

View File

@@ -0,0 +1,35 @@
"""
CourseTalk widget helpers
"""
from __future__ import unicode_literals
from openedx.core.djangoapps.coursetalk import models
def get_coursetalk_course_key(course_key):
"""
Return course key for coursetalk widget
CourseTalk unique key for a course contains only organization and course code.
:param course_key: SlashSeparatedCourseKey instance
:type course_key: SlashSeparatedCourseKey
:return: CourseTalk course key
:rtype: str
"""
return '{0.org}_{0.course}'.format(course_key)
def inject_coursetalk_keys_into_context(context, course_key):
"""
Set params to view context based on course_key and CourseTalkWidgetConfiguration
:param context: view context
:type context: dict
:param course_key: SlashSeparatedCourseKey instance
:type course_key: SlashSeparatedCourseKey
"""
show_coursetalk_widget = models.CourseTalkWidgetConfiguration.is_enabled()
if show_coursetalk_widget:
context['show_coursetalk_widget'] = True
context['platform_key'] = models.CourseTalkWidgetConfiguration.get_platform_key()
context['course_review_key'] = get_coursetalk_course_key(course_key)

View File

@@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
import django.db.models.deletion
from django.conf import settings
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name='CourseTalkWidgetConfiguration',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('change_date', models.DateTimeField(auto_now_add=True, verbose_name='Change date')),
('enabled', models.BooleanField(default=False, verbose_name='Enabled')),
('platform_key', models.CharField(help_text="This key needs to associate CourseTalk reviews with your platform. Better to use domain name Ex: for 'http://edx.org' platform_key will be 'edx'", max_length=50)),
('changed_by', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, editable=False, to=settings.AUTH_USER_MODEL, null=True, verbose_name='Changed by')),
],
options={
'ordering': ('-change_date',),
'abstract': False,
},
),
]

View File

@@ -0,0 +1,39 @@
"""
Models for CourseTalk configurations
"""
from __future__ import unicode_literals
from django.db import models
from django.utils.translation import ugettext_lazy as _
from config_models.models import ConfigurationModel
class CourseTalkWidgetConfiguration(ConfigurationModel):
"""
This model represents Enable Configuration for CourseTalk widget.
If the setting enabled, widget will will be available on course
info page and on course about page.
"""
platform_key = models.fields.CharField(
max_length=50,
help_text=_(
"The platform key associates CourseTalk widgets with your platform. "
"Generally, it is the domain name for your platform. For example, "
"if your platform is http://edx.org, the platform key is \"edx\"."
)
)
@classmethod
def get_platform_key(cls):
"""
Return platform_key for current active configuration.
If current configuration is not enabled - return empty string
:return: Platform key
:rtype: unicode
"""
return cls.current().platform_key if cls.is_enabled() else ''
def __unicode__(self):
return 'CourseTalkWidgetConfiguration - {0}'.format(self.enabled)

View File

@@ -0,0 +1,58 @@
""" CourseTalk widget helpers tests """
from __future__ import unicode_literals
from unittest import skipUnless
from django import test
from django.conf import settings
from opaque_keys.edx.locations import SlashSeparatedCourseKey
from openedx.core.djangoapps.coursetalk import helpers
from openedx.core.djangoapps.coursetalk import models
@skipUnless(settings.ROOT_URLCONF == 'lms.urls', 'Tests only valid in LMS')
class CourseTalkKeyTests(test.TestCase):
"""
CourseTalkKeyTests:
tests for function get_coursetalk_course_key
tests for function inject_coursetalk_keys_into_context
"""
PLATFORM_KEY = 'some_platform'
def setUp(self):
super(CourseTalkKeyTests, self).setUp()
self.course_key = SlashSeparatedCourseKey('org', 'course', 'run')
self.context = {}
def db_set_up(self, enabled):
"""
Setup database for this test:
Create CourseTalkWidgetConfiguration
"""
config = models.CourseTalkWidgetConfiguration.current()
config.enabled = enabled
config.platform_key = self.PLATFORM_KEY
config.save()
def test_simple_key(self):
coursetalk_course_key = helpers.get_coursetalk_course_key(self.course_key)
self.assertEqual(coursetalk_course_key, 'org_course')
def test_inject_coursetalk_keys_when_widget_not_enabled(self):
self.db_set_up(False)
helpers.inject_coursetalk_keys_into_context(self.context, self.course_key)
self.assertNotIn('show_coursetalk_widget', self.context)
self.assertNotIn('platform_key', self.context)
self.assertNotIn('course_review_key', self.context)
def test_inject_coursetalk_keys_when_widget_enabled(self):
self.db_set_up(True)
helpers.inject_coursetalk_keys_into_context(self.context, self.course_key)
self.assertIn('show_coursetalk_widget', self.context)
self.assertIn('platform_key', self.context)
self.assertIn('course_review_key', self.context)
self.assertEqual(self.context.get('show_coursetalk_widget'), True)
self.assertEqual(self.context.get('platform_key'), self.PLATFORM_KEY)
self.assertEqual(self.context.get('course_review_key'), 'org_course')