Skip to content

Commit

Permalink
flake8 fmt + additional linting
Browse files Browse the repository at this point in the history
Signed-off-by: Jason <git@jasoncameron.dev>
  • Loading branch information
JasonLovesDoggo committed Feb 8, 2024
1 parent 43094b9 commit 829f392
Show file tree
Hide file tree
Showing 128 changed files with 592 additions and 756 deletions.
1 change: 0 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,3 @@ flake.nix
flake.lock
_config.yml
README.md

5 changes: 5 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[flake8]
ignore = F403,F405,E501
max-line-length = 101
exclude = docs/*
max-complexity = 10
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
uses: docker/setup-buildx-action@v1
- name: Login to GHCR
if: github.event_name != 'pull_request'
uses: docker/login-action@v1
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
Expand Down
16 changes: 10 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,40 @@ repos:
rev: v4.5.0
hooks:
- id: trailing-whitespace
args: [ --markdown-linebreak-ext=md ]
- id: end-of-file-fixer
- id: check-json
exclude: "\\.geojson$|manifest\\.json$"
- id: check-toml
- id: check-xml
exclude: "\\.svg$"
- id: check-yaml
- id: debug-statements
- id: check-builtin-literals
- id: check-case-conflict
- id: check-symlinks
- id: check-docstring-first
- id: detect-private-key
- repo: https://github.com/psf/black
rev: 24.1.1
hooks:
- id: black
args: [ --line-length=101 ]

- repo: https://github.com/PyCQA/isort
rev: 5.13.2
hooks:
- id: isort

- repo: https://github.com/PyCQA/flake8
rev: 7.0.0
hooks:
- id: flake8
# - repo: https://github.com/PyCQA/flake8
# rev: 7.0.0
# hooks:
# - id: flake8

- repo: https://github.com/Riverside-Healthcare/djLint
rev: v1.34.1
hooks:
- id: djlint-reformat-django
- id: djlint-django
# - id: djlint-django fix errors first lo

- repo: local
hooks:
Expand Down
82 changes: 44 additions & 38 deletions core/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ def get_queryset(self, request):
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == "organization":
if not request.user.is_superuser:
kwargs["queryset"] = models.Organization.objects.filter(
execs=request.user
)
kwargs["queryset"] = models.Organization.objects.filter(execs=request.user)
return super().formfield_for_foreignkey(db_field, request, **kwargs)


Expand Down Expand Up @@ -150,9 +148,7 @@ def get_readonly_fields(self, request, obj=None):

def formfield_for_manytomany(self, db_field, request, **kwargs):
if db_field.name == "supervisors" and not request.user.is_superuser:
kwargs["queryset"] = models.User.objects.filter(is_teacher=True).order_by(
"username"
)
kwargs["queryset"] = models.User.objects.filter(is_teacher=True).order_by("username")
if db_field.name == "execs":
kwargs["queryset"] = models.User.objects.all().order_by("username")
if db_field.name == "tags":
Expand Down Expand Up @@ -213,8 +209,7 @@ def get_queryset(self, request):
return qs
return (
qs.filter(
Q(organization__supervisors=request.user)
| Q(organization__execs=request.user)
Q(organization__supervisors=request.user) | Q(organization__execs=request.user)
)
.distinct()
.filter(organization__is_active=True)
Expand Down Expand Up @@ -248,8 +243,22 @@ def get_readonly_fields(self, request, obj=None):
fields = set(all_fields)
fields_matrix = [
[
{"author", "organization", "title", "tags", "is_public", "supervisor"},
{"author", "organization", "title", "tags", "is_public", "supervisor"},
{
"author",
"organization",
"title",
"tags",
"is_public",
"supervisor",
},
{
"author",
"organization",
"title",
"tags",
"is_public",
"supervisor",
},
{
"author",
"organization",
Expand All @@ -276,8 +285,20 @@ def get_readonly_fields(self, request, obj=None):
[
{"author", "organization", "supervisor"},
{"author", "organization", "status", "supervisor"},
{"author", "organization", "status", "show_after", "supervisor"},
{"author", "organization", "status", "supervisor", "rejection_reason"},
{
"author",
"organization",
"status",
"show_after",
"supervisor",
},
{
"author",
"organization",
"status",
"supervisor",
"rejection_reason",
},
],
]

Expand Down Expand Up @@ -359,9 +380,7 @@ def formfield_for_foreignkey(self, db_field, request, **kwargs):
.distinct()
.order_by("name")
)
elif (
db_field.name in {"author", "supervisor"} and not request.user.is_superuser
):
elif db_field.name in {"author", "supervisor"} and not request.user.is_superuser:
orgs = models.Organization.objects.filter(
Q(supervisors=request.user) | Q(execs=request.user)
)
Expand All @@ -385,9 +404,7 @@ def formfield_for_foreignkey(self, db_field, request, **kwargs):
def formfield_for_manytomany(self, db_field, request, **kwargs):
if db_field.name == "tags":
kwargs["queryset"] = (
models.Tag.objects.filter(
Q(organization=None) | Q(organization__execs=request.user)
)
models.Tag.objects.filter(Q(organization=None) | Q(organization__execs=request.user))
.distinct()
.order_by("name")
)
Expand Down Expand Up @@ -425,9 +442,7 @@ def save_model(self, request, obj, form, change):
if (not change) or obj.status not in ("d", "p"):
notify_supervisors = True

self.message_user(
request, f"Successfully sent announcement for review."
)
self.message_user(request, f"Successfully sent announcement for review.")
obj.status = "p" if obj.status != "d" else "d"

super().save_model(request, obj, form, change)
Expand Down Expand Up @@ -490,9 +505,7 @@ def get_changeform_initial_data(self, request):
def formfield_for_manytomany(self, db_field, request, **kwargs):
if db_field.name == "tags":
kwargs["queryset"] = (
models.Tag.objects.filter(
Q(organization=None) | Q(organization__execs=request.user)
)
models.Tag.objects.filter(Q(organization=None) | Q(organization__execs=request.user))
.distinct()
.order_by("name")
)
Expand Down Expand Up @@ -526,9 +539,7 @@ def get_changeform_initial_data(self, request):
def formfield_for_manytomany(self, db_field, request, **kwargs):
if db_field.name == "tags":
kwargs["queryset"] = (
models.Tag.objects.filter(Q(organization=None))
.distinct()
.order_by("name")
models.Tag.objects.filter(Q(organization=None)).distinct().order_by("name")
)
if request.user.is_superuser:
kwargs["queryset"] = models.Tag.objects.all().order_by("name")
Expand Down Expand Up @@ -559,9 +570,7 @@ def get_form(self, request, obj=None, **kwargs):
def formfield_for_manytomany(self, db_field, request, **kwargs):
if db_field.name == "tags":
kwargs["queryset"] = (
models.Tag.objects.filter(
Q(organization=None) | Q(organization__execs=request.user)
)
models.Tag.objects.filter(Q(organization=None) | Q(organization__execs=request.user))
.distinct()
.order_by("name")
)
Expand All @@ -572,9 +581,9 @@ def formfield_for_manytomany(self, db_field, request, **kwargs):
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == "organization":
if not request.user.is_superuser:
kwargs["queryset"] = models.Organization.objects.filter(
execs=request.user
).order_by("name")
kwargs["queryset"] = models.Organization.objects.filter(execs=request.user).order_by(
"name"
)
if request.user.is_superuser:
kwargs["queryset"] = models.Organization.objects.all().order_by("name")
return super().formfield_for_foreignkey(db_field, request, **kwargs)
Expand All @@ -591,9 +600,7 @@ def has_change_permission(self, request, obj=None):
def save_model(self, request, obj, form, change):
if not all(
map(
lambda date: obj.term.start_datetime()
<= date
<= obj.term.end_datetime(),
lambda date: obj.term.start_datetime() <= date <= obj.term.end_datetime(),
[obj.start_date, obj.end_date],
)
):
Expand Down Expand Up @@ -634,8 +641,7 @@ def get_inline_instances(self, request, obj=None):

def has_view_permission(self, request, obj=None):
if obj is None and (
request.user.organizations_owning.exists()
or request.user.organizations_leading.exists()
request.user.organizations_owning.exists() or request.user.organizations_leading.exists()
):
return True

Expand Down
4 changes: 1 addition & 3 deletions core/api/serializers/announcement.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@


class AnnouncementSerializer(serializers.ModelSerializer):
author = PrimaryKeyAndSlugRelatedField(
slug_field="username", queryset=models.User.objects.all()
)
author = PrimaryKeyAndSlugRelatedField(slug_field="username", queryset=models.User.objects.all())
organization = PrimaryKeyAndSlugRelatedField(
slug_field="slug", queryset=models.Organization.objects.all()
)
Expand Down
4 changes: 1 addition & 3 deletions core/api/serializers/blogpost.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@


class BlogPostSerializer(serializers.ModelSerializer):
author = PrimaryKeyAndSlugRelatedField(
slug_field="username", queryset=models.User.objects.all()
)
author = PrimaryKeyAndSlugRelatedField(slug_field="username", queryset=models.User.objects.all())
tags = TagSerializer(many=True)

class Meta:
Expand Down
26 changes: 15 additions & 11 deletions core/api/serializers/custom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ def to_internal_value(self, data):
return ContentType.objects.get(app_label="core", model=data)
except ObjectDoesNotExist:
self.fail(
"does_not_exist", slug_name=self.slug_field, value=smart_str(data)
"does_not_exist",
slug_name=self.slug_field,
value=smart_str(data),
)
except (TypeError, ValueError):
self.fail("invalid")
Expand Down Expand Up @@ -138,7 +140,9 @@ def to_internal_value(self, data):
return User.objects.get(id=data)
except ObjectDoesNotExist:
self.fail(
"does_not_exist", slug_name=self.slug_field, value=smart_str(data)
"does_not_exist",
slug_name=self.slug_field,
value=smart_str(data),
)
except (TypeError, ValueError):
self.fail("invalid")
Expand Down Expand Up @@ -171,7 +175,8 @@ def to_internal_value(self, data):
missing_ids = set(data) - set(users)
for missing_id in missing_ids:
self.fail(
"invalid", message=f"User with ID {missing_id} does not exist."
"invalid",
message=f"User with ID {missing_id} does not exist.",
)
return User.objects.filter(id__in=data)

Expand All @@ -196,9 +201,7 @@ def __init__(self, **kwargs):
value_list=("id", self.slug_field),
db_filter=dict(is_active=True),
)
default_error_messages = {
"does_not_exist": "Organization '{value}' does not exist."
}
default_error_messages = {"does_not_exist": "Organization '{value}' does not exist."}
super().__init__(choices, **kwargs)
self.default_error_messages.update(default_error_messages)

Expand All @@ -207,7 +210,9 @@ def to_internal_value(self, data):
return Organization.objects.get(pk=data)
except ObjectDoesNotExist:
self.fail(
"does_not_exist", slug_name=self.slug_field, value=smart_str(data)
"does_not_exist",
slug_name=self.slug_field,
value=smart_str(data),
)
except (TypeError, ValueError):
self.fail("invalid")
Expand All @@ -226,9 +231,7 @@ def to_internal_value(self, data):
if not isinstance(data, list):
raise serializers.ValidationError("Expected a list of organization IDs.")

organizations = Organization.objects.filter(id__in=data).values_list(
"id", flat=True
)
organizations = Organization.objects.filter(id__in=data).values_list("id", flat=True)
if len(organizations) != len(data):
missing_ids = set(data) - set(organizations)
for missing_id in missing_ids:
Expand Down Expand Up @@ -270,7 +273,8 @@ def to_internal_value(self, data):
missing_ids = set(data) - set(tags)
for missing_id in missing_ids:
self.fail(
"invalid", message=f"Tag with ID {missing_id} does not exist."
"invalid",
message=f"Tag with ID {missing_id} does not exist.",
)

return Tag.objects.filter(id__in=data)
Expand Down
4 changes: 1 addition & 3 deletions core/api/serializers/organization.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@


class OrganizationSerializer(serializers.ModelSerializer):
owner = PrimaryKeyAndSlugRelatedField(
slug_field="username", queryset=models.User.objects.all()
)
owner = PrimaryKeyAndSlugRelatedField(slug_field="username", queryset=models.User.objects.all())
supervisors = PrimaryKeyAndSlugRelatedField(
slug_field="username", many=True, queryset=models.User.objects.all()
)
Expand Down
4 changes: 1 addition & 3 deletions core/api/serializers/timetable.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@

class TimetableSerializer(serializers.ModelSerializer):
id = serializers.PrimaryKeyRelatedField(queryset=models.Tag.objects.all())
owner = PrimaryKeyAndSlugRelatedField(
slug_field="username", queryset=models.User.objects.all()
)
owner = PrimaryKeyAndSlugRelatedField(slug_field="username", queryset=models.User.objects.all())
term = TermSerializer()
courses = CourseSerializer(many=True)

Expand Down
4 changes: 3 additions & 1 deletion core/api/serializers/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
class UserSerializer(serializers.ModelSerializer):
id = serializers.PrimaryKeyRelatedField(queryset=models.Tag.objects.all())
organizations = serializers.SlugRelatedField(
slug_field="name", many=True, queryset=models.Organization.objects.all()
slug_field="name",
many=True,
queryset=models.Organization.objects.all(),
)
tags_following = TagSerializer(many=True)
gravatar_url = serializers.SerializerMethodField(read_only=True)
Expand Down
Loading

0 comments on commit 829f392

Please sign in to comment.