Commit f868623a by Clinton Blackburn

Added CSV Support

Change-Id: I364eadff8cf5ce4598895923c3770defcef2fc5d
parent acefe9fc
...@@ -53,4 +53,4 @@ loaddata: syncdb ...@@ -53,4 +53,4 @@ loaddata: syncdb
python manage.py loaddata courses education_levels single_course_activity course_enrollment_birth_year course_enrollment_education course_enrollment_gender problem_response_answer_distribution course_enrollment_daily countries course_enrollment_country --database=analytics python manage.py loaddata courses education_levels single_course_activity course_enrollment_birth_year course_enrollment_education course_enrollment_gender problem_response_answer_distribution course_enrollment_daily countries course_enrollment_country --database=analytics
demo: clean requirements loaddata demo: clean requirements loaddata
python manage.py set_api_key analytics analytics python manage.py set_api_key edx edx
...@@ -31,7 +31,7 @@ class CourseActivityByWeek(models.Model): ...@@ -31,7 +31,7 @@ class CourseActivityByWeek(models.Model):
class BaseCourseEnrollment(models.Model): class BaseCourseEnrollment(models.Model):
course = models.ForeignKey(Course, null=False) course = models.ForeignKey(Course, null=False)
date = models.DateField(null=False) date = models.DateField(null=False, db_index=True)
count = models.IntegerField(null=False) count = models.IntegerField(null=False)
class Meta(object): class Meta(object):
......
import collections
def flatten(dictionary, parent_key='', sep='.'):
"""
Flatten dictionary
http://stackoverflow.com/a/6027615
"""
items = []
for key, value in dictionary.items():
new_key = parent_key + sep + key if parent_key else key
if isinstance(value, collections.MutableMapping):
items.extend(flatten(value, new_key).items())
else:
items.append((new_key, value))
return dict(items)
"""Common settings and globals.""" """Common settings and globals."""
from os.path import abspath, basename, dirname, join, normpath from os.path import abspath, basename, dirname, join, normpath
from sys import stderr from sys import stderr
...@@ -250,6 +249,11 @@ REST_FRAMEWORK = { ...@@ -250,6 +249,11 @@ REST_FRAMEWORK = {
# For the browseable API # For the browseable API
'rest_framework.authentication.SessionAuthentication', 'rest_framework.authentication.SessionAuthentication',
), ),
'DEFAULT_RENDERER_CLASSES': (
'rest_framework.renderers.JSONRenderer',
'rest_framework.renderers.BrowsableAPIRenderer',
'rest_framework_csv.renderers.CSVRenderer',
)
} }
########## END REST FRAMEWORK CONFIGURATION ########## END REST FRAMEWORK CONFIGURATION
......
...@@ -85,5 +85,5 @@ ENABLE_ADMIN_SITE = True ...@@ -85,5 +85,5 @@ ENABLE_ADMIN_SITE = True
TEST_RUNNER = 'django_nose.NoseTestSuiteRunner' TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'
SWAGGER_SETTINGS = { SWAGGER_SETTINGS = {
'api_key': 'analytics' 'api_key': 'edx'
} }
from contextlib import contextmanager from contextlib import contextmanager
from functools import partial
from django.conf import settings from django.conf import settings
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.db.utils import ConnectionHandler, DatabaseError from django.db.utils import ConnectionHandler, DatabaseError
from django.test import TestCase from django.test import TestCase
from django.test.utils import override_settings from django.test.utils import override_settings
from mock import patch, Mock
import mock import mock
from rest_framework.authtoken.models import Token from rest_framework.authtoken.models import Token
...@@ -15,13 +14,16 @@ class TestCaseWithAuthentication(TestCase): ...@@ -15,13 +14,16 @@ class TestCaseWithAuthentication(TestCase):
def setUp(self): def setUp(self):
super(TestCaseWithAuthentication, self).setUp() super(TestCaseWithAuthentication, self).setUp()
test_user = User.objects.create_user('tester', 'test@example.com', 'testpassword') test_user = User.objects.create_user('tester', 'test@example.com', 'testpassword')
token = Token.objects.create(user=test_user) self.token = Token.objects.create(user=test_user)
self.authenticated_get = partial(self.client.get, HTTP_AUTHORIZATION='Token ' + token.key, follow=True)
def authenticated_get(self, path, data=None, follow=True, **extra):
data = data or {}
return self.client.get(path, data, follow, HTTP_AUTHORIZATION='Token ' + self.token.key, **extra)
@contextmanager @contextmanager
def no_database(): def no_database():
cursor_mock = Mock(side_effect=DatabaseError) cursor_mock = mock.Mock(side_effect=DatabaseError)
with mock.patch('django.db.backends.util.CursorWrapper', cursor_mock): with mock.patch('django.db.backends.util.CursorWrapper', cursor_mock):
yield yield
...@@ -58,7 +60,7 @@ class OperationalEndpointsTest(TestCaseWithAuthentication): ...@@ -58,7 +60,7 @@ class OperationalEndpointsTest(TestCaseWithAuthentication):
@staticmethod @staticmethod
@contextmanager @contextmanager
def override_database_connections(databases): def override_database_connections(databases):
with patch('analyticsdataserver.views.connections', ConnectionHandler(databases)): with mock.patch('analyticsdataserver.views.connections', ConnectionHandler(databases)):
yield yield
@override_settings(ANALYTICS_DATABASE='reporting') @override_settings(ANALYTICS_DATABASE='reporting')
......
...@@ -5,3 +5,4 @@ django-model-utils==1.4.0 ...@@ -5,3 +5,4 @@ django-model-utils==1.4.0
djangorestframework==2.3.5 djangorestframework==2.3.5
ipython==2.1.0 ipython==2.1.0
django-rest-swagger==0.1.14 django-rest-swagger==0.1.14
djangorestframework-csv==1.3.3
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