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

Linting with flake8 #261

Merged
merged 3 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
33 changes: 33 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[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,
# Missing whitespace after ',', ';', or ':' (E231)
E231,
# Line too long (82 > 79 characters) (E501)
E501,
# E251 unexpected spaces around keyword / parameter equals
E251,
# E502 the backslash is redundant between brackets
E502,
# E128 continuation line under-indented for visual indent
E128,
# E125 continuation line with same indent as next logical line
E125,
# E131 continuation line unaligned for hanging indent
E131,
# E129 visually indented line with same indent as next logical line
E129,
# Multiple spaces after ',' (E241)
E241,
E402,

exclude = apps/mappings/apps.py, apps/xero/admin.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we excluding these two files?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

they had imports like signal, django.contrib.admin which are needed but it was giving unused error.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ignore those lines only by adding a comment in the previous line, don't ignore the whole file

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assign back review once you fix this

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 All @@ -32,4 +32,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/production_deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,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
35 changes: 35 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
default_stages: [commit]
fail_fast: true
exclude: ^(sql/|(.*\/)?migrations\/)
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/timothycrosley/isort
rev: 5.12.0
hooks:
- id: isort
args: ["--profile", "black", --trailing-comma, --line-length=125]

- 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-xero-api/
WORKDIR /fyle-xero-api

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

# Expose development port
EXPOSE 8000
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Django Rest Framework API for Fyle Xero Integration
```
$ mv 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 Xero Integration
XERO_REDIRECT_URI: http://localhost:4200/workspaces/xero/callback
XERO_TOKEN_URI: https://identity.xero.com/connect/token
```

* Build docker images

```
Expand All @@ -53,7 +53,7 @@ Django Rest Framework API for Fyle Xero Integration
```

* To tail the logs a service you can do

```
docker-compose logs -f <api / qcluster>
```
Expand Down
106 changes: 48 additions & 58 deletions apps/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import logging

from rest_framework.views import status
from rest_framework.response import Response
from rest_framework.views import status
from xerosdk.exceptions import (
InternalServerError,
InvalidClientError,
InvalidGrant,
InvalidTokenError,
UnsuccessfulAuthentication,
WrongParamsError,
)

from xerosdk.exceptions import InvalidGrant, InvalidTokenError, UnsuccessfulAuthentication, \
WrongParamsError, InvalidClientError, InternalServerError
from apps.workspaces.models import XeroCredentials, Workspace, FyleCredential, \
WorkspaceGeneralSettings, WorkspaceSchedule
from apps.fyle.models import ExpenseGroup
from apps.mappings.models import GeneralMapping, TenantMapping
from apps.workspaces.models import FyleCredential, Workspace, WorkspaceGeneralSettings, WorkspaceSchedule, XeroCredentials

logger = logging.getLogger(__name__)
logger.level = logging.INFO
Expand All @@ -21,103 +26,88 @@ 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, InvalidTokenError) as exception:
logger.info('Xero token expired workspace_id - %s %s', kwargs['workspace_id'],
{'error': exception.response})
logger.info(
"Xero token expired workspace_id - %s %s",
kwargs["workspace_id"],
{"error": exception.response},
)
return Response(
data={
'message': 'Xero token expired workspace_id'
},
status=status.HTTP_400_BAD_REQUEST
data={"message": "Xero 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 (InvalidGrant, UnsuccessfulAuthentication, InvalidClientError) as exception:
except (
InvalidGrant,
UnsuccessfulAuthentication,
InvalidClientError,
) as exception:
logger.info(exception)
return Response(
data={
'message': 'Xero connection expired'
},
status=status.HTTP_400_BAD_REQUEST
data={"message": "Xero connection expired"},
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 XeroCredentials.DoesNotExist:
logger.info('Xero credentials not found in workspace')
logger.info("Xero credentials not found in workspace")
return Response(
data={
'message': 'Xero credentials not found in workspace'
},
status=status.HTTP_400_BAD_REQUEST
data={"message": "Xero credentials not found in workspace"},
status=status.HTTP_400_BAD_REQUEST,
)

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

except InternalServerError as exception:
except InternalServerError:
return Response(
{
'message': 'Internal server error'
},
status=status.HTTP_500_INTERNAL_SERVER_ERROR
)
{"message": "Internal server error"},
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
)

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
Loading