From e224fed272b0a75919a115ab5f9b3c622c6d14d3 Mon Sep 17 00:00:00 2001 From: RyanAquino Date: Tue, 21 Dec 2021 11:53:14 +0800 Subject: [PATCH] ZDL-94: Integrate isort package --- .github/workflows/django-cicd.yml | 7 ++++-- .isort.cfg | 2 ++ authentication/admin.py | 1 + authentication/models.py | 5 ++-- authentication/serializers.py | 5 ++-- .../tests/factories/group_factory.py | 2 +- .../tests/factories/user_factory.py | 5 ++-- authentication/tests/test_auth_api.py | 6 ++--- authentication/urls.py | 5 ++-- authentication/views.py | 8 +++--- conftest.py | 5 ++-- orders/admin.py | 2 +- orders/migrations/0001_initial.py | 2 +- orders/migrations/0002_auto_20210917_2012.py | 2 +- orders/models.py | 7 +++--- orders/serializers.py | 1 + orders/tests/factories/order_factory.py | 8 +++--- orders/tests/factories/order_item_factory.py | 10 +++++--- orders/tests/test_orders_api.py | 6 ++--- orders/urls.py | 4 +-- orders/validators.py | 3 ++- orders/views.py | 25 +++++++++---------- products/admin.py | 1 + products/migrations/0001_initial.py | 2 +- products/models.py | 2 +- products/serializers.py | 1 + products/tests/factories/product_factory.py | 5 ++-- products/tests/test_products_api.py | 5 ++-- products/urls.py | 4 +-- products/views.py | 14 ++++++----- requirements.txt | 3 +++ zadalaAPI/settings.py | 5 ++-- zadalaAPI/urls.py | 12 ++++----- 33 files changed, 101 insertions(+), 74 deletions(-) create mode 100644 .isort.cfg diff --git a/.github/workflows/django-cicd.yml b/.github/workflows/django-cicd.yml index 6a9d93c..35dfde5 100644 --- a/.github/workflows/django-cicd.yml +++ b/.github/workflows/django-cicd.yml @@ -18,7 +18,7 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip - pip install black flake8 pytest mypy + pip install black flake8 pytest mypy isort - name: Lint with flake8 run: | flake8 . --statistics @@ -27,6 +27,9 @@ jobs: mypy . - name: Check formatting run: black . --exclude=venv --check + - name: Check isort imports + run: | + isort . --check-only unit-test: runs-on: ubuntu-latest services: @@ -50,6 +53,6 @@ jobs: if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - name: Run migrations run: python manage.py migrate - - name: Test with pytest + - name: Test with pytest with coverage run: | pytest . --cov \ No newline at end of file diff --git a/.isort.cfg b/.isort.cfg new file mode 100644 index 0000000..bf7b9fb --- /dev/null +++ b/.isort.cfg @@ -0,0 +1,2 @@ +[settings] +profile=black \ No newline at end of file diff --git a/authentication/admin.py b/authentication/admin.py index e7ec47e..7ddae1e 100644 --- a/authentication/admin.py +++ b/authentication/admin.py @@ -1,4 +1,5 @@ from django.contrib import admin + from authentication.models import User admin.site.register(User) diff --git a/authentication/models.py b/authentication/models.py index c67dd63..e853638 100644 --- a/authentication/models.py +++ b/authentication/models.py @@ -1,11 +1,12 @@ -from django.db import models from django.contrib.auth.models import ( AbstractBaseUser, - Group, BaseUserManager, + Group, PermissionsMixin, ) +from django.db import models from rest_framework_simplejwt.tokens import RefreshToken + from authentication.validators import UserTokens diff --git a/authentication/serializers.py b/authentication/serializers.py index 0a1c9d2..9d85399 100644 --- a/authentication/serializers.py +++ b/authentication/serializers.py @@ -1,7 +1,8 @@ -from rest_framework import serializers -from authentication.models import User from django.contrib import auth +from rest_framework import serializers from rest_framework.exceptions import AuthenticationFailed + +from authentication.models import User from authentication.validators import UserLogin diff --git a/authentication/tests/factories/group_factory.py b/authentication/tests/factories/group_factory.py index 6e7b1a8..8d322f7 100644 --- a/authentication/tests/factories/group_factory.py +++ b/authentication/tests/factories/group_factory.py @@ -1,5 +1,5 @@ -from django.contrib.auth.models import Group import factory +from django.contrib.auth.models import Group class CustomersGroupFactory(factory.django.DjangoModelFactory): diff --git a/authentication/tests/factories/user_factory.py b/authentication/tests/factories/user_factory.py index 416838c..e18df8a 100644 --- a/authentication/tests/factories/user_factory.py +++ b/authentication/tests/factories/user_factory.py @@ -1,7 +1,8 @@ -from authentication.models import User +import factory from factory import PostGenerationMethodCall from factory.django import DjangoModelFactory -import factory + +from authentication.models import User class UserFactory(DjangoModelFactory): diff --git a/authentication/tests/test_auth_api.py b/authentication/tests/test_auth_api.py index 1aa983e..c786ff5 100644 --- a/authentication/tests/test_auth_api.py +++ b/authentication/tests/test_auth_api.py @@ -1,10 +1,10 @@ +import pytest +from django.contrib.auth.models import Group from django.test import Client from django.test.client import MULTIPART_CONTENT -from authentication.tests.factories.user_factory import UserFactory from authentication.models import User -from django.contrib.auth.models import Group -import pytest +from authentication.tests.factories.user_factory import UserFactory @pytest.mark.django_db diff --git a/authentication/urls.py b/authentication/urls.py index eb96d49..12079ab 100644 --- a/authentication/urls.py +++ b/authentication/urls.py @@ -1,9 +1,10 @@ from django.urls import path + from .views import ( - UserRegisterView, - UserLoginView, SupplierRegisterView, + UserLoginView, UserProfileView, + UserRegisterView, ) urlpatterns = [ diff --git a/authentication/views.py b/authentication/views.py index 430364a..71487c2 100644 --- a/authentication/views.py +++ b/authentication/views.py @@ -1,16 +1,16 @@ from rest_condition import Or +from rest_framework import status from rest_framework.generics import GenericAPIView from rest_framework.permissions import IsAuthenticated +from rest_framework.response import Response from .permissions import CustomerAccessPermission, SupplierAccessPermission from .serializers import ( - UserSerializer, - UserLoginSerializers, SupplierSerializer, + UserLoginSerializers, UserProfileSerializer, + UserSerializer, ) -from rest_framework.response import Response -from rest_framework import status class UserRegisterView(GenericAPIView): diff --git a/conftest.py b/conftest.py index 4d10aaf..124d320 100644 --- a/conftest.py +++ b/conftest.py @@ -1,8 +1,9 @@ import pytest -from authentication.tests.factories.user_factory import UserFactory +from django.contrib.auth.models import Group from django.test.client import Client from pytest_django.lazy_django import skip_if_no_django -from django.contrib.auth.models import Group + +from authentication.tests.factories.user_factory import UserFactory @pytest.fixture diff --git a/orders/admin.py b/orders/admin.py index 1cab970..788b24a 100644 --- a/orders/admin.py +++ b/orders/admin.py @@ -1,6 +1,6 @@ from django.contrib import admin -from .models import Order, OrderItem, ShippingAddress +from .models import Order, OrderItem, ShippingAddress admin.site.register(Order) admin.site.register(OrderItem) diff --git a/orders/migrations/0001_initial.py b/orders/migrations/0001_initial.py index 13eb39b..a811e1e 100644 --- a/orders/migrations/0001_initial.py +++ b/orders/migrations/0001_initial.py @@ -1,8 +1,8 @@ # Generated by Django 3.0.8 on 2021-01-08 02:01 +import django.db.models.deletion from django.conf import settings from django.db import migrations, models -import django.db.models.deletion class Migration(migrations.Migration): diff --git a/orders/migrations/0002_auto_20210917_2012.py b/orders/migrations/0002_auto_20210917_2012.py index 02e10b1..8f1df9b 100644 --- a/orders/migrations/0002_auto_20210917_2012.py +++ b/orders/migrations/0002_auto_20210917_2012.py @@ -1,7 +1,7 @@ # Generated by Django 3.0.8 on 2021-09-17 12:12 -from django.db import migrations, models import django.utils.timezone +from django.db import migrations, models class Migration(migrations.Migration): diff --git a/orders/models.py b/orders/models.py index d739b44..a3c185d 100644 --- a/orders/models.py +++ b/orders/models.py @@ -1,9 +1,10 @@ -from django.utils import timezone from django.db import models -from authentication.models import User -from products.models import Product from django.db.models.signals import post_delete from django.dispatch import receiver +from django.utils import timezone + +from authentication.models import User +from products.models import Product class Order(models.Model): diff --git a/orders/serializers.py b/orders/serializers.py index aeb20b1..6e78430 100644 --- a/orders/serializers.py +++ b/orders/serializers.py @@ -1,4 +1,5 @@ from rest_framework import serializers + from orders.models import Order, OrderItem, ShippingAddress from products.serializers import ProductSerializer diff --git a/orders/tests/factories/order_factory.py b/orders/tests/factories/order_factory.py index cf15baf..f37a00a 100644 --- a/orders/tests/factories/order_factory.py +++ b/orders/tests/factories/order_factory.py @@ -1,8 +1,10 @@ -from orders.models import Order +import datetime + +import factory from factory.django import DjangoModelFactory + from authentication.tests.factories.user_factory import UserFactory -import factory -import datetime +from orders.models import Order class OrderFactory(DjangoModelFactory): diff --git a/orders/tests/factories/order_item_factory.py b/orders/tests/factories/order_item_factory.py index 300caaa..a907675 100644 --- a/orders/tests/factories/order_item_factory.py +++ b/orders/tests/factories/order_item_factory.py @@ -1,9 +1,11 @@ +import datetime + +import factory +from factory.django import DjangoModelFactory + from orders.models import OrderItem -from products.tests.factories.product_factory import ProductFactory from orders.tests.factories.order_factory import OrderFactory -from factory.django import DjangoModelFactory -import factory -import datetime +from products.tests.factories.product_factory import ProductFactory class OrderItemFactory(DjangoModelFactory): diff --git a/orders/tests/test_orders_api.py b/orders/tests/test_orders_api.py index f0c4723..529e1fd 100644 --- a/orders/tests/test_orders_api.py +++ b/orders/tests/test_orders_api.py @@ -1,10 +1,10 @@ from datetime import datetime -from orders.tests.factories.order_item_factory import OrderItemFactory -from orders.tests.factories.order_factory import OrderFactory -from orders.models import OrderItem, Order import pytest +from orders.models import Order, OrderItem +from orders.tests.factories.order_factory import OrderFactory +from orders.tests.factories.order_item_factory import OrderItemFactory from products.tests.factories.product_factory import ProductFactory diff --git a/orders/urls.py b/orders/urls.py index 743c64b..9983337 100644 --- a/orders/urls.py +++ b/orders/urls.py @@ -1,7 +1,7 @@ -from orders.views import OrderViewSet -from django.urls import path, include +from django.urls import include, path from rest_framework.routers import DefaultRouter +from orders.views import OrderViewSet router = DefaultRouter() router.register("", OrderViewSet) diff --git a/orders/validators.py b/orders/validators.py index db2b7b7..96b2a89 100644 --- a/orders/validators.py +++ b/orders/validators.py @@ -1,6 +1,7 @@ -from pydantic import BaseModel from typing import Dict, List +from pydantic import BaseModel + class OrdersList(BaseModel): total_items: int diff --git a/orders/views.py b/orders/views.py index 88962e8..d86ee41 100644 --- a/orders/views.py +++ b/orders/views.py @@ -1,27 +1,26 @@ +import datetime + +from rest_condition import Or +from rest_framework import mixins, status, viewsets +from rest_framework.decorators import action from rest_framework.generics import get_object_or_404 +from rest_framework.permissions import IsAuthenticated +from rest_framework.response import Response +from authentication.permissions import ( + CustomerAccessPermission, + SupplierAccessPermission, +) from orders.exceptions import UnprocessableEntity from orders.models import Order, OrderItem, ShippingAddress -from rest_framework.response import Response from orders.serializers import ( OrderItemSerializer, OrderSerializer, - UpdateCartSerializer, ShippingAddressSerializer, + UpdateCartSerializer, ) -from rest_framework.permissions import IsAuthenticated -from authentication.permissions import ( - CustomerAccessPermission, - SupplierAccessPermission, -) -from rest_framework import mixins, viewsets -from rest_framework.decorators import action -from rest_condition import Or - from orders.validators import OrdersList from products.models import Product -from rest_framework import status -import datetime class OrderViewSet(mixins.ListModelMixin, viewsets.GenericViewSet): diff --git a/products/admin.py b/products/admin.py index 3c835b1..2225ef2 100644 --- a/products/admin.py +++ b/products/admin.py @@ -1,4 +1,5 @@ from django.contrib import admin + from products.models import Product admin.site.register(Product) diff --git a/products/migrations/0001_initial.py b/products/migrations/0001_initial.py index 7bf9502..b0aacf4 100644 --- a/products/migrations/0001_initial.py +++ b/products/migrations/0001_initial.py @@ -1,8 +1,8 @@ # Generated by Django 3.0.8 on 2021-01-08 02:01 +import django.db.models.deletion from django.conf import settings from django.db import migrations, models -import django.db.models.deletion class Migration(migrations.Migration): diff --git a/products/models.py b/products/models.py index 7a02554..f3d4531 100644 --- a/products/models.py +++ b/products/models.py @@ -1,5 +1,5 @@ -from django.db import models from django.conf import settings +from django.db import models class Product(models.Model): diff --git a/products/serializers.py b/products/serializers.py index eef7cd4..efa486a 100644 --- a/products/serializers.py +++ b/products/serializers.py @@ -1,4 +1,5 @@ from rest_framework import serializers + from products.models import Product diff --git a/products/tests/factories/product_factory.py b/products/tests/factories/product_factory.py index b6a09c0..a714dbd 100644 --- a/products/tests/factories/product_factory.py +++ b/products/tests/factories/product_factory.py @@ -1,6 +1,7 @@ -from products.models import Product -from factory.django import DjangoModelFactory import factory +from factory.django import DjangoModelFactory + +from products.models import Product class ProductFactory(DjangoModelFactory): diff --git a/products/tests/test_products_api.py b/products/tests/test_products_api.py index 82a0585..76c2569 100644 --- a/products/tests/test_products_api.py +++ b/products/tests/test_products_api.py @@ -1,8 +1,9 @@ import pytest -from products.tests.factories.product_factory import ProductFactory -from products.models import Product from django.test.client import MULTIPART_CONTENT +from products.models import Product +from products.tests.factories.product_factory import ProductFactory + @pytest.mark.django_db def test_list_all_products_with_empty_db(logged_in_client): diff --git a/products/urls.py b/products/urls.py index b0fd36d..9af8e05 100644 --- a/products/urls.py +++ b/products/urls.py @@ -1,7 +1,7 @@ -from products.views import ProductViewSet +from django.urls import include, path from rest_framework import routers -from django.urls import path, include +from products.views import ProductViewSet router = routers.DefaultRouter() router.register("", ProductViewSet) diff --git a/products/views.py b/products/views.py index b8cb486..0d47c40 100644 --- a/products/views.py +++ b/products/views.py @@ -1,11 +1,13 @@ -from products.models import Product +from rest_framework import filters +from rest_framework.pagination import PageNumberPagination +from rest_framework.parsers import MultiPartParser from rest_framework.permissions import IsAuthenticatedOrReadOnly -from .serializers import ProductSerializer -from authentication.permissions import SupplierAccessPermission from rest_framework.viewsets import ModelViewSet -from rest_framework.parsers import MultiPartParser -from rest_framework.pagination import PageNumberPagination -from rest_framework import filters + +from authentication.permissions import SupplierAccessPermission +from products.models import Product + +from .serializers import ProductSerializer class ProductViewSet(ModelViewSet): diff --git a/requirements.txt b/requirements.txt index 124f9d9..1e4cf70 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,10 +1,12 @@ appdirs==1.4.4 asgiref==3.2.10 +atomicwrites==1.4.0 attrs==19.3.0 black==20.8b1 certifi==2020.6.20 chardet==3.0.4 click==7.1.2 +colorama==0.4.4 coreapi==2.3.3 coreschema==0.0.4 coverage==5.3.1 @@ -21,6 +23,7 @@ idna==2.10 importlib-metadata==1.7.0 inflection==0.5.1 iniconfig==1.0.1 +isort==5.10.1 itypes==1.2.0 Jinja2==2.11.2 MarkupSafe==1.1.1 diff --git a/zadalaAPI/settings.py b/zadalaAPI/settings.py index 9a03cf0..b551436 100644 --- a/zadalaAPI/settings.py +++ b/zadalaAPI/settings.py @@ -9,9 +9,10 @@ For the full list of settings and their values, see https://docs.djangoproject.com/en/3.0/ref/settings/ """ -from zadala_config import database, ZADALA_SECRET_KEY -import os import datetime +import os + +from zadala_config import ZADALA_SECRET_KEY, database # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) diff --git a/zadalaAPI/urls.py b/zadalaAPI/urls.py index f031e1c..a84a0c8 100644 --- a/zadalaAPI/urls.py +++ b/zadalaAPI/urls.py @@ -13,14 +13,14 @@ 1. Import the include() function: from django.urls import include, path 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ -from django.contrib import admin -from django.urls import path, include -from django.conf.urls.static import static from django.conf import settings -from rest_framework_simplejwt.views import TokenRefreshView -from rest_framework import permissions -from drf_yasg.views import get_schema_view +from django.conf.urls.static import static +from django.contrib import admin +from django.urls import include, path from drf_yasg import openapi +from drf_yasg.views import get_schema_view +from rest_framework import permissions +from rest_framework_simplejwt.views import TokenRefreshView schema_view = get_schema_view( openapi.Info(