Skip to content

Commit

Permalink
Blackify code
Browse files Browse the repository at this point in the history
Run code through black.

Signed-off-by: Stephen Finucane <[email protected]>
  • Loading branch information
stephenfin committed May 6, 2022
1 parent 838ebe3 commit 94d75a1
Show file tree
Hide file tree
Showing 108 changed files with 3,274 additions and 1,976 deletions.
13 changes: 9 additions & 4 deletions docs/api/schemas/generate-schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ def generate_schemas():
env = jinja2.Environment(
loader=jinja2.FileSystemLoader(ROOT_DIR),
trim_blocks=True,
lstrip_blocks=True)
lstrip_blocks=True,
)
template = env.get_template('patchwork.j2')

for version in VERSIONS:
version_dir = os.path.join(
ROOT_DIR, 'v%d.%d' % version if version else 'latest')
ROOT_DIR, 'v%d.%d' % version if version else 'latest'
)

if not os.path.exists(version_dir):
os.mkdir(version_dir)
Expand All @@ -37,8 +39,11 @@ def generate_schemas():
version = version or LATEST_VERSION

with open(os.path.join(version_dir, 'patchwork.yaml'), 'wb') as fh:
template.stream(version=version, version_str=version_str,
version_url=version_url).dump(fh, encoding='utf-8')
template.stream(
version=version,
version_str=version_str,
version_url=version_url,
).dump(fh, encoding='utf-8')
fh.write(b'\n')

print(f'Schemas written to {ROOT_DIR}.')
Expand Down
6 changes: 1 addition & 5 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@
# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
'sphinx.ext.todo',
'reno.sphinxext',
'sphinxcontrib.openapi'
]
extensions = ['sphinx.ext.todo', 'reno.sphinxext', 'sphinxcontrib.openapi']

# The theme to use for HTML and HTML Help pages.
html_theme = 'sphinx_rtd_theme'
Expand Down
4 changes: 2 additions & 2 deletions manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

if __name__ == "__main__":
os.environ.setdefault(
"DJANGO_SETTINGS_MODULE",
"patchwork.settings.production")
"DJANGO_SETTINGS_MODULE", "patchwork.settings.production"
)

from django.core.management import execute_from_command_line

Expand Down
54 changes: 41 additions & 13 deletions patchwork/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class UserProfileInline(admin.StackedInline):


class UserAdmin(BaseUserAdmin):
inlines = (UserProfileInline, )
inlines = (UserProfileInline,)


admin.site.unregister(User)
Expand Down Expand Up @@ -78,7 +78,7 @@ class StateAdmin(admin.ModelAdmin):

class CoverAdmin(admin.ModelAdmin):
list_display = ('name', 'submitter', 'project', 'date')
list_filter = ('project', )
list_filter = ('project',)
search_fields = ('name', 'submitter__name', 'submitter__email')
date_hierarchy = 'date'

Expand All @@ -87,8 +87,15 @@ class CoverAdmin(admin.ModelAdmin):


class PatchAdmin(admin.ModelAdmin):
list_display = ('name', 'submitter', 'project', 'state', 'date',
'archived', 'is_pull_request')
list_display = (
'name',
'submitter',
'project',
'state',
'date',
'archived',
'is_pull_request',
)
list_filter = ('project', 'submitter', 'state', 'archived')
list_select_related = ('submitter', 'project', 'state')
search_fields = ('name', 'submitter__name', 'submitter__email')
Expand Down Expand Up @@ -129,23 +136,38 @@ class PatchInline(admin.StackedInline):


class SeriesAdmin(admin.ModelAdmin):
list_display = ('name', 'submitter', 'project', 'date', 'version', 'total',
'received_total', 'received_all')
list_display = (
'name',
'submitter',
'project',
'date',
'version',
'total',
'received_total',
'received_all',
)
list_filter = ('project', 'submitter')
list_select_related = ('submitter', 'project')
readonly_fields = ('received_total', 'received_all')
search_fields = ('submitter__name', 'submitter__email')
exclude = ('patches', )
inlines = (PatchInline, )
exclude = ('patches',)
inlines = (PatchInline,)

def received_all(self, series):
return series.received_all

received_all.boolean = True

def get_queryset(self, request):
qs = super(SeriesAdmin, self).get_queryset(request)
return qs.prefetch_related(Prefetch(
'patches', Patch.objects.only('series',)))
return qs.prefetch_related(
Prefetch(
'patches',
Patch.objects.only(
'series',
),
)
)


admin.site.register(Series, SeriesAdmin)
Expand All @@ -159,9 +181,15 @@ class SeriesReferenceAdmin(admin.ModelAdmin):


class CheckAdmin(admin.ModelAdmin):
list_display = ('patch', 'user', 'state', 'target_url',
'description', 'context')
exclude = ('date', )
list_display = (
'patch',
'user',
'state',
'target_url',
'description',
'context',
)
exclude = ('date',)
search_fields = ('patch__name', 'project__name')
date_hierarchy = 'date'

Expand Down
3 changes: 2 additions & 1 deletion patchwork/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# [1] https://github.com/encode/django-rest-framework/issues/7550
# [2] https://github.com/encode/django-rest-framework/pull/7574


def _get_attribute(self, instance):
# Can't have any relationships if not created
if hasattr(instance, 'pk') and instance.pk is None:
Expand All @@ -39,7 +40,7 @@ def _get_attribute(self, instance):
field=self.field_name,
serializer=self.parent.__class__.__name__,
instance=instance.__class__.__name__,
exc=exc
exc=exc,
)
)
raise type(exc)(msg)
Expand Down
13 changes: 9 additions & 4 deletions patchwork/api/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@


