From 920ce74877177a12029fc547da42ea932efeac28 Mon Sep 17 00:00:00 2001 From: Evans Dianga <evansdianga@gmail.com> Date: Thu, 23 May 2019 20:11:57 +0300 Subject: [PATCH] Modernize INCR-210 user_api/management (#20435) --- .../commands/bulk_user_org_email_optout.py | 6 +++-- .../cancel_user_retirement_request.py | 4 ++-- .../commands/create_user_gdpr_testing.py | 24 ++++++++----------- .../management/commands/email_opt_in_list.py | 5 ++-- .../commands/migrate_user_profile_langs.py | 5 ++-- .../commands/populate_retirement_states.py | 3 +-- .../commands/sync_hubspot_contacts.py | 7 +++--- .../tests/test_bulk_user_org_email_optout.py | 4 +++- .../tests/test_cancel_retirement.py | 2 ++ .../tests/test_email_opt_in_list.py | 20 +++++++++------- .../tests/test_populate_retirement_states.py | 8 ++++--- .../tests/test_sync_hubspot_contacts.py | 7 ++++-- 12 files changed, 52 insertions(+), 43 deletions(-) diff --git a/openedx/core/djangoapps/user_api/management/commands/bulk_user_org_email_optout.py b/openedx/core/djangoapps/user_api/management/commands/bulk_user_org_email_optout.py index 0d474652c45..8203443341a 100644 --- a/openedx/core/djangoapps/user_api/management/commands/bulk_user_org_email_optout.py +++ b/openedx/core/djangoapps/user_api/management/commands/bulk_user_org_email_optout.py @@ -16,14 +16,16 @@ If the user/org combo does not currently exist in the table, a row will be creat will be have the 'email-optin' tag set to 'False'. """ -from __future__ import print_function +from __future__ import absolute_import, print_function + import csv import logging import time +from django.core.management.base import BaseCommand, CommandError from django.db import connections from django.db.utils import DatabaseError -from django.core.management.base import BaseCommand, CommandError +from six.moves import range log = logging.getLogger(__name__) diff --git a/openedx/core/djangoapps/user_api/management/commands/cancel_user_retirement_request.py b/openedx/core/djangoapps/user_api/management/commands/cancel_user_retirement_request.py index b43323f2307..fb83464d604 100644 --- a/openedx/core/djangoapps/user_api/management/commands/cancel_user_retirement_request.py +++ b/openedx/core/djangoapps/user_api/management/commands/cancel_user_retirement_request.py @@ -3,15 +3,15 @@ Use this mgmt command when a user requests retirement mistakenly, then requests for the retirement request to be cancelled. The command can't cancel a retirement that has already commenced - only pending retirements. """ -from __future__ import print_function +from __future__ import absolute_import, print_function import logging from django.core.management.base import BaseCommand, CommandError + from openedx.core.djangoapps.user_api.accounts.utils import generate_password from openedx.core.djangoapps.user_api.models import UserRetirementStatus - LOGGER = logging.getLogger(__name__) diff --git a/openedx/core/djangoapps/user_api/management/commands/create_user_gdpr_testing.py b/openedx/core/djangoapps/user_api/management/commands/create_user_gdpr_testing.py index 1abd379edf4..ff9f4145b3a 100644 --- a/openedx/core/djangoapps/user_api/management/commands/create_user_gdpr_testing.py +++ b/openedx/core/djangoapps/user_api/management/commands/create_user_gdpr_testing.py @@ -4,34 +4,30 @@ Enrolls the user in the DemoX course. Optionally takes in username, email, and course UUID arguments. """ -from __future__ import unicode_literals +from __future__ import absolute_import, unicode_literals from datetime import datetime from uuid import uuid4 -from pytz import UTC -from django.core.management.base import BaseCommand -from django.contrib.auth.models import User from consent.models import DataSharingConsent +from django.contrib.auth.models import User +from django.core.management.base import BaseCommand from enterprise.models import ( EnterpriseCourseEnrollment, EnterpriseCustomer, EnterpriseCustomerUser, - PendingEnterpriseCustomerUser, + PendingEnterpriseCustomerUser ) -from entitlements.models import CourseEntitlement, CourseEntitlementSupportDetail from integrated_channels.sap_success_factors.models import SapSuccessFactorsLearnerDataTransmissionAudit -from lms.djangoapps.verify_student.models import SoftwareSecurePhotoVerification from opaque_keys.edx.keys import CourseKey -from openedx.core.djangoapps.course_groups.models import UnregisteredLearnerCohortAssignments, CourseUserGroup +from pytz import UTC + +from entitlements.models import CourseEntitlement, CourseEntitlementSupportDetail +from lms.djangoapps.verify_student.models import SoftwareSecurePhotoVerification +from openedx.core.djangoapps.course_groups.models import CourseUserGroup, UnregisteredLearnerCohortAssignments from openedx.core.djangoapps.profile_images.images import create_profile_images from openedx.core.djangoapps.profile_images.tests.helpers import make_image_file -from student.models import ( - CourseEnrollment, - PendingEmailChange, - UserProfile, - CourseEnrollmentAllowed -) +from student.models import CourseEnrollment, CourseEnrollmentAllowed, PendingEmailChange, UserProfile from ...models import UserOrgTag diff --git a/openedx/core/djangoapps/user_api/management/commands/email_opt_in_list.py b/openedx/core/djangoapps/user_api/management/commands/email_opt_in_list.py index c9ed29c998d..474c91274b9 100644 --- a/openedx/core/djangoapps/user_api/management/commands/email_opt_in_list.py +++ b/openedx/core/djangoapps/user_api/management/commands/email_opt_in_list.py @@ -19,7 +19,7 @@ When reports are generated, we need to handle: The command will always use the read replica database if one is configured. """ -from __future__ import unicode_literals +from __future__ import absolute_import, unicode_literals import contextlib import csv @@ -34,6 +34,7 @@ from django.db import connections from django.utils import timezone from opaque_keys.edx.keys import CourseKey from six import text_type +from six.moves import range from xmodule.modulestore.django import modulestore @@ -43,7 +44,7 @@ LOGGER = logging.getLogger(__name__) def chunks(sequence, chunk_size): - return (sequence[index: index + chunk_size] for index in xrange(0, len(sequence), chunk_size)) + return (sequence[index: index + chunk_size] for index in range(0, len(sequence), chunk_size)) class Command(BaseCommand): diff --git a/openedx/core/djangoapps/user_api/management/commands/migrate_user_profile_langs.py b/openedx/core/djangoapps/user_api/management/commands/migrate_user_profile_langs.py index ea11750d579..d14fae397ea 100644 --- a/openedx/core/djangoapps/user_api/management/commands/migrate_user_profile_langs.py +++ b/openedx/core/djangoapps/user_api/management/commands/migrate_user_profile_langs.py @@ -1,19 +1,18 @@ """ Migrates user preferences from one language code to another in batches. Dark lang preferences are not affected. """ -from __future__ import print_function +from __future__ import absolute_import, print_function import logging from time import sleep from django.conf import settings from django.core.management.base import BaseCommand, CommandError -from django.db.models import Q, Max +from django.db.models import Max, Q from openedx.core.djangoapps.dark_lang.models import DarkLangConfig from openedx.core.djangoapps.user_api.models import UserPreference - DEFAULT_CHUNK_SIZE = 10000 DEFAULT_SLEEP_TIME_SECS = 10 diff --git a/openedx/core/djangoapps/user_api/management/commands/populate_retirement_states.py b/openedx/core/djangoapps/user_api/management/commands/populate_retirement_states.py index f227379a981..618c9343c89 100644 --- a/openedx/core/djangoapps/user_api/management/commands/populate_retirement_states.py +++ b/openedx/core/djangoapps/user_api/management/commands/populate_retirement_states.py @@ -9,7 +9,7 @@ need to be configurable by open source partners and modifying the with a variety of unpleasant follow-on effects for the partner when upgrading the model at a later date. """ -from __future__ import print_function +from __future__ import absolute_import, print_function import copy import logging @@ -20,7 +20,6 @@ from django.db.models import F from openedx.core.djangoapps.user_api.models import RetirementState, UserRetirementStatus - LOGGER = logging.getLogger(__name__) START_STATE = 'PENDING' diff --git a/openedx/core/djangoapps/user_api/management/commands/sync_hubspot_contacts.py b/openedx/core/djangoapps/user_api/management/commands/sync_hubspot_contacts.py index 78add32e80e..935364b2974 100644 --- a/openedx/core/djangoapps/user_api/management/commands/sync_hubspot_contacts.py +++ b/openedx/core/djangoapps/user_api/management/commands/sync_hubspot_contacts.py @@ -3,16 +3,17 @@ Management command to sync platform users with hubspot ./manage.py lms sync_hubspot_contacts ./manage.py lms sync_hubspot_contacts --initial-sync-days=7 --batch-size=20 """ +from __future__ import absolute_import + import json import time import traceback -import urlparse from datetime import datetime, timedelta +import six.moves.urllib.parse from django.contrib.auth.models import User from django.core.management.base import BaseCommand, CommandError from django.utils.html import escapejs - from edx_rest_api_client.client import EdxRestApiClient from slumber.exceptions import HttpClientError, HttpServerError @@ -137,7 +138,7 @@ class Command(BaseCommand): contacts.append(contact) api_key = site_conf.get_value('HUBSPOT_API_KEY') - client = EdxRestApiClient(urlparse.urljoin(HUBSPOT_API_BASE_URL, 'contacts/v1/contact')) + client = EdxRestApiClient(six.moves.urllib.parse.urljoin(HUBSPOT_API_BASE_URL, 'contacts/v1/contact')) try: client.batch.post(contacts, hapikey=api_key) return len(contacts) diff --git a/openedx/core/djangoapps/user_api/management/tests/test_bulk_user_org_email_optout.py b/openedx/core/djangoapps/user_api/management/tests/test_bulk_user_org_email_optout.py index b65d325cde5..2543b66527a 100644 --- a/openedx/core/djangoapps/user_api/management/tests/test_bulk_user_org_email_optout.py +++ b/openedx/core/djangoapps/user_api/management/tests/test_bulk_user_org_email_optout.py @@ -1,12 +1,14 @@ """ Test the test_bulk_user_org_email_optout management command """ +from __future__ import absolute_import + import os import tempfile from contextlib import contextmanager + import mock import pytest - from django.core.management import call_command pytestmark = pytest.mark.django_db diff --git a/openedx/core/djangoapps/user_api/management/tests/test_cancel_retirement.py b/openedx/core/djangoapps/user_api/management/tests/test_cancel_retirement.py index 8961e3f4486..e11fe8d87ec 100644 --- a/openedx/core/djangoapps/user_api/management/tests/test_cancel_retirement.py +++ b/openedx/core/djangoapps/user_api/management/tests/test_cancel_retirement.py @@ -1,6 +1,8 @@ """ Test the cancel_user_retirement_request management command """ +from __future__ import absolute_import + import pytest from django.contrib.auth.hashers import UNUSABLE_PASSWORD_PREFIX from django.contrib.auth.models import User diff --git a/openedx/core/djangoapps/user_api/management/tests/test_email_opt_in_list.py b/openedx/core/djangoapps/user_api/management/tests/test_email_opt_in_list.py index 18ff85258b3..9a64a502829 100644 --- a/openedx/core/djangoapps/user_api/management/tests/test_email_opt_in_list.py +++ b/openedx/core/djangoapps/user_api/management/tests/test_email_opt_in_list.py @@ -1,9 +1,11 @@ # -*- coding: utf-8 -*- """Tests for the email opt-in list management command. """ +from __future__ import absolute_import + +import csv import os.path -import tempfile import shutil -import csv +import tempfile from collections import defaultdict import ddt @@ -11,16 +13,16 @@ from django.contrib.auth.models import User from django.core.management import call_command from django.core.management.base import CommandError from six import text_type +from six.moves import range -from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase -from xmodule.modulestore.tests.factories import CourseFactory -from student.tests.factories import UserFactory, CourseEnrollmentFactory -from student.models import CourseEnrollment - -from openedx.core.djangoapps.user_api.preferences.api import update_email_opt_in -from openedx.core.djangoapps.user_api.models import UserOrgTag from openedx.core.djangoapps.user_api.management.commands import email_opt_in_list +from openedx.core.djangoapps.user_api.models import UserOrgTag +from openedx.core.djangoapps.user_api.preferences.api import update_email_opt_in from openedx.core.djangolib.testing.utils import skip_unless_lms +from student.models import CourseEnrollment +from student.tests.factories import CourseEnrollmentFactory, UserFactory +from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase +from xmodule.modulestore.tests.factories import CourseFactory @ddt.ddt diff --git a/openedx/core/djangoapps/user_api/management/tests/test_populate_retirement_states.py b/openedx/core/djangoapps/user_api/management/tests/test_populate_retirement_states.py index c12fdc4c3a7..8cc8fd8202e 100644 --- a/openedx/core/djangoapps/user_api/management/tests/test_populate_retirement_states.py +++ b/openedx/core/djangoapps/user_api/management/tests/test_populate_retirement_states.py @@ -1,13 +1,15 @@ """ Test the populate_retirement_states management command """ +from __future__ import absolute_import + import copy -import pytest -from django.core.management import call_command, CommandError +import pytest +from django.core.management import CommandError, call_command -from openedx.core.djangoapps.user_api.models import RetirementState, UserRetirementStatus from openedx.core.djangoapps.user_api.management.commands.populate_retirement_states import START_STATE +from openedx.core.djangoapps.user_api.models import RetirementState, UserRetirementStatus from student.tests.factories import UserFactory pytestmark = pytest.mark.django_db diff --git a/openedx/core/djangoapps/user_api/management/tests/test_sync_hubspot_contacts.py b/openedx/core/djangoapps/user_api/management/tests/test_sync_hubspot_contacts.py index e6e32df7215..49f6abb1add 100644 --- a/openedx/core/djangoapps/user_api/management/tests/test_sync_hubspot_contacts.py +++ b/openedx/core/djangoapps/user_api/management/tests/test_sync_hubspot_contacts.py @@ -1,18 +1,21 @@ """ Test the sync_hubspot_contacts management command """ +from __future__ import absolute_import + import json from datetime import timedelta -from mock import patch from django.core.management import call_command from django.test import TestCase from django.utils import timezone from django.utils.six import StringIO +from mock import patch +from six.moves import range -from openedx.core.djangolib.testing.utils import skip_unless_lms from openedx.core.djangoapps.site_configuration.tests.factories import SiteConfigurationFactory from openedx.core.djangoapps.user_api.management.commands.sync_hubspot_contacts import Command as sync_command +from openedx.core.djangolib.testing.utils import skip_unless_lms from student.models import UserAttribute, UserProfile from student.tests.factories import UserFactory -- GitLab