Skip to content

Commit

Permalink
[BD] Modernize analytics_data_api/v0 (openedx-unsupported#301)
Browse files Browse the repository at this point in the history
Modernize analytics_data_api/v0 with the following commands:

* python-modernize -w analytics_data_api/v0
* isort -rc analytics_data_api/v0

Then they were solved (or disabled) the quality errors (disabled duplicate-code in pylint)
  • Loading branch information
morenol authored Mar 4, 2020
1 parent 4798dad commit c52adef
Show file tree
Hide file tree
Showing 37 changed files with 213 additions and 135 deletions.
2 changes: 2 additions & 0 deletions analytics_data_api/v0/apps.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import absolute_import

from django.apps import AppConfig
from django.conf import settings
from elasticsearch_dsl import connections
Expand Down
5 changes: 4 additions & 1 deletion analytics_data_api/v0/connections.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from __future__ import absolute_import

import json
import time

import six
from boto.connection import AWSAuthConnection
from elasticsearch import Connection

Expand Down Expand Up @@ -37,7 +40,7 @@ def perform_request(self, method, url, params=None, body=None, timeout=None, ign
and the default is 70 seconds.
See: https://github.com/boto/boto/blob/develop/boto/connection.py#L533
"""
if not isinstance(body, basestring):
if not isinstance(body, six.string_types):
body = json.dumps(body)
start = time.time()
response = self.connection.make_request(method, url, params=params, data=body)
Expand Down
8 changes: 5 additions & 3 deletions analytics_data_api/v0/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from __future__ import absolute_import

import abc

import six


class BaseError(Exception):
class BaseError(six.with_metaclass(abc.ABCMeta, Exception)):
"""
Base error.
"""

__metaclass__ = abc.ABCMeta

message = None

def __str__(self):
Expand Down
10 changes: 6 additions & 4 deletions analytics_data_api/v0/middleware.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
from __future__ import absolute_import

import abc

import six
from django.http.response import JsonResponse
from rest_framework import status

from analytics_data_api.v0.exceptions import (
CannotCreateReportDownloadLinkError,
CourseKeyMalformedError,
CourseNotSpecifiedError,
LearnerEngagementTimelineNotFoundError,
LearnerNotFoundError,
ParameterValueError,
ReportFileNotFoundError,
CannotCreateReportDownloadLinkError,
)


class BaseProcessErrorMiddleware(object):
class BaseProcessErrorMiddleware(six.with_metaclass(abc.ABCMeta, object)):
"""
Base error.
"""

__metaclass__ = abc.ABCMeta

@abc.abstractproperty
def error(self):
""" Error class to catch. """
Expand Down
4 changes: 3 additions & 1 deletion analytics_data_api/v0/models.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from __future__ import absolute_import

import datetime
from itertools import groupby

from django.conf import settings
from django.db import models
from django.db.models import Count, Sum, When, IntegerField, Case, Max
from django.db.models import Case, Count, IntegerField, Max, Sum, When
from django.utils.timezone import now
# some fields (e.g. Float, Integer) are dynamic and your IDE may highlight them as unavailable
from elasticsearch_dsl import Date, DocType, Float, Integer, Q, String # pylint: disable=no-name-in-module
Expand Down
14 changes: 7 additions & 7 deletions analytics_data_api/v0/serializers.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
from __future__ import absolute_import

from collections import OrderedDict
from urlparse import urljoin

import six
from django.conf import settings
from rest_framework import pagination, serializers
from rest_framework.response import Response
from six.moves.urllib.parse import urljoin # pylint: disable=import-error,ungrouped-imports

from analytics_data_api.constants import (
engagement_events,
enrollment_modes,
)
from analytics_data_api.constants import engagement_events, enrollment_modes
from analytics_data_api.v0 import models


# Below are the enrollment modes supported by this API.
ENROLLMENT_MODES = [enrollment_modes.AUDIT, enrollment_modes.CREDIT, enrollment_modes.HONOR,
enrollment_modes.PROFESSIONAL, enrollment_modes.VERIFIED, enrollment_modes.MASTERS]
Expand Down Expand Up @@ -369,7 +369,7 @@ def get_segments(self, obj):
# using hasattr() instead because DocType.get() is overloaded and makes a request
if hasattr(obj, 'segments'):
# json parsing will fail unless in unicode
return [unicode(segment) for segment in obj.segments]
return [six.text_type(segment) for segment in obj.segments]
else:
return []

Expand Down
4 changes: 3 additions & 1 deletion analytics_data_api/v0/tests/test_connections.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from __future__ import absolute_import

import socket

from django.test import TestCase
from elasticsearch.exceptions import ElasticsearchException
from mock import patch

from analytics_data_api.v0.connections import BotoHttpConnection, ESConnection
from mock import patch


class ESConnectionTests(TestCase):
Expand Down
6 changes: 4 additions & 2 deletions analytics_data_api/v0/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from __future__ import absolute_import

from django.test import TestCase
from django_dynamic_fixture import G

from analytics_data_api.v0 import models
from analytics_data_api.constants.country import UNKNOWN_COUNTRY, get_country
from analytics_data_api.v0 import models
from django_dynamic_fixture import G


class CourseEnrollmentByCountryTests(TestCase):
Expand Down
14 changes: 9 additions & 5 deletions analytics_data_api/v0/tests/test_serializers.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from __future__ import absolute_import

from datetime import date

from django.test import TestCase
from django_dynamic_fixture import G

from analytics_data_api.v0 import models as api_models, serializers as api_serializers
from analytics_data_api.v0 import models as api_models
from analytics_data_api.v0 import serializers as api_serializers
from django_dynamic_fixture import G


class TestSerializer(api_serializers.CourseEnrollmentDailySerializer, api_serializers.DynamicFieldsModelSerializer):
Expand All @@ -14,14 +18,14 @@ def test_fields(self):
now = date.today()
instance = G(api_models.CourseEnrollmentDaily, course_id='1', count=1, date=now)
serialized = TestSerializer(instance)
self.assertListEqual(serialized.data.keys(), ['course_id', 'date', 'count', 'created'])
self.assertListEqual(list(serialized.data.keys()), ['course_id', 'date', 'count', 'created'])

instance = G(api_models.CourseEnrollmentDaily, course_id='2', count=1, date=now)
serialized = TestSerializer(instance, fields=('course_id',))
self.assertListEqual(serialized.data.keys(), ['course_id'])
self.assertListEqual(list(serialized.data.keys()), ['course_id'])

def test_exclude(self):
now = date.today()
instance = G(api_models.CourseEnrollmentDaily, course_id='3', count=1, date=now)
serialized = TestSerializer(instance, exclude=('course_id',))
self.assertListEqual(serialized.data.keys(), ['date', 'count', 'created'])
self.assertListEqual(list(serialized.data.keys()), ['date', 'count', 'created'])
2 changes: 2 additions & 0 deletions analytics_data_api/v0/tests/test_urls.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import absolute_import

from django.core.urlresolvers import reverse
from django.test import TestCase

Expand Down
7 changes: 5 additions & 2 deletions analytics_data_api/v0/tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from __future__ import absolute_import

import collections
import datetime

import pytz

from django_dynamic_fixture import G
from analytics_data_api.v0 import models
from django_dynamic_fixture import G


def flatten(dictionary, parent_key='', sep='.'):
Expand All @@ -16,7 +19,7 @@ def flatten(dictionary, parent_key='', sep='.'):
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())
items.extend(list(flatten(value, new_key).items()))
else:
items.append((new_key, value))
return dict(items)
Expand Down
12 changes: 8 additions & 4 deletions analytics_data_api/v0/tests/views/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
from __future__ import absolute_import

import csv
import json
import StringIO
from collections import OrderedDict
from urllib import urlencode

from django_dynamic_fixture import G
import six
from rest_framework import status
from six.moves import map, zip # pylint: disable=ungrouped-imports
from six.moves.urllib.parse import urlencode # pylint: disable=import-error

from analytics_data_api.v0.tests.utils import flatten
from django_dynamic_fixture import G


class CourseSamples(object):
Expand Down Expand Up @@ -58,12 +62,12 @@ def assertCsvResponseIsValid(self, response, expected_filename, expected_data=No

# Validate other response headers
if expected_headers:
for header_name, header_content in expected_headers.iteritems():
for header_name, header_content in six.iteritems(expected_headers):
self.assertEquals(response.get(header_name), header_content)

# Validate the content data
if expected_data:
data = map(flatten, expected_data)
data = list(map(flatten, expected_data))

# The CSV renderer sorts the headers alphabetically
fieldnames = sorted(data[0].keys())
Expand Down
9 changes: 5 additions & 4 deletions analytics_data_api/v0/tests/views/test_course_summaries.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
from __future__ import absolute_import

import datetime

import ddt
from django_dynamic_fixture import G
import pytz

from django.conf import settings

import ddt
from analytics_data_api.constants import enrollment_modes
from analytics_data_api.v0 import models, serializers
from analytics_data_api.v0.tests.views import CourseSamples, VerifyCourseIdMixin, APIListViewTestMixin
from analytics_data_api.v0.tests.views import APIListViewTestMixin, CourseSamples, VerifyCourseIdMixin
from analyticsdataserver.tests import TestCaseWithAuthentication
from django_dynamic_fixture import G


@ddt.ddt
Expand Down
Loading

0 comments on commit c52adef

Please sign in to comment.