test: Use django_stubs_ext.QuerySetAny for isinstance() checks

mypy complains if QuerySet is used because it uses a parameterized
generic type.
This commit is contained in:
Jennifer Richards 2023-05-13 10:05:33 -03:00
parent 7ad74c99e4
commit 68eb685382
No known key found for this signature in database
GPG key ID: 9B2BF5C5ADDA6A6E
3 changed files with 9 additions and 7 deletions

View file

@ -11,9 +11,10 @@ from django.core.serializers.json import Serializer
from django.http import HttpResponse from django.http import HttpResponse
from django.utils.encoding import smart_str from django.utils.encoding import smart_str
from django.db.models import Field from django.db.models import Field
from django.db.models.query import QuerySet
from django.db.models.signals import post_save, post_delete, m2m_changed from django.db.models.signals import post_save, post_delete, m2m_changed
from django_stubs_ext import QuerySetAny
import debug # pyflakes:ignore import debug # pyflakes:ignore
@ -145,7 +146,7 @@ class AdminJsonSerializer(Serializer):
field_value = None field_value = None
else: else:
field_value = field field_value = field
if isinstance(field_value, QuerySet) or isinstance(field_value, list): if isinstance(field_value, QuerySetAny) or isinstance(field_value, list):
self._current[name] = dict([ (rel.pk, self.expand_related(rel, name)) for rel in field_value ]) self._current[name] = dict([ (rel.pk, self.expand_related(rel, name)) for rel in field_value ])
else: else:
if hasattr(field_value, "_meta"): if hasattr(field_value, "_meta"):

View file

@ -13,11 +13,11 @@ from email.utils import parseaddr
from django import forms from django import forms
from django.conf import settings from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist, ValidationError from django.core.exceptions import ObjectDoesNotExist, ValidationError
from django.db.models.query import QuerySet
from django.forms.utils import ErrorList from django.forms.utils import ErrorList
from django.db.models import Q from django.db.models import Q
#from django.forms.widgets import RadioFieldRenderer #from django.forms.widgets import RadioFieldRenderer
from django.core.validators import validate_email from django.core.validators import validate_email
from django_stubs_ext import QuerySetAny
import debug # pyflakes:ignore import debug # pyflakes:ignore
@ -203,7 +203,7 @@ class SearchLiaisonForm(forms.Form):
class CustomModelMultipleChoiceField(forms.ModelMultipleChoiceField): class CustomModelMultipleChoiceField(forms.ModelMultipleChoiceField):
'''If value is a QuerySet, return it as is (for use in widget.render)''' '''If value is a QuerySet, return it as is (for use in widget.render)'''
def prepare_value(self, value): def prepare_value(self, value):
if isinstance(value, QuerySet): if isinstance(value, QuerySetAny):
return value return value
if (hasattr(value, '__iter__') and if (hasattr(value, '__iter__') and
not isinstance(value, str) and not isinstance(value, str) and

View file

@ -3,11 +3,12 @@
from django.urls import reverse as urlreverse from django.urls import reverse as urlreverse
from django.db.models.query import QuerySet
from django.forms.widgets import Widget from django.forms.widgets import Widget
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
from django.utils.html import conditional_escape from django.utils.html import conditional_escape
from django_stubs_ext import QuerySetAny
class ButtonWidget(Widget): class ButtonWidget(Widget):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
@ -34,7 +35,7 @@ class ShowAttachmentsWidget(Widget):
html = '<div id="id_%s">' % name html = '<div id="id_%s">' % name
html += '<span class="d-none showAttachmentsEmpty form-control widget">No files attached</span>' html += '<span class="d-none showAttachmentsEmpty form-control widget">No files attached</span>'
html += '<div class="attachedFiles form-control widget">' html += '<div class="attachedFiles form-control widget">'
if value and isinstance(value, QuerySet): if value and isinstance(value, QuerySetAny):
for attachment in value: for attachment in value:
html += '<a class="initialAttach" href="%s">%s</a>&nbsp' % (conditional_escape(attachment.document.get_href()), conditional_escape(attachment.document.title)) html += '<a class="initialAttach" href="%s">%s</a>&nbsp' % (conditional_escape(attachment.document.get_href()), conditional_escape(attachment.document.title))
html += '<a class="btn btn-primary btn-sm" href="{}">Edit</a>&nbsp'.format(urlreverse("ietf.liaisons.views.liaison_edit_attachment", kwargs={'object_id':attachment.statement.pk,'doc_id':attachment.document.pk})) html += '<a class="btn btn-primary btn-sm" href="{}">Edit</a>&nbsp'.format(urlreverse("ietf.liaisons.views.liaison_edit_attachment", kwargs={'object_id':attachment.statement.pk,'doc_id':attachment.document.pk}))
@ -43,4 +44,4 @@ class ShowAttachmentsWidget(Widget):
else: else:
html += 'No files attached' html += 'No files attached'
html += '</div></div>' html += '</div></div>'
return mark_safe(html) return mark_safe(html)