Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up some deprecation warnings #12469

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions kolibri/core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +0,0 @@
"""TODO: Write something about this module (everything in the docstring
enters the docs)

"""
default_app_config = "kolibri.core.apps.KolibriCoreConfig"
8 changes: 4 additions & 4 deletions kolibri/core/auth/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ def post(self, request):

class FacilityUsernameViewSet(ReadOnlyValuesViewset):
filter_backends = (DjangoFilterBackend, filters.SearchFilter)
filter_fields = ("facility",)
filterset_fields = ("facility",)
search_fields = ("^username",)

values = ("username",)
Expand Down Expand Up @@ -527,7 +527,7 @@ class MembershipViewSet(BulkDeleteMixin, BulkCreateMixin, viewsets.ModelViewSet)
queryset = Membership.objects.all()
serializer_class = MembershipSerializer
filterset_class = MembershipFilter
filter_fields = ["user", "collection", "user_ids"]
filterset_fields = ["user", "collection", "user_ids"]


class RoleFilter(FilterSet):
Expand All @@ -547,7 +547,7 @@ class RoleViewSet(BulkDeleteMixin, BulkCreateMixin, viewsets.ModelViewSet):
queryset = Role.objects.all()
serializer_class = RoleSerializer
filterset_class = RoleFilter
filter_fields = ["user", "collection", "kind", "user_ids"]
filterset_fields = ["user", "collection", "kind", "user_ids"]


dataset_keys = [
Expand Down Expand Up @@ -771,7 +771,7 @@ class LearnerGroupViewSet(ValuesViewset):
queryset = LearnerGroup.objects.all()
serializer_class = LearnerGroupSerializer

filter_fields = ("parent",)
filterset_fields = ("parent",)

values = ("id", "name", "parent", "user_ids")

Expand Down
14 changes: 12 additions & 2 deletions kolibri/core/auth/test/test_bulk_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,21 @@
@override_settings(LANGUAGE_CODE="en")
class UserExportTestCase(TestCase):
@classmethod
def setUpTestData(cls):
def setUpClass(cls):
# Import inside the settings decorator to ensure that the locale is controlled
# Do this inside setUpClass rather than setUpTestData as setUpTestData requires
# assigned objects to be compatible with copy.deepcopy - we do not need to copy
# the management commands as they will not be modified by the tests, unlike the
# models.
from ..management.commands import bulkexportusers as b

cls.b = b
# Run it before setUpClass super call, because this will then execute setUpTestData
# which requires cls.b to be defined.
super().setUpClass()

@classmethod
def setUpTestData(cls):
cls.data = create_dummy_facility_data(
classroom_count=CLASSROOMS, learnergroup_count=1
)
Expand All @@ -28,7 +38,7 @@ def setUpTestData(cls):
_, cls.filepath = tempfile.mkstemp(suffix=".csv")

cls.csv_rows = []
for row in b.csv_file_generator(cls.facility, cls.filepath, True):
for row in cls.b.csv_file_generator(cls.facility, cls.filepath, True):
cls.csv_rows.append(row)

def test_not_specified(self):
Expand Down
2 changes: 1 addition & 1 deletion kolibri/core/bookmarks/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ class BookmarksViewSet(ValuesViewset):
KolibriAuthPermissionsFilter,
DjangoFilterBackend,
)
filter_fields = ("contentnode_id",)
filterset_fields = ("contentnode_id",)
3 changes: 0 additions & 3 deletions kolibri/core/content/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
import os


default_app_config = "kolibri.core.content.apps.KolibriContentConfig"


