Skip to content

Commit

Permalink
Lint
Browse files Browse the repository at this point in the history
  • Loading branch information
zerolab committed Jun 16, 2023
1 parent 7f3d511 commit 0626f14
Show file tree
Hide file tree
Showing 17 changed files with 40 additions and 51 deletions.
11 changes: 4 additions & 7 deletions grapple/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,10 @@ def get_fields_and_properties(cls):
# cls._meta.get_fields(include_parents=False) includes symmetrical ManyToMany fields, while get_model_fields doesn't
fields = [field for field, instance in get_model_fields(cls)]

try:
properties = [
method[0]
for method in inspect.getmembers(cls, lambda o: isinstance(o, property))
]
except BaseException:
properties = []
properties = [
method[0]
for method in inspect.getmembers(cls, lambda o: isinstance(o, property))
]

return fields + properties

Expand Down
2 changes: 1 addition & 1 deletion grapple/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Generated by Django 2.2.1 on 2019-06-13 23:03

from django.db import migrations, models
import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):
Expand Down
8 changes: 3 additions & 5 deletions grapple/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,9 @@ def Mixin():
# Add support for NonNull/required to wrapper field
required_collection_type = None
if required:
required_collection_type = (
lambda nested_type: collection_type( # noqa: E731
nested_type, required=True
)
)

def required_collection_type(nested_type):
return collection_type(nested_type, required=True)

return graphql_type, required_collection_type or collection_type

Expand Down
6 changes: 2 additions & 4 deletions grapple/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ def __check_user_settings(self, user_settings):
if setting in user_settings or hasattr(django_settings, setting):
new_setting = setting.replace("GRAPPLE_", "")
logger.warning(
"The '%s' setting is deprecated and will be removed in the next release, use GRAPPLE['%s'] instead."
% (setting, new_setting)
f"The '{setting}' setting is deprecated and will be removed in the next release, use GRAPPLE['{new_setting}'] instead."
)
if setting in user_settings:
user_settings[new_setting] = user_settings[setting]
Expand All @@ -107,8 +106,7 @@ def __check_user_settings(self, user_settings):
for setting in REMOVED_SETTINGS:
if setting in user_settings:
raise RuntimeError(
"The '%s' setting has been removed. Please refer to '%s' for available settings."
% (setting, settings_doc_url)
f"The '{setting}' setting has been removed. Please refer to '{settings_doc_url}' for available settings."
)
return user_settings

Expand Down
2 changes: 1 addition & 1 deletion grapple/templates/grapple/graphiql.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
var eq = entry.indexOf('=');
if (eq >= 0) {
parameters[decodeURIComponent(entry.slice(0, eq))] =
decodeURIComponent(entry.slice(eq + 1));
decodeURIComponent(entry.slice(eq + 1));
}
});
// Produce a Location query string from a parameter object.
Expand Down
2 changes: 1 addition & 1 deletion grapple/types/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def resolve_document(self, info, id, **kwargs):
return mdl.objects.filter(
collection__view_restrictions__isnull=True
).get(pk=id)
except BaseException:
except mdl.DoesNotExist:
return None

