Skip to content

Commit

Permalink
Format code by black and isort.
Browse files Browse the repository at this point in the history
Update .pre-commit-config.yaml file.
Add github action CI.
  • Loading branch information
joe-nghiem-goldenowl committed Dec 10, 2023
1 parent 7559de4 commit 3e16723
Show file tree
Hide file tree
Showing 21 changed files with 250 additions and 137 deletions.
55 changes: 55 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
name: Tests CI

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
tests:
runs-on: ubuntu-latest
strategy:
fail-fast: false
max-parallel: 4
matrix:
python-version: [3.12.x]

services:
postgres:
image: postgres:14-alpine
env:
POSTGRES_PASSWORD: postgres
ports:
- 5432:5432

steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip -r requirements/test.txt
- name: Set up databases
run: |
PGPASSWORD="postgres" createuser -U postgres -d django_template --superuser -h localhost
PGPASSWORD="postgres" createdb -U postgres -O django_template django_template -h localhost
- name: Load fixture
run: |
python manage.py loaddata authentication/fixtures/customer.json --app authentication.customer
- name: Check linter
run: |
flake8 .
- name: Format code
run: |
black .
isort .
- name: Run unit test and report coverage
run: |
coverage run --source='.' manage.py test
coverage report
12 changes: 10 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
repos:
- repo: https://github.com/psf/black
rev: 22.12.0
rev: "23.11.0"
hooks:
- id: black
- id: black
- repo: https://github.com/pycqa/isort
rev: "5.12.0"
hooks:
- id: isort
- repo: https://github.com/pycqa/flake8
rev: "6.1.0"
hooks:
- id: flake8
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ Visit `http://127.0.0.1:8000/swagger/` to check API document.

`python manage.py test`

## Load seed data
## Load fixture data

`python manage.py loaddata authentication/fixtures/customer.json --app authentication.customer`

Expand Down
7 changes: 6 additions & 1 deletion authentication/admin/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
from django.contrib import admin

from authentication.models import *

from .customer_admin import CustomerAdmin

register_list = (
(Customer, CustomerAdmin, ),
(
Customer,
CustomerAdmin,
),
)

for register_item in register_list:
Expand Down
15 changes: 12 additions & 3 deletions authentication/admin/customer_admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
from django.contrib import admin


class CustomerAdmin(admin.ModelAdmin):
list_display = ('id', 'bio', 'birth_date', 'address', )
search_fields = ('id', 'address', )
list_filter = ('birth_date', )
list_display = (
"id",
"bio",
"birth_date",
"address",
)
search_fields = (
"id",
"address",
)
list_filter = ("birth_date",)
2 changes: 1 addition & 1 deletion authentication/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@


class AuthConfig(AppConfig):
name = 'authentication'
name = "authentication"
4 changes: 2 additions & 2 deletions authentication/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Generated by Django 4.2 on 2023-12-09 08:14

from django.conf import settings
import django.contrib.auth.models
from django.db import migrations, models
import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):
Expand Down
3 changes: 2 additions & 1 deletion authentication/models/customer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.db import models
from django.contrib.auth.models import User
from django.db import models


# Create your models here.
class Customer(User):
Expand Down
15 changes: 8 additions & 7 deletions authentication/serializers/register_serializer.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
from rest_framework import serializers
from django.contrib.auth.models import User
from rest_framework.validators import UniqueValidator
from django.contrib.auth.password_validation import validate_password
from rest_framework import serializers
from rest_framework.validators import UniqueValidator


class RegisterSerializer(serializers.ModelSerializer):
email = serializers.EmailField(
required=True,
validators=[UniqueValidator(queryset=User.objects.all())]
required=True, validators=[UniqueValidator(queryset=User.objects.all())]
)

password = serializers.CharField(write_only=True, required=True, validators=[validate_password])
password = serializers.CharField(
write_only=True, required=True, validators=[validate_password]
)

class Meta:
model = User
fields = ('username', 'password', 'email', 'first_name', 'last_name')
fields = ("username", "password", "email", "first_name", "last_name")

def create(self, validated_data):
user = User.objects.create(**validated_data)

user.set_password(validated_data['password'])
user.set_password(validated_data["password"])
user.save()

