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

Fix issue #2135 - project code style and linter tools #2196

Closed
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
10 changes: 10 additions & 0 deletions .bandit.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
exclude_dirs:
- /tests/
- ./venv/
- ./env/
- ./node_modules/
- .tox
- pydotorg/settings/local.py
skips:
- B101 # B101: assert
- B311 # B311: random
13 changes: 13 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[flake8]
max-line-length = 120
ignore =
D100,D101,D102,D103,D105,D205,D400,
E305,E266,E231
W504,W606,X100
exclude = ./node_modules/*,*/migrations/*,./venv/*,./env/*,./_personal/*,.tox/*
per-file-ignores =
manage.py:INP001
docs/source/conf.py:INP001
*/test_*.py: E501,W605
custom_storages/__init__.py:F401
sponsors/models/__init__.py:F401
33 changes: 30 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,33 @@
name: CI
on: [push, pull_request]
concurrency:
group: ${{ github.head_ref || github.run_id }}
cancel-in-progress: true

permissions:
contents: read

jobs:
lint:
name: Lint
runs-on: ubuntu-latest
steps:
- name: Check out repository
uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.9.16'
cache: 'pip' # caching pip dependencies
- run: python -m pip install pre-commit
shell: bash
- run: python -m pip freeze --local
shell: bash
- uses: actions/cache@v3
with:
path: ~/.cache/pre-commit
key: pre-commit-3|${{ env.pythonLocation }}|${{ hashFiles('.pre-commit-config.yaml') }}
- run: pre-commit run --show-diff-on-failure --color=always --all-files
shell: bash
test:
runs-on: ubuntu-latest
services:
Expand All @@ -16,10 +43,10 @@ jobs:
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
steps:
- name: Check out repository
uses: actions/checkout@v2
- uses: actions/setup-python@v2
uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: 3.9.16
python-version: "3.9.16"
- name: Cache Python dependencies
uses: actions/cache@v2
env:
Expand Down
43 changes: 43 additions & 0 deletions .github/workflows/pre-commit-autoupdate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Run pre-commit autoupdate every day at midnight
# and create a pull request if any changes

name: Pre-commit auto-update

on:
schedule:
- cron: "15 2 * * *"
workflow_dispatch: # to trigger manually

permissions:
contents: read

jobs:
auto-update:
# Disables this workflow from running in a repository that is not part of the indicated organization/user
if: github.repository_owner == 'pythondotorg'
permissions:
contents: write # for peter-evans/create-pull-request to create branch
pull-requests: write # for peter-evans/create-pull-request to create a PR

runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: "3.9.16"

- name: Install pre-commit
run: pip install pre-commit

- name: Autoupdate template
run: pre-commit autoupdate

- name: Create Pull Request
uses: peter-evans/create-pull-request@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
branch: update/pre-commit-autoupdate
title: Auto-update pre-commit hooks
commit-message: Auto-update pre-commit hooks
body: Update versions of tools in pre-commit configs to latest version
labels: update
19 changes: 19 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
repos:
- repo: https://github.com/pre-commit/mirrors-autopep8
rev: 'v2.0.0'
hooks:
- id: autopep8
args: ['--in-place', '--select', 'E101,E112,E113,E115,E116,E117,E121,E122,E123,E124,E125,E126,E127,E128,E129,E131,E133,E201,E202,E203,E211,E221,E222,E223,E224,E225,E226,E227,E228,E231,E231,E241,E242,E251,E252,E261,E262,E265,E271,E272,E273,E274,E275,E301,E302,E303,E304,E305,E306,W391']
- repo: https://github.com/asottile/pyupgrade
rev: v3.3.1
hooks:
- id: pyupgrade
args: ['--py3-plus', '--py39-plus']
- repo: https://github.com/PyCQA/flake8
rev: 5.0.4
hooks:
- id: flake8
additional_dependencies: [
'flake8-bugbear==22.12.6',
'flake8-no-pep420==2.3.0'
]
7 changes: 6 additions & 1 deletion blogs/admin.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from django.contrib import admin
from django.core.management import call_command

from .models import BlogEntry, Feed, FeedAggregate
from .models import (
BlogEntry,
Feed,
FeedAggregate,
)


@admin.register(BlogEntry)
Expand All @@ -22,4 +26,5 @@ class FeedAggregateAdmin(admin.ModelAdmin):
list_display = ['name', 'slug', 'description']
prepopulated_fields = {'slug': ('name',)}


admin.site.register(Feed)
11 changes: 9 additions & 2 deletions blogs/management/commands/update_blogs.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
from django.core.management.base import BaseCommand
from django.utils.timezone import now

from ...models import BlogEntry, RelatedBlog, Feed
from ...parser import get_all_entries, update_blog_supernav
from ...models import (
BlogEntry,
Feed,
RelatedBlog,
)
from ...parser import (
get_all_entries,
update_blog_supernav,
)


class Command(BaseCommand):
Expand Down
2 changes: 0 additions & 2 deletions blogs/models.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import feedparser

from bs4 import BeautifulSoup
from bs4.element import Comment

from django.db import models

from cms.models import ContentManageable
Expand Down
12 changes: 9 additions & 3 deletions blogs/parser.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import datetime
import feedparser