# Do this to prevent import of broken Windows filetype registry that makes guesstype not work.
# https://www.thecodingforums.com/threads/mimetypes-guess_type-broken-in-windows-on-py2-7-and-python-3-x.952693/
mimetypes.init(
Expand Down
3 changes: 2 additions & 1 deletion kolibri/core/content/utils/content_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ def _get_channel_data(self, channel_id, channel_version):
return channel_data

def _set_channel_data(self, channel_data):
assert isinstance(channel_data, ContentManifestChannelData)
if not isinstance(channel_data, ContentManifestChannelData):
raise TypeError("channel_data must be a ContentManifestChannelData")
self._channels_dict.setdefault(channel_data.channel_id, {})[
channel_data.channel_version
] = channel_data
Expand Down
1 change: 0 additions & 1 deletion kolibri/core/device/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
default_app_config = "kolibri.core.device.apps.KolibriDeviceAppConfig"
2 changes: 1 addition & 1 deletion kolibri/core/discovery/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class NetworkLocationViewSet(viewsets.ModelViewSet):
serializer_class = NetworkLocationSerializer
queryset = NetworkLocation.objects.exclude(location_type=LocationTypes.Reserved)
filter_backends = [DjangoFilterBackend]
filter_fields = [
filterset_fields = [
"id",
"subset_of_users_device",
"instance_id",
Expand Down
10 changes: 5 additions & 5 deletions kolibri/core/exams/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,14 @@ def filter_querysets(self, exam_queryset, draft_queryset):
exam_queryset = instantiated_backend.filter_queryset(
self.request, exam_queryset, self
)
# Do some special handling if the backend has a get_filter_class method
# this is required, as the get_filter_class makes an assertion based on
# Do some special handling if the backend has a get_filterset_class method
# this is required, as the get_filterset_class makes an assertion based on
# the model Meta of the FilterSet - which is set to Exam, so this will
# fail if we try to use the ExamFilter on the DraftExam queryset
# This is a workaround to allow the DraftExam queryset to be filtered
if hasattr(instantiated_backend, "get_filter_class"):
if hasattr(instantiated_backend, "get_filterset_class"):
# First get the filter class using the exam queryset
filter_class = instantiated_backend.get_filter_class(
filter_class = instantiated_backend.get_filterset_class(
self, exam_queryset
)
# If the filter class is not None, then we can use it to filter the draft_queryset
Expand All @@ -136,7 +136,7 @@ def filter_querysets(self, exam_queryset, draft_queryset):
request=self.request,
).qs
else:
# If the backend doesn't have a get_filter_class method, then we can just
# If the backend doesn't have a get_filterset_class method, then we can just
# filter the draft_queryset as normal
draft_queryset = instantiated_backend.filter_queryset(
self.request, draft_queryset, self
Expand Down
2 changes: 1 addition & 1 deletion kolibri/core/lessons/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def _map_lesson_classroom(item):
class LessonViewset(ValuesViewset):
serializer_class = LessonSerializer
filter_backends = (KolibriAuthPermissionsFilter, DjangoFilterBackend)
filter_fields = ("collection", "id")
filterset_fields = ("collection", "id")
permission_classes = (LessonPermissions,)
queryset = Lesson.objects.all().order_by("-date_created")

Expand Down
1 change: 0 additions & 1 deletion kolibri/core/logger/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
default_app_config = "kolibri.core.logger.apps.KolibriLoggerConfig"
2 changes: 1 addition & 1 deletion kolibri/core/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def allow_bulk_destroy(self, qs, filtered):
"""
# Only let a bulk destroy if the queryset is being filtered by a valid filter_field parameter
return any(
key in self.filter_fields for key in self.request.query_params.keys()
key in self.filterset_fields for key in self.request.query_params.keys()
)

def bulk_destroy(self, request, *args, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion kolibri/core/public/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ def post(self, request):

class FacilitySearchUsernameViewSet(BaseValuesViewset):
filter_backends = (DjangoFilterBackend, filters.SearchFilter)
filter_fields = ("facility",)
filterset_fields = ("facility",)
search_fields = ("^username",)

values = ("id", "username")
Expand Down
3 changes: 2 additions & 1 deletion kolibri/core/sqlite/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from django.core.management import call_command
from django.db.utils import DatabaseError
from sqlalchemy import exc
from sqlalchemy import text

from kolibri.deployment.default.sqlite_db_names import NOTIFICATIONS

Expand Down Expand Up @@ -61,7 +62,7 @@ def check_sqlite_integrity(connection):
conn = connection.cursor()

try:
result = conn.execute("PRAGMA integrity_check;").fetchall()
result = conn.execute(text("PRAGMA integrity_check;")).fetchall()
except (DatabaseError, exc.DatabaseError):
raise
finally:
Expand Down
10 changes: 6 additions & 4 deletions kolibri/core/tasks/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
from sqlalchemy import select
from sqlalchemy import String
from sqlalchemy import Table
from sqlalchemy import text
from sqlalchemy import update
from sqlalchemy.exc import OperationalError
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import declarative_base
from sqlalchemy.orm import sessionmaker

from kolibri.core.tasks.constants import DEFAULT_QUEUE
Expand Down Expand Up @@ -157,7 +158,8 @@ def set_sqlite_pragmas(self):
:return: None
"""
try:
self.engine.execute("PRAGMA journal_mode = WAL;")
with self.engine.connect() as conn:
conn.execute(text("PRAGMA journal_mode = WAL;"))
except OperationalError:
pass

Expand Down Expand Up @@ -297,7 +299,7 @@ def _postgres_next_queued_job(self, session, priority):
return self.engine.execute(
update(ORMJob)
.values(state=State.SELECTED)
.where(ORMJob.id == subquery.as_scalar())
.where(ORMJob.id == subquery.scalar_subquery())
.returning(ORMJob.saved_job)
).fetchone()

Expand Down Expand Up @@ -775,7 +777,7 @@ def schedule(
raise ValueError("Job argument must be a Job object.")

with self.session_scope() as session:
orm_job = session.query(ORMJob).get(job.job_id)
orm_job = session.get(ORMJob, job.job_id)
if orm_job and orm_job.state == State.RUNNING:
raise JobRunning()

Expand Down
1 change: 0 additions & 1 deletion kolibri/core/tasks/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ def run_validation(self, data):
enqueue_args = value.pop("enqueue_args", {})
value = self.validate(value)
value["enqueue_args"] = enqueue_args
assert value is not None, ".validate() should return the validated data"
except (ValidationError, DjangoValidationError) as exc:
raise ValidationError(detail=serializers.as_serializer_error(exc))

Expand Down
2 changes: 1 addition & 1 deletion kolibri/core/test/test_drf_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class LanguageViewSet(ModelViewSet):
filter_backends = (DjangoFilterBackend,)
queryset = Language.objects.all()
serializer_class = LanguageSerializer
filter_fields = ("id",)
filterset_fields = ("id",)


class BulkDeleteView(BulkDeleteMixin, LanguageViewSet):
Expand Down
8 changes: 5 additions & 3 deletions kolibri/plugins/app/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.contrib.auth import login
from django.core.exceptions import ValidationError
from django.http import HttpResponseRedirect
from django.utils.http import is_safe_url
from django.utils.http import url_has_allowed_host_and_scheme
from django.utils.http import urlunquote
from rest_framework.decorators import action
from rest_framework.exceptions import APIException
Expand Down Expand Up @@ -70,15 +70,17 @@ def get(self, request, token):
logger.error(e)
redirect_url = request.GET.get("next", "/")
# Copied and modified from https://github.com/django/django/blob/stable/1.11.x/django/views/i18n.py#L40
if (redirect_url or not request.is_ajax()) and not is_safe_url(
if (
redirect_url or not request.is_ajax()
) and not url_has_allowed_host_and_scheme(
url=redirect_url,
allowed_hosts={request.get_host()},
require_https=request.is_secure(),
):
redirect_url = request.META.get("HTTP_REFERER")
if redirect_url:
redirect_url = urlunquote(redirect_url) # HTTP_REFERER may be encoded.
if not is_safe_url(
if not url_has_allowed_host_and_scheme(
url=redirect_url,
allowed_hosts={request.get_host()},
require_https=request.is_secure(),
Expand Down