return user
7 changes: 5 additions & 2 deletions authentication/tasks.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from django_template.celery import Celery

app = Celery('tasks',)
app = Celery(
"tasks",
)

@app.task(name='run_every_monday_morning')

@app.task(name="run_every_monday_morning")
def add(x, y):
return x + y
29 changes: 18 additions & 11 deletions authentication/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
from django.test import TestCase
from django.contrib.auth.models import User
from django.test import TestCase
from rest_framework_simplejwt.tokens import RefreshToken

class TestCalls(TestCase):

class TestCalls(TestCase):
@classmethod
def setUpTestData(cls):
cls.user = User.objects.create_user(username="nguyen.hung", password="1234ABbcd!@", email="nguyen.hung@gmail.com")
cls.user = User.objects.create_user(
username="nguyen.hung",
password="1234ABbcd!@",
email="nguyen.hung@gmail.com",
)

def test_call_register(self):
data = {
"username": "bao.binh",
"password": "1234ABbcd!@",
"email": "bao.binh@gmail.com",
"first_name": "bao",
"last_name": "binh"
"last_name": "binh",
}

response = self.client.post("/api/v1/auth/register/", data, content_type="application/json")
response = self.client.post(
"/api/v1/auth/register/", data, content_type="application/json"
)

self.assertEqual(response.status_code, 201)
self.assertIn("username", response.json())
Expand All @@ -27,12 +33,11 @@ def test_call_register(self):
self.assertEqual(response.json().get("username"), "bao.binh")

def test_call_login(self):
data = {
"username": "nguyen.hung",
"password": "1234ABbcd!@"
}
data = {"username": "nguyen.hung", "password": "1234ABbcd!@"}

response = self.client.post("/api/v1/auth/login/", data, content_type="application/json")
response = self.client.post(
"/api/v1/auth/login/", data, content_type="application/json"
)

self.assertEqual(response.status_code, 200)
self.assertIn("access", response.json())
Expand All @@ -44,7 +49,9 @@ def test_call_refresh_token(self):
"refresh": str(refresh),
}

response = self.client.post("/api/v1/auth/token/refresh/", data, content_type="application/json")
response = self.client.post(
"/api/v1/auth/token/refresh/", data, content_type="application/json"
)

self.assertEqual(response.status_code, 200)
self.assertIn("access", response.json())
9 changes: 5 additions & 4 deletions authentication/urls.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from django.urls import path
from authentication.views import RegisterView
from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView

from authentication.views import RegisterView

urlpatterns = [
path('login/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
path('token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
path('register/', RegisterView.as_view(), name='auth_register'),
path("login/", TokenObtainPairView.as_view(), name="token_obtain_pair"),
path("token/refresh/", TokenRefreshView.as_view(), name="token_refresh"),
path("register/", RegisterView.as_view(), name="auth_register"),
]
1 change: 1 addition & 0 deletions authentication/views/register_view.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.contrib.auth.models import User
from rest_framework import generics
from rest_framework.permissions import AllowAny

from authentication.serializers import RegisterSerializer


Expand Down
2 changes: 1 addition & 1 deletion django_template/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
# Django starts so that shared_task will use this app.
from .celery import app as celery_app

__all__ = ('celery_app',)
__all__ = ("celery_app",)
2 changes: 1 addition & 1 deletion django_template/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@

from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_template.settings.dev')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_template.settings.dev")

application = get_asgi_application()
13 changes: 9 additions & 4 deletions django_template/celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,24 @@
from django.conf import settings

# Set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_template.settings.dev')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_template.settings.dev")

app = Celery('django_template', backend=settings.CELERY_RESULT_BACKEND, broker=settings.BROKER_URL)
app = Celery(
"django_template",
backend=settings.CELERY_RESULT_BACKEND,
broker=settings.BROKER_URL,
)

# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
# should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')
app.config_from_object("django.conf:settings", namespace="CELERY")

# Load task modules from all registered Django apps.
app.autodiscover_tasks()


@app.task(bind=True)
def debug_task(self):
print(f'Request: {self.request!r}')
print(f"Request: {self.request!r}")
Loading

0 comments on commit 3e16723

Please sign in to comment.