import feedparser
from django.conf import settings
from django.template.loader import render_to_string
from django.utils.timezone import make_aware, utc
from django.utils.timezone import (
make_aware,
utc,
)

from boxes.models import Box
from .models import BlogEntry, Feed
from .models import (
BlogEntry,
Feed,
)


def get_all_entries(feed_url):
Expand Down
1 change: 0 additions & 1 deletion blogs/templatetags/blogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,3 @@ def feed_list(slug, limit=10):
"""
return BlogEntry.objects.filter(
feed__feedaggregate__slug=slug).order_by('-pub_date')[:limit]

5 changes: 4 additions & 1 deletion blogs/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from django.test import TestCase
from django.utils import timezone

from ..models import BlogEntry, Feed
from ..models import (
BlogEntry,
Feed,
)


class BlogModelTest(TestCase):
Expand Down
2 changes: 1 addition & 1 deletion blogs/tests/test_parser.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import datetime
import unittest

from ..parser import get_all_entries
from .utils import get_test_rss_path
from ..parser import get_all_entries


class BlogParserTest(unittest.TestCase):
Expand Down
14 changes: 10 additions & 4 deletions blogs/tests/test_templatetags.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
from django.core.management import call_command
from django.template import (
Context,
Template,
)
from django.test import TestCase
from django.template import Template, Context
from django.utils.timezone import now

from ..templatetags.blogs import get_latest_blog_entries
from ..models import BlogEntry, Feed, FeedAggregate
from .utils import get_test_rss_path
from ..models import (
BlogEntry,
Feed,
FeedAggregate,
)
from ..templatetags.blogs import get_latest_blog_entries


class BlogTemplateTagTest(TestCase):
Expand Down Expand Up @@ -63,7 +70,6 @@ def test_feed_list(self):
)
fa.feeds.add(f1, f2)


t = Template("""
{% load blogs %}
{% feed_list 'test' as entries %}
Expand Down
8 changes: 5 additions & 3 deletions blogs/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from django.core.management import call_command
from django.urls import reverse
from django.test import TestCase

from ..models import BlogEntry, Feed
from django.urls import reverse

from .utils import get_test_rss_path
from ..models import (
BlogEntry,
Feed,
)


class BlogViewTest(TestCase):
Expand Down
3 changes: 2 additions & 1 deletion blogs/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from . import views
from django.urls import path

from . import views

urlpatterns = [
path('', views.BlogHome.as_view(), name='blog'),
]
1 change: 1 addition & 0 deletions boxes/admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.contrib import admin

from cms.admin import ContentManageableModelAdmin
from .models import Box

Expand Down
4 changes: 1 addition & 3 deletions boxes/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
import pathlib

import factory

from django.conf import settings
from factory.django import DjangoModelFactory

from .models import Box

from users.factories import UserFactory
from .models import Box


class BoxFactory(DjangoModelFactory):
Expand Down
2 changes: 2 additions & 0 deletions boxes/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
from django.conf import settings
from django.db import models
from markupfield.fields import MarkupField

from cms.models import ContentManageable

DEFAULT_MARKUP_TYPE = getattr(settings, 'DEFAULT_MARKUP_TYPE', 'restructuredtext')


class Box(ContentManageable):
label = models.SlugField(max_length=100, unique=True)
content = MarkupField(default_markup_type=DEFAULT_MARKUP_TYPE)
Expand Down
Empty file.
10 changes: 9 additions & 1 deletion boxes/tests.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import logging

from django import template
from django.test import TestCase, override_settings
from django.test import (
TestCase,
override_settings,
)

from .models import Box

logging.disable(logging.CRITICAL)


class BaseTestCase(TestCase):
def setUp(self):
self.box = Box.objects.create(label='test', content='test content')


class TemplateTagTests(BaseTestCase):
def render(self, tmpl, **context):
t = template.Template(tmpl)
Expand All @@ -22,6 +29,7 @@ def test_tag_invalid_label(self):
r = self.render('{% load boxes %}{% box "missing" %}')
self.assertEqual(r, '')


class ViewTests(BaseTestCase):

@override_settings(ROOT_URLCONF='boxes.urls')
Expand Down
3 changes: 2 additions & 1 deletion boxes/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from .views import box
from django.urls import path

from .views import box

urlpatterns = [
path('<slug:label>/', box, name='box'),
]
2 changes: 2 additions & 0 deletions boxes/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from django.http import HttpResponse
from django.shortcuts import get_object_or_404

from .models import Box


def box(request, label):
b = get_object_or_404(Box, label=label)
return HttpResponse(b.content.rendered)
2 changes: 1 addition & 1 deletion cms/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def get_fieldsets(self, request, obj=None):
# Remove created/updated/creator from any existing fieldsets. They'll
# be there if the child class didn't manually declare fieldsets.
fieldsets = super().get_fieldsets(request, obj)
for name, fieldset in fieldsets:
for __, fieldset in fieldsets:
for f in ('created', 'updated', 'creator', 'last_modified_by'):
if f in fieldset['fields']:
fieldset['fields'].remove(f)
Expand Down
Loading