def resolve_documents(self, info, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion grapple/types/media.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def resolve_media_item(self, info, id, **kwargs):
return mdl.objects.filter(
collection__view_restrictions__isnull=True
).get(pk=id)
except BaseException:
except mdl.DoesNotExist:
return None

def resolve_media(self, info, **kwargs):
Expand Down
28 changes: 12 additions & 16 deletions grapple/types/pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,16 +182,14 @@ def get_preview_page(token):
key, value = arg.split("=")
params[key] = value

id = params.get("id")
if id:
if _id := params.get("id"):
"""
This is a page that had already been saved. Lookup the class and call get_page_from_preview_token.
TODO: update headless preview to always send page_type in the token so we can always
the if content_type branch and elimiate the if id branch.
the if content_type branch and eliminate the if id branch.
"""
page = WagtailPage.objects.get(pk=id).specific
if page:
if page := WagtailPage.objects.get(pk=_id).specific:
cls = type(page)
"""
get_page_from_preview_token is added by wagtail-headless-preview,
Expand All @@ -202,15 +200,13 @@ def get_preview_page(token):
"""we assume that get_page_from_preview_token validates the token"""
return cls.get_page_from_preview_token(token)

content_type = params.get("page_type")
if content_type:
if content_type := params.get("page_type"):
"""
this is a page which has not been saved yet. lookup the content_type to get the class
and call get_page_from_preview_token.
"""
app_label, model = content_type.lower().split(".")
ctype = ContentType.objects.get(app_label=app_label, model=model)
if ctype:
if ctype := ContentType.objects.get(app_label=app_label, model=model):
cls = ctype.model_class()
"""
get_page_from_preview_token is added by wagtail-headless-preview,
Expand All @@ -220,7 +216,7 @@ def get_preview_page(token):
if hasattr(cls, "get_page_from_preview_token"):
"""we assume that get_page_from_preview_token validates the token"""
return cls.get_page_from_preview_token(token)
except Exception:
except (WagtailPage.DoesNotExist, ContentType.DoesNotExist, ValueError):
"""
catch and suppress errors. we don't want to expose any information about unpublished content
accidentally.
Expand All @@ -242,15 +238,15 @@ def get_specific_page(

# Everything but the special RootPage
qs = WagtailPage.objects.live().public().filter(depth__gt=1).specific()
ctype = None

if site:
qs = qs.in_site(site)

if content_type:
app_label, model = content_type.lower().split(".")
ctype = ContentType.objects.get(app_label=app_label, model=model)
qs = qs.filter(content_type=ctype)
qs = qs.filter(
content_type=ContentType.objects.get(app_label=app_label, model=model)
)

if id:
page = qs.get(pk=id)
Expand All @@ -274,7 +270,7 @@ def get_specific_page(
if qs.exists():
page = qs.first()

except BaseException:
except WagtailPage.DoesNotExist:
page = None

return page
Expand All @@ -292,12 +288,12 @@ def get_site_filter(info, **kwargs):
if site_hostname is not None:
try:
return resolve_site(site_hostname)
except Site.MultipleObjectsReturned:
except Site.MultipleObjectsReturned as err:
raise GraphQLError(
"Your 'site' filter value of '{}' returned multiple sites. Try adding a port number (for example: '{}:80').".format(
site_hostname, site_hostname
)
)
) from err
elif in_current_site:
return Site.find_for_request(info.context)

Expand Down
8 changes: 4 additions & 4 deletions grapple/types/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ def resolve_setting(self, info, **kwargs):
if site_hostname is not None:
try:
site = resolve_site(site_hostname)
except Site.MultipleObjectsReturned:
except Site.MultipleObjectsReturned as err:
raise GraphQLError(
"Your 'site' filter value of '{}' returned multiple sites. Try adding a port number (for example: '{}:80').".format(
site_hostname, site_hostname
)
)
) from err
else:
site = None

Expand All @@ -65,12 +65,12 @@ def resolve_settings(self, info, **kwargs):
if site_hostname is not None:
try:
site = resolve_site(site_hostname)
except Site.MultipleObjectsReturned:
except Site.MultipleObjectsReturned as err:
raise GraphQLError(
"Your 'site' filter value of '{}' returned multiple sites. Try adding a port number (for example: '{}:80').".format(
site_hostname, site_hostname
)
)
) from err
else:
site = None

Expand Down
4 changes: 2 additions & 2 deletions grapple/types/sites.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ def resolve_site(self, info, **kwargs):
elif hostname:
try:
return resolve_site(hostname)
except Site.MultipleObjectsReturned:
except Site.MultipleObjectsReturned as err:
raise GraphQLError(
"Your 'hostname' filter value of '{}' returned multiple sites. Try adding a port number (for example: '{}:80').".format(
hostname, hostname
)
)
) from err

return Mixin
2 changes: 1 addition & 1 deletion grapple/types/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Mixin:
def resolve_tag(self, info, id, **kwargs):
try:
return Tag.objects.get(pk=id)
except BaseException:
except Tag.DoesNotExist:
return None

def resolve_tags(self, info, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@

DEBUG = True

SECRET_KEY = "this-is-not-a-secret"
SECRET_KEY = "this-is-not-a-secret" # noqa: #S105

# SECURITY WARNING: define the correct hosts in production!
ALLOWED_HOSTS = ["*"]
Expand Down
2 changes: 1 addition & 1 deletion tests/test_blog.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def get_blocks_from_body(self, block_type, block_query="rawValue", page_id=None)

# Print the error response
if not executed.get("data"):
print(executed)
print(executed) # noqa: T201

blocks = []
for block in executed["data"]["page"]["body"]:
Expand Down
3 changes: 2 additions & 1 deletion tests/testapp/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import factory
import wagtail_factories
from factory import fuzzy
from wagtail import blocks

from testapp.blocks import (
ImageGalleryBlock,
ImageGalleryImage,
Expand All @@ -19,7 +21,6 @@
Person,
SimpleModel,
)
from wagtail import blocks


# START: Block Factories
Expand Down
5 changes: 2 additions & 3 deletions tests/testapp/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# Generated by Django 4.1.9 on 2023-05-12 00:46

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import modelcluster.contrib.taggit
import modelcluster.fields
Expand All @@ -15,7 +13,8 @@
import wagtail.search.index
import wagtail.snippets.blocks
import wagtail_headless_preview.models

from django.conf import settings
from django.db import migrations, models
from wagtail import VERSION as WAGTAIL_VERSION

if WAGTAIL_VERSION < (4, 2):
Expand Down
2 changes: 1 addition & 1 deletion tests/testapp/models/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from modelcluster.contrib.taggit import ClusterTaggableManager
from modelcluster.fields import ParentalKey
from taggit.models import TaggedItemBase
from testapp.blocks import StreamFieldBlock
from wagtail.admin.panels import FieldPanel, InlinePanel
from wagtail.contrib.settings.models import (
BaseGenericSetting,
Expand Down Expand Up @@ -39,6 +38,7 @@
GraphQLTag,
)
from grapple.utils import resolve_paginated_queryset
from testapp.blocks import StreamFieldBlock

document_model_string = getattr(
settings, "WAGTAILDOCS_DOCUMENT_MODEL", "wagtaildocs.Document"
Expand Down
2 changes: 1 addition & 1 deletion tests/testapp/mutations.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import graphene
from testapp.models import Advert, AuthorPage
from wagtail.models import Page

from grapple.registry import registry
from grapple.types.pages import PageInterface
from grapple.types.rich_text import RichText
from testapp.models import Advert, AuthorPage


class CreateAuthor(graphene.Mutation):
Expand Down

0 comments on commit 0626f14

Please sign in to comment.