Skip to content

Commit

Permalink
Introduce flake8 for linting (#455)
Browse files Browse the repository at this point in the history
* liniting added

* liniting added

* liniting added

* liniting added

* flake8 improved

* flake8 improved

* flake8 improved

* flake8 improved

* flake8 improved

* flake8 improved

* flake8 improved

* Readme Updated

* Readme Updated

* Resolved all linting issues

* Resolved all linting issues

* comments added to flake8
  • Loading branch information
ruuushhh authored Jul 10, 2023
1 parent 77de3f9 commit 2dd01f7
Show file tree
Hide file tree
Showing 277 changed files with 23,058 additions and 9,695 deletions.
12 changes: 12 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[flake8]
extend-ignore =
# Comparison to true should be 'if cond is true:' or 'if cond:'
E712,
# Comparison to None should be 'cond is None:' (E711)
E711,
# Line break occurred before a binary operator (W503)
W503
max-line-length = 99
max-complexity = 19
ban-relative-imports = true
select = B,C,E,F,N,W,I25
4 changes: 2 additions & 2 deletions .github/workflows/codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: ubuntu-latest
environment: CI Environment
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v2
- uses: satackey/[email protected]
continue-on-error: true
- name: Bring up Services and Run Tests
Expand Down Expand Up @@ -42,4 +42,4 @@ jobs:
run: docker compose -f docker-compose-pipeline.yml down
- name: Evaluate Coverage
if: ${{ (env.STATUS == 'FAIL') || (env.FAILED > 0) }}
run: exit 1
run: exit 1
2 changes: 1 addition & 1 deletion .github/workflows/staging_deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,4 @@ jobs:
sentry-cli releases finalize $SENTRY_RELEASE
# Create new deploy for this Sentry release
sentry-cli releases deploys $SENTRY_RELEASE new -e $SENTRY_DEPLOY_ENVIRONMENT
sentry-cli releases deploys $SENTRY_RELEASE new -e $SENTRY_DEPLOY_ENVIRONMENT
39 changes: 39 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
default_stages: [commit]
fail_fast: true
default_language_version:
python: python3

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
hooks:
- id: check-ast
- id: check-merge-conflict
- id: debug-statements
- id: detect-private-key
- id: end-of-file-fixer
- id: trailing-whitespace

- repo: https://github.com/psf/black
rev: 22.3.0
hooks:
- id: black

- repo: https://github.com/timothycrosley/isort
rev: 5.12.0
hooks:
- id: isort
args: ["--profile", "black"]

- repo: https://github.com/pycqa/flake8
rev: 3.9.2
hooks:
- id: flake8
additional_dependencies: [flake8-isort, flake8-tidy-imports]

- repo: https://github.com/Lucas-C/pre-commit-hooks-bandit
rev: v1.0.5
hooks:
- id: python-bandit-vulnerability-check
args: [ -lll, --recursive, -c, bandit.yaml, .]
files: .py$
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ RUN if [ "$CI" = "ENABLED" ]; then \
apt -y update; \
apt-get install postgresql-15 -y --no-install-recommends; \
fi

# Installing requirements
COPY requirements.txt /tmp/requirements.txt
RUN pip install --upgrade pip && pip install -r /tmp/requirements.txt && pip install pylint-django==2.3.0
RUN pip install --upgrade pip && pip install -r /tmp/requirements.txt && pip install flake8


# Copy Project to the container
Expand All @@ -29,7 +29,7 @@ COPY . /fyle-qbo-api/
WORKDIR /fyle-qbo-api

# Do linting checks
RUN pylint --load-plugins pylint_django --rcfile=.pylintrc apps/**.py
RUN flake8 .

# Expose development port
EXPOSE 8000
Expand Down
28 changes: 23 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Django Rest Framework API for Fyle Quickbooks Online Integration
```
$ cp docker-compose.yml.template docker-compose.yml
```
* Setup environment variables in docker_compose.yml
```yaml
Expand All @@ -33,7 +33,7 @@ Django Rest Framework API for Fyle Quickbooks Online Integration
QBO_TOKEN_URI:
QBO_ENVIRONMENT:
```

* Build docker images

```
Expand All @@ -53,7 +53,7 @@ Django Rest Framework API for Fyle Quickbooks Online Integration
```
* To tail the logs of a service you can do
```
docker-compose logs -f <api / qcluster>
```
Expand Down Expand Up @@ -126,5 +126,23 @@ Django Rest Framework API for Fyle Quickbooks Online Integration
* Run the following command to update tests SQL fixture (`tests/sql_fixtures/reset_db_fixtures/reset_db.sql`)
```
docker-compose -f docker-compose-pipeline.yml exec api /bin/bash tests/sql_fixtures/migration_fixtures/create_migration.sh
```
docker-compose -f docker-compose-pipeline.yml exec api /bin/bash tests/sql_fixtures/migration_fixtures/create_migration.sh
```
### Working with pre commit hooks ###
* Run below command to install pre commit, if it is not already installed.
pre commit version must be at least 3.3.1.
```shell
brew install pre-commit
```
* To set up pre commit hook in your local development environment run below command.
This step may take a little longer to complete.
Once done pre commit hooks will automatically run on changed files when you do `git commit`.
```shell
pre-commit install --install-hooks
```
* To run pre commit hooks on all files run below command
```shell
pre-commit run --all-files
```
90 changes: 40 additions & 50 deletions apps/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import logging

from rest_framework.views import status
from qbosdk.exceptions import InvalidTokenError, WrongParamsError
from rest_framework.response import Response
from qbosdk.exceptions import WrongParamsError, InvalidTokenError
from rest_framework.views import status

from apps.workspaces.models import QBOCredential, FyleCredential
from apps.fyle.models import ExpenseGroup
from apps.mappings.models import GeneralMapping
from apps.workspaces.models import Workspace, WorkspaceSchedule, WorkspaceGeneralSettings

from apps.workspaces.models import (
FyleCredential,
QBOCredential,
Workspace,
WorkspaceGeneralSettings,
WorkspaceSchedule,
)

logger = logging.getLogger(__name__)
logger.level = logging.INFO
Expand All @@ -21,90 +25,76 @@ def new_fn(*args, **kwargs):
return func(*args, **kwargs)
except ExpenseGroup.DoesNotExist:
return Response(
data={
'message': 'Expense group not found'
},
status=status.HTTP_400_BAD_REQUEST
data={"message": "Expense group not found"},
status=status.HTTP_400_BAD_REQUEST,
)

except FyleCredential.DoesNotExist:
return Response(
data={
'message': 'Fyle credentials not found in workspace'
},
status=status.HTTP_400_BAD_REQUEST
data={"message": "Fyle credentials not found in workspace"},
status=status.HTTP_400_BAD_REQUEST,
)

except GeneralMapping.DoesNotExist:
return Response(
{
'message': 'General mappings do not exist for the workspace'
},
status=status.HTTP_400_BAD_REQUEST
{"message": "General mappings do not exist for the workspace"},
status=status.HTTP_400_BAD_REQUEST,
)

except WrongParamsError as exception:
logger.info('QBO token expired workspace_id - %s %s', kwargs['workspace_id'],
{'error': exception.response})
logger.info(
"QBO token expired workspace_id - %s %s",
kwargs["workspace_id"],
{"error": exception.response},
)
return Response(
data={
'message': 'QBO token expired workspace_id'
},
status=status.HTTP_400_BAD_REQUEST
data={"message": "QBO token expired workspace_id"},
status=status.HTTP_400_BAD_REQUEST,
)

except InvalidTokenError as exception:
logger.info('QBO token expired workspace_id - %s %s', kwargs['workspace_id'], {
'error': exception.response})
logger.info(
"QBO token expired workspace_id - %s %s",
kwargs["workspace_id"],
{"error": exception.response},
)
return Response(
data={
'message': 'QBO token expired workspace_id'
},
status=status.HTTP_400_BAD_REQUEST
data={"message": "QBO token expired workspace_id"},
status=status.HTTP_400_BAD_REQUEST,
)

except Workspace.DoesNotExist:
return Response(
data={
'message': 'Workspace with this id does not exist'
},
status=status.HTTP_400_BAD_REQUEST
data={"message": "Workspace with this id does not exist"},
status=status.HTTP_400_BAD_REQUEST,
)


except WorkspaceSchedule.DoesNotExist:
return Response(
data={
'message': 'Workspace schedule does not exist in workspace'
},
status=status.HTTP_400_BAD_REQUEST
data={"message": "Workspace schedule does not exist in workspace"},
status=status.HTTP_400_BAD_REQUEST,
)

except WorkspaceGeneralSettings.DoesNotExist:
return Response(
{
'message': 'General Settings does not exist in workspace'
},
status=status.HTTP_400_BAD_REQUEST
{"message": "General Settings does not exist in workspace"},
status=status.HTTP_400_BAD_REQUEST,
)


except QBOCredential.DoesNotExist:
logger.info('QBO credentials not found in workspace')
logger.info("QBO credentials not found in workspace")
return Response(
data={
'message': 'QBO credentials not found in workspace'
},
status=status.HTTP_400_BAD_REQUEST
data={"message": "QBO credentials not found in workspace"},
status=status.HTTP_400_BAD_REQUEST,
)

except Exception as exception:
logger.exception(exception)
return Response(
data={
'message': 'An unhandled error has occurred, please re-try later'
"message": "An unhandled error has occurred, please re-try later"
},
status=status.HTTP_400_BAD_REQUEST
status=status.HTTP_400_BAD_REQUEST,
)

return new_fn
Expand Down
Loading

0 comments on commit 2dd01f7

Please sign in to comment.