if DRF_VERSION > (3, 11):

class CurrentPatchDefault(object):
requires_context = True

Expand All @@ -32,7 +33,9 @@ class CurrentCoverDefault(object):

def __call__(self, serializer_field):
return serializer_field.context['request'].cover

else:

class CurrentPatchDefault(object):
def set_context(self, serializer_field):
self.patch = serializer_field.context['request'].patch
Expand All @@ -56,6 +59,7 @@ class LinkHeaderPagination(PageNumberPagination):
https://tools.ietf.org/html/rfc5988#section-5
https://developer.github.com/guides/traversing-with-pagination
"""

page_size = settings.REST_RESULTS_PER_PAGE
max_page_size = settings.MAX_REST_RESULTS_PER_PAGE
page_size_query_param = 'per_page'
Expand Down Expand Up @@ -95,6 +99,7 @@ class PatchworkPermission(permissions.BasePermission):
This permission works for Project, Patch, PatchComment
and CoverComment model objects
"""

def has_object_permission(self, request, view, obj):
# read only for everyone
if request.method in permissions.SAFE_METHODS:
Expand All @@ -109,15 +114,15 @@ def get_object(self):
queryset = self.filter_queryset(self.get_queryset())
filter_kwargs = {}
for field_name, field in zip(
self.lookup_fields, self.lookup_url_kwargs):
self.lookup_fields, self.lookup_url_kwargs
):
if self.kwargs[field]:
filter_kwargs[field_name] = self.kwargs[field]

return get_object_or_404(queryset, **filter_kwargs)


class CheckHyperlinkedIdentityField(HyperlinkedIdentityField):

def get_url(self, obj, view_name, request, format):
# Unsaved objects will not yet have a valid URL.
if obj.pk is None:
Expand All @@ -135,10 +140,10 @@ def get_url(self, obj, view_name, request, format):


class BaseHyperlinkedModelSerializer(HyperlinkedModelSerializer):

def to_representation(self, instance):
data = super(BaseHyperlinkedModelSerializer, self).to_representation(
instance)
instance
)

request = self.context.get('request')
for version in getattr(self.Meta, 'versioned_fields', {}):
Expand Down
49 changes: 34 additions & 15 deletions patchwork/api/bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class BundlePermission(permissions.BasePermission):
Bundle creation/updating was only added in API v1.2 and we don't want to
change behavior in older API versions.
"""

def has_permission(self, request, view):
# read-only permission for everything
if request.method in permissions.SAFE_METHODS:
Expand All @@ -36,16 +37,19 @@ def has_permission(self, request, view):
raise exceptions.MethodNotAllowed(request.method)

if request.method == 'POST' and (
not request.user or not request.user.is_authenticated):
not request.user or not request.user.is_authenticated
):
return False

# we have more to do but we can't do that until we have an object
return True

def has_object_permission(self, request, view, obj):
if (request.user and
request.user.is_authenticated and
request.user == obj.owner):
if (
request.user
and request.user.is_authenticated
and request.user == obj.owner
):
return True

if not obj.public:
Expand All @@ -62,8 +66,9 @@ class BundleSerializer(BaseHyperlinkedModelSerializer):
project = ProjectSerializer(read_only=True)
mbox = SerializerMethodField()
owner = UserSerializer(read_only=True)
patches = PatchSerializer(many=True, required=True,
style={'base_template': 'input.html'})
patches = PatchSerializer(
many=True, required=True, style={'base_template': 'input.html'}
)

def get_web_url(self, instance):
request = self.context.get('request')
Expand All @@ -82,7 +87,8 @@ def create(self, validated_data):
def update(self, instance, validated_data):
patches = validated_data.pop('patches', None)
instance = super(BundleSerializer, self).update(
instance, validated_data)
instance, validated_data
)
if patches:
instance.overwrite_patches(patches)
return instance
Expand All @@ -92,8 +98,9 @@ def validate_patches(self, value):
raise ValidationError('Bundles cannot be empty')

if len(set([p.project.id for p in value])) > 1:
raise ValidationError('Bundle patches must belong to the same '
'project')
raise ValidationError(
'Bundle patches must belong to the same ' 'project'
)

return value

Expand All @@ -105,11 +112,20 @@ def validate(self, data):

class Meta:
model = Bundle
fields = ('id', 'url', 'web_url', 'project', 'name', 'owner',
'patches', 'public', 'mbox')
fields = (
'id',
'url',
'web_url',
'project',
'name',
'owner',
'patches',
'public',
'mbox',
)
read_only_fields = ('project', 'owner', 'mbox')
versioned_fields = {
'1.1': ('web_url', ),
'1.1': ('web_url',),
}
extra_kwargs = {
'url': {'view_name': 'api-bundle-detail'},
Expand All @@ -127,10 +143,13 @@ def get_queryset(self):
else:
bundle_filter = Q(public=True)

return Bundle.objects\
.filter(bundle_filter)\
.prefetch_related('patches',)\
return (
Bundle.objects.filter(bundle_filter)
.prefetch_related(
'patches',
)
.select_related('owner', 'project')
)


class BundleList(BundleMixin, ListCreateAPIView):
Expand Down
13 changes: 11 additions & 2 deletions patchwork/api/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,17 @@ def to_representation(self, instance):

class Meta:
model = Check
fields = ('id', 'url', 'patch', 'user', 'date', 'state', 'target_url',
'context', 'description')
fields = (
'id',
'url',
'patch',
'user',
'date',
'state',
'target_url',
'context',
'description',
)
read_only_fields = ('date',)
extra_kwargs = {
'url': {'view_name': 'api-check-detail'},
Expand Down
Loading

0 comments on commit 94d75a1

Please sign in to comment.