Skip to content

Commit

Permalink
chore: integrate pre-commit (#1117)
Browse files Browse the repository at this point in the history
* integrate pre-commit
* run isort with profile black
* run Trim Trailing Whitespace
* run end-of-file-fixer
* fix flake8 F401: import not used error
* fix flake8 F841: local variable assigned to but never used error
* fix flake8 F541: f-string is missing placeholders error
* make isort black compatible
* run black compatible isort
* fix E712: comparison to True should be 'if cond:' error
* fix E266: too many leading '#' error
* fix F403: 'import *' used error
* fix E402: module level import not at top of file error
* remove ignored errors from .flake8
* document pre-commit and isort
* ignore W503, E501 & C901
* Update README.md
* Update README.md
* don't remove migrate
  • Loading branch information
KushalBeniwal authored Jul 11, 2021
1 parent 2b294ca commit 4193589
Show file tree
Hide file tree
Showing 128 changed files with 435 additions and 324 deletions.
2 changes: 1 addition & 1 deletion .coveragerc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[run]
omit =
omit =
# omit everything in the tests directory
tests/*
13 changes: 13 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[flake8]
ignore =
# W503: line break before binary operator
# ignored as it creates inconsistency with black
W503,
# E501: line too long (> 88 characters)
# ignored till a final decision is made on what to do with long comments
E501,
# C901: variable or function name is too complex (> 18 characters)
# ignored till someone simplifies the names
C901
max-line-length = 88
max-complexity = 18
6 changes: 3 additions & 3 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
* When developing a new feature, include at least one test when applicable.
* When submitting a PR, please follow [this template](PULL_REQUEST_TEMPLATE.md) (which will probably be already filled up once you create the PR).
* When submitting a PR with changes to user interface (e.g.: new screen, or an email template), please add screenshots to the PR description.
* When you are finished with your work, please squash your commits otherwise we will squash them on your PR (this can help us maintain a clear commit history).
* When you are finished with your work, please squash your commits otherwise we will squash them on your PR (this can help us maintain a clear commit history).
* When writing in-code documentation as Docstrings, please follow [this guide](https://github.com/google/styleguide/blob/gh-pages/pyguide.md#38-comments-and-docstrings).
* When creating an issue to report a bug in the project, please follow our [bug_report.md](https://github.com/anitab-org/.github/blob/main/.github/ISSUE_TEMPLATE/bug_report.md) template.

## General Guidelines

* If you’re just getting started work on an issue labeled “First Timers Only” in any project. Additional resources are available on our [website](http://www.systers.io).
* In an active repository (not an archived one), choose an open issue from the issue list, claim it in the comments, and a maintainer will assign it to you.
* In an active repository (not an archived one), choose an open issue from the issue list, claim it in the comments, and a maintainer will assign it to you.
* After approval you must make continuous notes on your progress in the issue while working. If there is not at least one comment every 3 days, the maintainer can reassign the issue.
* Create a branch specific to the issue you're working on, so that you send a PR from that branch instead of the base branch on your fork.
* If you’d like to create a new issue, please go through our issue list first (open as well as closed) and make sure the issues you are reporting do not replicate the existing issues.
* If you’d like to create a new issue, please go through our issue list first (open as well as closed) and make sure the issues you are reporting do not replicate the existing issues.
* Have a short description on what has gone wrong (like a root cause analysis and description of the fix), if that information is not already present in the issue.
* If you have issues on multiple pages, report them separately. Do not combine them into a single issue.
2 changes: 1 addition & 1 deletion .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Fixes # [ISSUE]

**Code/Quality Assurance Only**

- [ ] My changes generate no new warnings
- [ ] My changes generate no new warnings
- [ ] My PR currently breaks something (fix or feature that would cause existing functionality to not work as expected)
- [ ] I have added tests that prove my fix is effective or that my feature works
- [ ] New and existing unit tests pass locally with my changes
Expand Down
4 changes: 2 additions & 2 deletions .github/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ newIssueWelcomeComment: |
Hello there!👋 Welcome to the project!💖
Thank you and congrats 🎉 for opening your very first issue in this project.
AnitaB.org Open Source is an inclusive community, committed to creating a safe and positive environment 🌸 Please follow our [Code Of Conduct](https://github.com/anitab-org/mentorship-backend/blob/develop/docs/code_of_conduct.md) 🙌
AnitaB.org Open Source is an inclusive community, committed to creating a safe and positive environment 🌸 Please follow our [Code Of Conduct](https://github.com/anitab-org/mentorship-backend/blob/develop/docs/code_of_conduct.md) 🙌
You can learn more about the community on [our website](https://anitab-org.github.io/) and [documentation repository](https://github.com/anitab-org/documentation#readme).
You may submit a PR on the same if you like!
Expand Down Expand Up @@ -39,7 +39,7 @@ firstPRMergeComment: |
Woohoo!🎊 Congrats on merging your first contribution to [mentorship-backend](https://github.com/anitab-org/mentorship-backend)!✨
Your code is officially a part of AnitaB.org Open Source now!! Time for a celebration 🙌🎉⚡️
Feel free to explore more on this project.🤗
Join us on [AnitaB.org Open Source Zulip](https://anitab-org.zulipchat.com/). 🤝
Join us on [AnitaB.org Open Source Zulip](https://anitab-org.zulipchat.com/). 🤝
We have different streams for each active repository for discussions. 👩🏽‍💻🧑🏼‍💻
Do join our weekly check-in stream to share your weekly goals and the awesome work you did here.😃
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ name: Lint Code and Run Tests
on:
pull_request:
branches: [develop]
paths-ignore:
paths-ignore:
- '**.md'
- /docs/CODEOWNERS
push:
branches: [develop]
paths-ignore:
paths-ignore:
- '**.md'
- /docs/CODEOWNERS

Expand All @@ -21,7 +21,7 @@ jobs:
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
- uses: psf/black@stable
- uses: psf/black@stable

build:
needs: lint
Expand Down
9 changes: 9 additions & 0 deletions .isort.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[tool.isort]
profile = "black"
multi_line_output = 3

[settings]
line_length = 88
multi_line_output = 3
include_trailing_comma = True
known_third_party = celery,django,environ,pyquery,pytz,redis,requests,rest_framework
21 changes: 21 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.3.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/psf/black
rev: 21.5b1
hooks:
- id: black
args: [--check]
- repo: https://gitlab.com/pycqa/flake8
rev: 3.9.2
hooks:
- id: flake8
- repo: https://github.com/pycqa/isort
rev: 5.8.0
hooks:
- id: isort
args: [--profile, black, -c]
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,35 @@ To run black:
black .
```

#### Sorting imports with isort

We use [_isort_](https://pypi.org/project/isort/) to sort imports alphabetically, and automatically separated into
sections and by type. To install _isort_:

```
pip install isort
```

To run isort:

```
isort . --profile=black
```

#### Automate auto-formatting tools with pre-commit

We use [_pre-commit_](https://pre-commit.com/) to check formatting before every commit message. To install _pre-commit_:

```
pip install pre-commit
```

To set up the git hook scripts:

```
pre-commit install
```

## Documentation

Documentation for the project is hosted [here](https://anitab-org.github.io/mentorship-backend/). We use Docusaurus for maintaining the documentation of the project.
Expand Down
5 changes: 3 additions & 2 deletions app/api/api_extension.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
from flask_restx import Api

# Adding namespaces
from app.api.resources.user import users_ns as user_namespace
from app.api.resources.admin import admin_ns as admin_namespace
from app.api.resources.mentorship_relation import (
mentorship_relation_ns as mentorship_namespace,
)
from app.api.resources.task import task_ns as task_namespace
from app.api.resources.task_comment import task_comment_ns as task_comment_namespace

# Adding namespaces
from app.api.resources.user import users_ns as user_namespace


def androidlink():
return "<a href=https://github.com/anitab-org/mentorship-android>Android</a>"
Expand Down
5 changes: 3 additions & 2 deletions app/api/dao/admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Dict
from http import HTTPStatus
from typing import Dict

from app import messages
from app.database.models.user import UserModel
from app.utils.decorator_utils import email_verification_required
Expand Down Expand Up @@ -65,7 +66,7 @@ def revoke_admin_user(user_id: int, data: Dict[str, str]):
"""
admin_user_id = data["user_id"]

admin_count = UserModel.query.filter(UserModel.is_admin == True).count()
admin_count = UserModel.query.filter(UserModel.is_admin).count()

if user_id == admin_user_id and admin_count == 1:
return messages.USER_CANNOT_REVOKE_ADMIN_STATUS, HTTPStatus.FORBIDDEN
Expand Down
6 changes: 2 additions & 4 deletions app/api/dao/mentorship_relation.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from datetime import datetime, timedelta
from typing import Dict
from http import HTTPStatus
from typing import Dict

from app import messages
from app.database.models.mentorship_relation import MentorshipRelationModel
from app.database.models.tasks_list import TasksListModel
Expand Down Expand Up @@ -242,7 +243,6 @@ def reject_request(user_id: int, request_id: int):
message: A message corresponding to the completed action; success if mentorship relation request is rejected, failure if otherwise.
"""

user = UserModel.find_by_id(user_id)
request = MentorshipRelationModel.find_by_id(request_id)

# verify if request exists
Expand Down Expand Up @@ -286,7 +286,6 @@ def cancel_relation(user_id: int, relation_id: int):
message: A message corresponding to the completed action; success if mentorship relation is terminated, failure if otherwise.
"""

user = UserModel.find_by_id(user_id)
request = MentorshipRelationModel.find_by_id(relation_id)

# verify if request exists
Expand Down Expand Up @@ -325,7 +324,6 @@ def delete_request(user_id: int, request_id: int):
message: A message corresponding to the completed action; success if mentorship relation request is deleted, failure if otherwise.
"""

user = UserModel.find_by_id(user_id)
request = MentorshipRelationModel.find_by_id(request_id)

# verify if request exists
Expand Down
8 changes: 2 additions & 6 deletions app/api/dao/task.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from datetime import datetime
from typing import Dict
from http import HTTPStatus
from typing import Dict

from app import messages
from app.database.models.mentorship_relation import MentorshipRelationModel
from app.database.models.user import UserModel
from app.utils.decorator_utils import email_verification_required
from app.utils.enum_utils import MentorshipRelationState

Expand Down Expand Up @@ -31,7 +31,6 @@ def create_task(user_id: int, mentorship_relation_id: int, data: Dict[str, str])

description = data["description"]

user = UserModel.find_by_id(user_id)
relation = MentorshipRelationModel.find_by_id(_id=mentorship_relation_id)
if relation is None:
return messages.MENTORSHIP_RELATION_DOES_NOT_EXIST, HTTPStatus.NOT_FOUND
Expand Down Expand Up @@ -68,7 +67,6 @@ def list_tasks(user_id: int, mentorship_relation_id: int):
mentorship relation as a string. The last element is the HTTP response code
"""

user = UserModel.find_by_id(user_id)
relation = MentorshipRelationModel.find_by_id(mentorship_relation_id)
if relation is None:
return messages.MENTORSHIP_RELATION_DOES_NOT_EXIST, HTTPStatus.NOT_FOUND
Expand Down Expand Up @@ -101,7 +99,6 @@ def delete_task(user_id: int, mentorship_relation_id: int, task_id: int):
task was deleted successfully or not as a string. The last element is the HTTP response code.
"""

user = UserModel.find_by_id(user_id)
relation = MentorshipRelationModel.find_by_id(mentorship_relation_id)
if relation is None:
return messages.MENTORSHIP_RELATION_DOES_NOT_EXIST, HTTPStatus.NOT_FOUND
Expand Down Expand Up @@ -138,7 +135,6 @@ def complete_task(user_id: int, mentorship_relation_id: int, task_id: int):
if the task was set to complete successfully or not as a string. The last element is the HTTP response code.
"""

user = UserModel.find_by_id(user_id)
relation = MentorshipRelationModel.find_by_id(mentorship_relation_id)
if relation is None:
return messages.MENTORSHIP_RELATION_DOES_NOT_EXIST, HTTPStatus.NOT_FOUND
Expand Down
3 changes: 2 additions & 1 deletion app/api/dao/task_comment.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from http import HTTPStatus

from app import messages
from app.database.models.mentorship_relation import MentorshipRelationModel
from app.database.models.task_comment import TaskCommentModel
from app.utils.decorator_utils import email_verification_required
from app.utils.enum_utils import MentorshipRelationState
from http import HTTPStatus


def validate_data_for_task_comment(user_id, task_id, relation_id):
Expand Down
9 changes: 5 additions & 4 deletions app/api/dao/user.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
from datetime import datetime
from operator import itemgetter
from http import HTTPStatus
from operator import itemgetter
from typing import Dict

from flask_restx import marshal
from sqlalchemy import func

from app import messages
from app.api.dao.mentorship_relation import MentorshipRelationDAO
from app.api.email_utils import confirm_token
from app.api.models.task import list_tasks_response_body
from app.database.models.mentorship_relation import MentorshipRelationModel
from app.database.models.user import UserModel
from app.utils.decorator_utils import email_verification_required
from app.utils.enum_utils import MentorshipRelationState
from app.database.models.mentorship_relation import MentorshipRelationModel
from app.api.models.task import list_tasks_response_body
from app.api.dao.mentorship_relation import MentorshipRelationDAO
from app.utils.validation_utils import is_email_valid


Expand Down
12 changes: 6 additions & 6 deletions app/api/email_utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import datetime
from itsdangerous import URLSafeTimedSerializer, BadSignature

from flask_mail import Message
from flask import render_template
from flask_mail import Message
from itsdangerous import BadSignature, URLSafeTimedSerializer

import config
from app.api.mail_extension import mail
Expand Down Expand Up @@ -81,10 +81,10 @@ def send_email_verification_message(user_name, email):
email: User email address.
"""
confirmation_token = generate_confirmation_token(email)
from app.api.resources.user import (
UserEmailConfirmation,
) # import here to avoid circular imports
from app.api.api_extension import api
from app.api.resources.user import ( # import here to avoid circular imports
UserEmailConfirmation,
)

confirm_url = api.url_for(
UserEmailConfirmation, token=confirmation_token, _external=True
Expand All @@ -108,8 +108,8 @@ def send_email_mentorship_relation_accepted(request_id):
request_id: Request id of the mentorship request.
"""

from app.database.models.user import UserModel
from app.database.models.mentorship_relation import MentorshipRelationModel
from app.database.models.user import UserModel

# Getting the request from id.
request = MentorshipRelationModel.find_by_id(request_id)
Expand Down
4 changes: 3 additions & 1 deletion app/api/jwt_extension.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from flask_jwt_extended import JWTManager
from http import HTTPStatus

from flask_jwt_extended import JWTManager

from app import messages
from app.api.api_extension import api

Expand Down
2 changes: 1 addition & 1 deletion app/api/models/admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from flask_restx import fields, Model
from flask_restx import Model, fields


def add_models_to_namespace(api_namespace):
Expand Down
3 changes: 2 additions & 1 deletion app/api/models/mentorship_relation.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from flask_restx import fields, Model
from flask_restx import Model, fields

from app.utils.enum_utils import MentorshipRelationState

from .task import create_task_request_body, list_tasks_response_body
from .task_comment import task_comment_model, task_comments_model

Expand Down
2 changes: 1 addition & 1 deletion app/api/models/task.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from flask_restx import fields, Model
from flask_restx import Model, fields


def add_models_to_namespace(api_namespace):
Expand Down
2 changes: 1 addition & 1 deletion app/api/models/task_comment.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from flask_restx import fields, Model
from flask_restx import Model, fields


def add_models_to_namespace(api_namespace):
Expand Down
3 changes: 2 additions & 1 deletion app/api/models/user.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from flask_restx import fields, Model
from flask_restx import Model, fields

from app.api.models.mentorship_relation import (
mentorship_request_response_body_for_user_dashboard_body,
)
Expand Down
Loading

0 comments on commit 4193589

Please sign in to comment.