refactor: ran pyugprade on lms/djangoapps/courseware (#26738)

This commit is contained in:
Usama Sadiq
2021-03-12 12:16:46 +05:00
committed by GitHub
parent 859df03049
commit b1b3ba9c81
19 changed files with 44 additions and 76 deletions

View File

@@ -11,7 +11,6 @@ import lxml.etree
from django.core.management.base import BaseCommand
from fs.osfs import OSFS
from path import Path as path
from six.moves import map
from xmodule.modulestore.xml import XMLModuleStore
@@ -35,7 +34,7 @@ def export(course, export_dir):
"""
fs = OSFS(export_dir, create=True)
if not fs.isdirempty('.'): # lint-amnesty, pylint: disable=no-member
print(u'WARNING: Directory {dir} not-empty. May clobber/confuse things'.format(dir=export_dir))
print(f'WARNING: Directory {export_dir} not-empty. May clobber/confuse things')
try:
course.runtime.export_fs = fs
@@ -55,7 +54,7 @@ def export(course, export_dir):
def import_with_checks(course_dir): # lint-amnesty, pylint: disable=missing-function-docstring
all_ok = True
print(u'Attempting to load "{}"'.format(course_dir))
print(f'Attempting to load "{course_dir}"')
course_dir = path(course_dir)
data_dir = course_dir.dirname()
@@ -71,13 +70,13 @@ def import_with_checks(course_dir): # lint-amnesty, pylint: disable=missing-fun
def str_of_err(tpl):
(msg, exc_str) = tpl
return '{msg}\n{exc}'.format(msg=msg, exc=exc_str)
return f'{msg}\n{exc_str}'
courses = modulestore.get_courses()
n = len(courses)
if n != 1:
print(u'ERROR: Expect exactly 1 course. Loaded {n}: {lst}'.format(n=n, lst=courses))
print(f'ERROR: Expect exactly 1 course. Loaded {n}: {courses}')
return (False, None)
course = courses[0]
@@ -102,7 +101,7 @@ def import_with_checks(course_dir): # lint-amnesty, pylint: disable=missing-fun
print('Running validators...')
for validate in validators:
print(u'Running {}'.format(validate.__name__))
print(f'Running {validate.__name__}')
all_ok = validate(course) and all_ok
if all_ok:
@@ -130,7 +129,7 @@ def check_roundtrip(course_dir):
# diff = dircmp(course_dir, export_dir, ignore=[], hide=[])
print('======== Roundtrip diff: =========')
sys.stdout.flush() # needed to make diff appear in the right place
os.system(u'diff -r {} {}'.format(course_dir, export_dir))
os.system(f'diff -r {course_dir} {export_dir}')
print('======== ideally there is no diff above this =======')

View File

@@ -8,7 +8,6 @@ Output is UTF-8 encoded by default.
from textwrap import dedent
from django.core.management.base import BaseCommand
from six import text_type
from openedx.core.djangoapps.content.course_overviews.models import CourseOverview
@@ -22,6 +21,6 @@ class Command(BaseCommand): # lint-amnesty, pylint: disable=missing-class-docst
help='name of the modulestore to use')
def handle(self, *args, **options):
output = '\n'.join(text_type(course_overview.id) for course_overview in CourseOverview.get_all_courses()) + '\n'
output = '\n'.join(str(course_overview.id) for course_overview in CourseOverview.get_all_courses()) + '\n'
return output

View File

@@ -20,7 +20,6 @@ The resulting JSON object has one entry for each module in the course:
import json
from textwrap import dedent
import six
from django.core.management.base import BaseCommand, CommandError
from opaque_keys import InvalidKeyError
from opaque_keys.edx.keys import CourseKey
@@ -76,7 +75,7 @@ class Command(BaseCommand): # lint-amnesty, pylint: disable=missing-class-docst
info = dump_module(course, inherited=options['inherited'], defaults=options['inherited_defaults'])
return json.dumps(info, indent=2, sort_keys=True, default=six.text_type)
return json.dumps(info, indent=2, sort_keys=True, default=str)
def dump_module(module, destination=None, inherited=False, defaults=False):
@@ -93,11 +92,11 @@ def dump_module(module, destination=None, inherited=False, defaults=False):
if isinstance(module, DiscussionXBlock) and 'discussion_id' not in items:
items['discussion_id'] = module.discussion_id
filtered_metadata = {k: v for k, v in six.iteritems(items) if k not in FILTER_LIST}
filtered_metadata = {k: v for k, v in items.items() if k not in FILTER_LIST}
destination[six.text_type(module.location)] = {
destination[str(module.location)] = {
'category': module.location.block_type,
'children': [six.text_type(child) for child in getattr(module, 'children', [])],
'children': [str(child) for child in getattr(module, 'children', [])],
'metadata': filtered_metadata,
}
@@ -118,7 +117,7 @@ def dump_module(module, destination=None, inherited=False, defaults=False):
return field.values != field.default
inherited_metadata = {field.name: field.read_json(module) for field in module.fields.values() if is_inherited(field)} # lint-amnesty, pylint: disable=line-too-long
destination[six.text_type(module.location)]['inherited_metadata'] = inherited_metadata
destination[str(module.location)]['inherited_metadata'] = inherited_metadata
for child in module.get_children():
dump_module(child, destination, inherited, defaults)

View File

@@ -50,10 +50,10 @@ class Command(BaseCommand):
python_lib_filename = options.get('python_lib_filename')
output = (
u"Importing...\n"
u" data_dir={data}, source_dirs={courses}\n"
u" Importing static content? {import_static}\n"
u" Importing python lib? {import_python_lib}"
"Importing...\n"
" data_dir={data}, source_dirs={courses}\n"
" Importing static content? {import_static}\n"
" Importing python lib? {import_python_lib}"
).format(
data=data_dir,
courses=source_dirs,
@@ -74,5 +74,5 @@ class Command(BaseCommand):
for course in course_items:
course_id = course.id
if not are_permissions_roles_seeded(course_id):
self.stdout.write(u'Seeding forum roles for course {0}\n'.format(course_id))
self.stdout.write(f'Seeding forum roles for course {course_id}\n')
seed_permissions_roles(course_id)

View File

@@ -1,18 +1,14 @@
# coding=utf-8
"""
Tests for Django management commands
"""
import json
from six import StringIO
from io import StringIO
import factory
import six
from django.conf import settings
from django.core.management import call_command
from six import text_type
from xmodule.modulestore import ModuleStoreEnum
from xmodule.modulestore.django import modulestore
@@ -42,7 +38,7 @@ class CommandsTestBase(SharedModuleStoreTestCase):
@classmethod
def setUpClass(cls):
super(CommandsTestBase, cls).setUpClass()
super().setUpClass()
cls.test_course_key = modulestore().make_course_key("edX", "simple", "2012_Fall")
cls.loaded_courses = cls.load_courses()
@@ -59,7 +55,7 @@ class CommandsTestBase(SharedModuleStoreTestCase):
org=unique_org,
course='simple',
run="run",
display_name=u'2012_Fáĺĺ',
display_name='2012_Fáĺĺ',
modulestore=store
)
@@ -88,12 +84,12 @@ class CommandsTestBase(SharedModuleStoreTestCase):
def test_dump_course_ids(self):
output = self.call_command('dump_course_ids')
dumped_courses = output.strip().split('\n')
course_ids = {text_type(course_id) for course_id in self.loaded_courses}
course_ids = {str(course_id) for course_id in self.loaded_courses}
dumped_ids = set(dumped_courses)
assert course_ids == dumped_ids
def test_correct_course_structure_metadata(self):
course_id = text_type(self.test_course_key)
course_id = str(self.test_course_key)
args = [course_id]
kwargs = {'modulestore': 'default'}
@@ -106,7 +102,7 @@ class CommandsTestBase(SharedModuleStoreTestCase):
assert len(list(dump.values())) > 0
def test_dump_course_structure(self):
args = [text_type(self.test_course_key)]
args = [str(self.test_course_key)]
kwargs = {'modulestore': 'default'}
output = self.call_command('dump_course_structure', *args, **kwargs)
@@ -114,7 +110,7 @@ class CommandsTestBase(SharedModuleStoreTestCase):
# check that all elements in the course structure have metadata,
# but not inherited metadata:
for element in six.itervalues(dump):
for element in dump.values():
assert 'metadata' in element
assert 'children' in element
assert 'category' in element
@@ -122,7 +118,7 @@ class CommandsTestBase(SharedModuleStoreTestCase):
# Check a few elements in the course dump
test_course_key = self.test_course_key
parent_id = text_type(test_course_key.make_usage_key('chapter', 'Overview'))
parent_id = str(test_course_key.make_usage_key('chapter', 'Overview'))
assert dump[parent_id]['category'] == 'chapter'
assert len(dump[parent_id]['children']) == 3
@@ -130,12 +126,11 @@ class CommandsTestBase(SharedModuleStoreTestCase):
assert dump[child_id]['category'] == 'videosequence'
assert len(dump[child_id]['children']) == 2
video_id = text_type(test_course_key.make_usage_key('video', 'Welcome'))
video_id = str(test_course_key.make_usage_key('video', 'Welcome'))
assert dump[video_id]['category'] == 'video'
video_metadata = dump[video_id]['metadata']
video_metadata.pop('edx_video_id', None)
six.assertCountEqual(
self,
self.assertCountEqual(
list(video_metadata.keys()),
['youtube_id_0_75', 'youtube_id_1_0', 'youtube_id_1_25', 'youtube_id_1_5']
)
@@ -146,13 +141,13 @@ class CommandsTestBase(SharedModuleStoreTestCase):
assert len(dump) == 17
def test_dump_inherited_course_structure(self):
args = [text_type(self.test_course_key)]
args = [str(self.test_course_key)]
kwargs = {'modulestore': 'default', 'inherited': True}
output = self.call_command('dump_course_structure', *args, **kwargs)
dump = json.loads(output)
# check that all elements in the course structure have inherited metadata,
# and that it contains a particular value as well:
for element in six.itervalues(dump):
for element in dump.values():
assert 'metadata' in element
assert 'children' in element
assert 'category' in element
@@ -161,13 +156,13 @@ class CommandsTestBase(SharedModuleStoreTestCase):
assert 'due' not in element['inherited_metadata']
def test_dump_inherited_course_structure_with_defaults(self):
args = [text_type(self.test_course_key)]
args = [str(self.test_course_key)]
kwargs = {'modulestore': 'default', 'inherited': True, 'inherited_defaults': True}
output = self.call_command('dump_course_structure', *args, **kwargs)
dump = json.loads(output)
# check that all elements in the course structure have inherited metadata,
# and that it contains a particular value as well:
for element in six.itervalues(dump):
for element in dump.values():
assert 'metadata' in element
assert 'children' in element
assert 'category' in element
@@ -176,16 +171,16 @@ class CommandsTestBase(SharedModuleStoreTestCase):
assert element['inherited_metadata']['due'] is None
def test_export_discussion_ids(self):
output = self.call_command('dump_course_structure', text_type(self.course.id))
output = self.call_command('dump_course_structure', str(self.course.id))
dump = json.loads(output)
dumped_id = dump[text_type(self.discussion.location)]['metadata']['discussion_id']
dumped_id = dump[str(self.discussion.location)]['metadata']['discussion_id']
assert dumped_id == self.discussion.discussion_id
def test_export_discussion_id_custom_id(self):
output = self.call_command('dump_course_structure', text_type(self.test_course_key))
output = self.call_command('dump_course_structure', str(self.test_course_key))
dump = json.loads(output)
discussion_key = text_type(self.test_course_key.make_usage_key('discussion', 'custom_id'))
dumped_id = dump[text_type(discussion_key)]['metadata']['discussion_id']
discussion_key = str(self.test_course_key.make_usage_key('discussion', 'custom_id'))
dumped_id = dump[str(discussion_key)]['metadata']['discussion_id']
assert dumped_id == 'custom'
def check_export_file(self, tar_file): # pylint: disable=missing-function-docstring
@@ -198,7 +193,7 @@ class CommandsTestBase(SharedModuleStoreTestCase):
assert_in = self.assertIn
assert_in('edX-simple-2012_Fall', names)
assert_in('edX-simple-2012_Fall/policies/{}/policy.json'.format(self.url_name), names)
assert_in(f'edX-simple-2012_Fall/policies/{self.url_name}/policy.json', names)
assert_in('edX-simple-2012_Fall/html/toylab.html', names)
assert_in('edX-simple-2012_Fall/videosequence/A_simple_sequence.xml', names)
assert_in('edX-simple-2012_Fall/sequential/Lecture_2.xml', names)

View File

@@ -1,6 +1,3 @@
# -*- coding: utf-8 -*-
import django.utils.timezone
import model_utils.fields
from django.conf import settings
@@ -120,26 +117,26 @@ class Migration(migrations.Migration):
),
migrations.AlterUniqueTogether(
name='xmoduleuserstatesummaryfield',
unique_together=set([('usage_id', 'field_name')]),
unique_together={('usage_id', 'field_name')},
),
migrations.AlterUniqueTogether(
name='xmodulestudentprefsfield',
unique_together=set([('student', 'module_type', 'field_name')]),
unique_together={('student', 'module_type', 'field_name')},
),
migrations.AlterUniqueTogether(
name='xmodulestudentinfofield',
unique_together=set([('student', 'field_name')]),
unique_together={('student', 'field_name')},
),
migrations.AlterUniqueTogether(
name='studentmodule',
unique_together=set([('student', 'module_state_key', 'course_id')]),
unique_together={('student', 'module_state_key', 'course_id')},
),
migrations.AlterUniqueTogether(
name='studentfieldoverride',
unique_together=set([('course_id', 'field', 'location', 'student')]),
unique_together={('course_id', 'field', 'location', 'student')},
),
migrations.AlterUniqueTogether(
name='offlinecomputedgrade',
unique_together=set([('user', 'course_id')]),
unique_together={('user', 'course_id')},
),
]

View File

@@ -1,6 +1,3 @@
# -*- coding: utf-8 -*-
import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models

View File

@@ -1,6 +1,3 @@
# -*- coding: utf-8 -*-
from django.db import migrations, models

View File

@@ -1,6 +1,3 @@
# -*- coding: utf-8 -*-
from django.db import migrations, models

View File

@@ -1,6 +1,3 @@
# -*- coding: utf-8 -*-
import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models

View File

@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.13 on 2018-05-27 16:27

View File

@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.13 on 2018-05-29 15:18
@@ -15,6 +14,6 @@ class Migration(migrations.Migration):
migrations.AlterField(
model_name='studentmodule',
name='done',
field=models.CharField(choices=[(u'na', u'NOT_APPLICABLE'), (u'f', u'FINISHED'), (u'i', u'INCOMPLETE')], default=u'na', max_length=8),
field=models.CharField(choices=[('na', 'NOT_APPLICABLE'), ('f', 'FINISHED'), ('i', 'INCOMPLETE')], default='na', max_length=8),
),
]

View File

@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.20 on 2019-03-21 15:25

View File

@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.22 on 2019-07-03 19:55

View File

@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.22 on 2019-07-09 15:59

View File

@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.23 on 2019-08-28 15:50

View File

@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.23 on 2019-09-08 04:54
#
# This migration does not produce any actual database changes; it only affects

View File

@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.25 on 2019-10-01 18:58

View File

@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.29 on 2020-03-11 10:07