KintoneAppBuilder created

This commit is contained in:
2023-07-10 09:40:49 +09:00
commit 80446a3860
94 changed files with 13622 additions and 0 deletions

126
backend/.gitignore vendored Normal file
View File

@@ -0,0 +1,126 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
.python-version
# celery beat schedule file
celerybeat-schedule
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyderproject2
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# pytype static type analyzer
.pytype/
# Cython debug symbols
cython_debug/
# VS Code settings
.vscode/

13
backend/Dockerfile Normal file
View File

@@ -0,0 +1,13 @@
FROM python:3.8
RUN mkdir /app
WORKDIR /app
RUN apt update && \
apt install -y postgresql-client
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY . .

159
backend/README.en.md Normal file
View File

@@ -0,0 +1,159 @@
# KintoneAppBuilder
## Features
- **FastAPI** with Python 3.8
- **React 16** with Typescript, Redux, and react-router
- Postgres
- SqlAlchemy with Alembic for migrations
- Pytest for backend tests
- Jest for frontend tests
- Perttier/Eslint (with Airbnb style guide)
- Docker compose for easier development
- Nginx as a reverse proxy to allow backend and frontend on the same port
## Development
The only dependencies for this project should be docker and docker-compose.
### Quick Start
Starting the project with hot-reloading enabled
(the first time it will take a while):
```bash
docker-compose up -d
```
To run the alembic migrations (for the users table):
```bash
docker-compose run --rm backend alembic upgrade head
```
And navigate to http://localhost:8000
_Note: If you see an Nginx error at first with a `502: Bad Gateway` page, you may have to wait for webpack to build the development server (the nginx container builds much more quickly)._
Auto-generated docs will be at
http://localhost:8000/api/docs
### Rebuilding containers:
```
docker-compose build
```
### Restarting containers:
```
docker-compose restart
```
### Bringing containers down:
```
docker-compose down
```
### Frontend Development
Alternatively to running inside docker, it can sometimes be easier
to use npm directly for quicker reloading. To run using npm:
```
cd frontend
npm install
npm start
```
This should redirect you to http://localhost:3000
### Frontend Tests
```
cd frontend
npm install
npm test
```
## Migrations
Migrations are run using alembic. To run all migrations:
```
docker-compose run --rm backend alembic upgrade head
```
To create a new migration:
```
alembic revision -m "create users table"
```
And fill in `upgrade` and `downgrade` methods. For more information see
[Alembic's official documentation](https://alembic.sqlalchemy.org/en/latest/tutorial.html#create-a-migration-script).
## Testing
There is a helper script for both frontend and backend tests:
```
./scripts/test.sh
```
### Backend Tests
```
docker-compose run backend pytest
```
any arguments to pytest can also be passed after this command
### Frontend Tests
```
docker-compose run frontend test
```
This is the same as running npm test from within the frontend directory
## Logging
```
docker-compose logs
```
Or for a specific service:
```
docker-compose logs -f name_of_service # frontend|backend|db
```
## Project Layout
```
backend
└── app
├── alembic
│ └── versions # where migrations are located
├── api
│ └── api_v1
│ └── endpoints
├── core # config
├── db # db models
├── tests # pytest
└── main.py # entrypoint to backend
frontend
└── public
└── src
├── components
│ └── Home.tsx
├── config
│ └── index.tsx # constants
├── __tests__
│ └── test_home.tsx
├── index.tsx # entrypoint
└── App.tsx # handles routing
```

18
backend/Temp/app.json Normal file
View File

@@ -0,0 +1,18 @@
{
"appId": "5",
"code": "",
"name": "日報",
"description": "<div>日々の業務内容、報告事項、所感などを記載していくアプリです。<div>記録を行うだけでなく、あとからの振り返りやメンバー間のコミュニケーションにも活用できます。</div></div>",
"createdAt": "2023-07-08T02:03:27.000Z",
"creator": {
"code": "MXZ",
"name": "馬 小哲"
},
"modifiedAt": "2023-07-08T02:26:06.000Z",
"modifier": {
"code": "MXZ",
"name": "馬 小哲"
},
"spaceId": null,
"threadId": null
}

View File

77
backend/Temp/form.json Normal file
View File

@@ -0,0 +1,77 @@
{
"properties": [
{
"type": "CREATOR",
"label": "作成者",
"noLabel": "false",
"code": "作成者"
},
{
"type": "MULTI_LINE_TEXT",
"label": "業務内容",
"noLabel": "false",
"code": "文字列__複数行_",
"required": "false",
"defaultValue": ""
},
{
"type": "MULTI_LINE_TEXT",
"label": "所感、学び",
"noLabel": "false",
"code": "文字列__複数行__0",
"required": "false",
"defaultValue": ""
},
{
"type": "FILE",
"label": "添付ファイル",
"noLabel": "false",
"code": "添付ファイル",
"required": "false"
},
{
"type": "DATE",
"label": "日付",
"noLabel": "false",
"code": "日付",
"required": "false",
"unique": "false",
"defaultValue": null,
"defaultExpression": "NOW"
},
{
"type": "DROP_DOWN",
"label": "部署",
"noLabel": "false",
"code": "ドロップダウン",
"required": "false",
"options": [
"営業",
"マーケティング",
"総務",
"サポート",
"開発"
],
"defaultValue": null
},
{
"type": "RADIO_BUTTON",
"label": "目標達成度",
"noLabel": "false",
"code": "ラジオボタン",
"required": "true",
"options": [
"達成",
"未達"
],
"defaultValue": "達成"
},
{
"type": "USER_SELECT",
"label": "確認者",
"noLabel": "true",
"code": "ユーザー選択",
"required": "false"
}
]
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,145 @@
{
"revision": "16",
"properties": {
"レコード番号": {
"type": "RECORD_NUMBER",
"code": "レコード番号",
"label": "レコード番号",
"noLabel": false
},
"作業者": {
"type": "STATUS_ASSIGNEE",
"code": "作業者",
"label": "作業者",
"enabled": true
},
"更新者": {
"type": "MODIFIER",
"code": "更新者",
"label": "更新者",
"noLabel": false
},
"作成者": {
"type": "CREATOR",
"code": "作成者",
"label": "作成者",
"noLabel": false
},
"ステータス": {
"type": "STATUS",
"code": "ステータス",
"label": "ステータス",
"enabled": true
},
"ラジオボタン": {
"type": "RADIO_BUTTON",
"code": "ラジオボタン",
"label": "目標達成度",
"noLabel": false,
"required": true,
"options": {
"未達": {
"label": "未達",
"index": "1"
},
"達成": {
"label": "達成",
"index": "0"
}
},
"defaultValue": "達成",
"align": "HORIZONTAL"
},
"ドロップダウン": {
"type": "DROP_DOWN",
"code": "ドロップダウン",
"label": "部署",
"noLabel": false,
"required": false,
"options": {
"総務": {
"label": "総務",
"index": "2"
},
"サポート": {
"label": "サポート",
"index": "3"
},
"マーケティング": {
"label": "マーケティング",
"index": "1"
},
"営業": {
"label": "営業",
"index": "0"
},
"開発": {
"label": "開発",
"index": "4"
}
},
"defaultValue": ""
},
"ユーザー選択": {
"type": "USER_SELECT",
"code": "ユーザー選択",
"label": "確認者",
"noLabel": true,
"required": false,
"entities": [],
"defaultValue": []
},
"更新日時": {
"type": "UPDATED_TIME",
"code": "更新日時",
"label": "更新日時",
"noLabel": false
},
"カテゴリー": {
"type": "CATEGORY",
"code": "カテゴリー",
"label": "カテゴリー",
"enabled": false
},
"文字列__複数行__0": {
"type": "MULTI_LINE_TEXT",
"code": "文字列__複数行__0",
"label": "所感、学び",
"noLabel": false,
"required": false,
"defaultValue": ""
},
"文字列__複数行_": {
"type": "MULTI_LINE_TEXT",
"code": "文字列__複数行_",
"label": "業務内容",
"noLabel": false,
"required": false,
"defaultValue": ""
},
"添付ファイル": {
"type": "FILE",
"code": "添付ファイル",
"label": "添付ファイル",
"noLabel": false,
"required": false,
"thumbnailSize": "150"
},
"作成日時": {
"type": "CREATED_TIME",
"code": "作成日時",
"label": "作成日時",
"noLabel": false
},
"日付": {
"type": "DATE",
"code": "日付",
"label": "日付",
"noLabel": false,
"required": false,
"unique": false,
"defaultValue": "",
"defaultNowValue": true
}
}
}

View File

@@ -0,0 +1,83 @@
{
"layout": [
{
"type": "ROW",
"fields": [
{
"type": "DATE",
"code": "日付",
"size": {
"width": "117"
}
},
{
"type": "CREATOR",
"code": "作成者",
"size": {
"width": "120"
}
},
{
"type": "DROP_DOWN",
"code": "ドロップダウン",
"size": {
"width": "142"
}
},
{
"type": "USER_SELECT",
"code": "ユーザー選択",
"size": {
"width": "344"
}
}
]
},
{
"type": "ROW",
"fields": [
{
"type": "MULTI_LINE_TEXT",
"code": "文字列__複数行_",
"size": {
"width": "444",
"innerHeight": "115"
}
},
{
"type": "RADIO_BUTTON",
"code": "ラジオボタン",
"size": {
"width": "194"
}
}
]
},
{
"type": "ROW",
"fields": [
{
"type": "MULTI_LINE_TEXT",
"code": "文字列__複数行__0",
"size": {
"width": "444",
"innerHeight": "115"
}
}
]
},
{
"type": "ROW",
"fields": [
{
"type": "FILE",
"code": "添付ファイル",
"size": {
"width": "207"
}
}
]
}
],
"revision": "16"
}

View File

@@ -0,0 +1,83 @@
{
"layout": [
{
"type": "ROW",
"fields": [
{
"type": "DATE",
"code": "日付",
"size": {
"width": "117"
}
},
{
"type": "CREATOR",
"code": "作成者",
"size": {
"width": "120"
}
},
{
"type": "DROP_DOWN",
"code": "ドロップダウン",
"size": {
"width": "142"
}
},
{
"type": "USER_SELECT",
"code": "ユーザー選択",
"size": {
"width": "344"
}
}
]
},
{
"type": "ROW",
"fields": [
{
"type": "MULTI_LINE_TEXT",
"code": "文字列__複数行_",
"size": {
"width": "444",
"innerHeight": "115"
}
},
{
"type": "RADIO_BUTTON",
"code": "ラジオボタン",
"size": {
"width": "194"
}
}
]
},
{
"type": "ROW",
"fields": [
{
"type": "MULTI_LINE_TEXT",
"code": "文字列__複数行__0",
"size": {
"width": "444",
"innerHeight": "115"
}
}
]
},
{
"type": "ROW",
"fields": [
{
"type": "FILE",
"code": "添付ファイル",
"size": {
"width": "207"
}
}
]
}
],
"revision": "16"
}

82
backend/alembic.ini Normal file
View File

@@ -0,0 +1,82 @@
# A generic, single database configuration.
[alembic]
# path to migration scripts
script_location = app/alembic
# template used to generate migration files
# file_template = %%(rev)s_%%(slug)s
# timezone to use when rendering the date
# within the migration file as well as the filename.
# string value is passed to dateutil.tz.gettz()
# leave blank for localtime
timezone = America/Los_Angeles
# max length of characters to apply to the
# "slug" field
# truncate_slug_length = 40
# set to 'true' to run the environment during
# the 'revision' command, regardless of autogenerate
# revision_environment = false
# set to 'true' to allow .pyc and .pyo files without
# a source .py file to be detected as revisions in the
# versions/ directory
# sourceless = false
# version location specification; this defaults
# to alembic/versions. When using multiple version
# directories, initial revisions must be specified with --version-path
# version_locations = %(here)s/bar %(here)s/bat alembic/versions
# the output encoding used when revision files
# are written from script.py.mako
# output_encoding = utf-8
[post_write_hooks]
# post_write_hooks defines scripts or Python functions that are run
# on newly generated revision scripts. See the documentation for further
# detail and examples
# format using "black" - use the console_scripts runner, against the "black" entrypoint
# hooks=black
# black.type=console_scripts
# black.entrypoint=black
# black.options=-l 79
# Logging configuration
[loggers]
keys = root,sqlalchemy,alembic
[handlers]
keys = console
[formatters]
keys = generic
[logger_root]
level = WARN
handlers = console
qualname =
[logger_sqlalchemy]
level = WARN
handlers =
qualname = sqlalchemy.engine
[logger_alembic]
level = INFO
handlers =
qualname = alembic
[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic
[formatter_generic]
format = %(levelname)-5.5s [%(name)s] %(message)s
datefmt = %H:%M:%S

0
backend/app/__init__.py Normal file
View File

85
backend/app/alembic.ini Normal file
View File

@@ -0,0 +1,85 @@
# A generic, single database configuration.
[alembic]
# path to migration scripts
script_location = alembic
# template used to generate migration files
# file_template = %%(rev)s_%%(slug)s
# timezone to use when rendering the date
# within the migration file as well as the filename.
# string value is passed to dateutil.tz.gettz()
# leave blank for localtime
# timezone =
# max length of characters to apply to the
# "slug" field
# truncate_slug_length = 40
# set to 'true' to run the environment during
# the 'revision' command, regardless of autogenerate
# revision_environment = false
# set to 'true' to allow .pyc and .pyo files without
# a source .py file to be detected as revisions in the
# versions/ directory
# sourceless = false
# version location specification; this defaults
# to alembic/versions. When using multiple version
# directories, initial revisions must be specified with --version-path
# version_locations = %(here)s/bar %(here)s/bat alembic/versions
# the output encoding used when revision files
# are written from script.py.mako
# output_encoding = utf-8
sqlalchemy.url = driver://user:pass@localhost/dbname
[post_write_hooks]
# post_write_hooks defines scripts or Python functions that are run
# on newly generated revision scripts. See the documentation for further
# detail and examples
# format using "black" - use the console_scripts runner, against the "black" entrypoint
# hooks=black
# black.type=console_scripts
# black.entrypoint=black
# black.options=-l 79
# Logging configuration
[loggers]
keys = root,sqlalchemy,alembic
[handlers]
keys = console
[formatters]
keys = generic
[logger_root]
level = WARN
handlers = console
qualname =
[logger_sqlalchemy]
level = WARN
handlers =
qualname = sqlalchemy.engine
[logger_alembic]
level = INFO
handlers =
qualname = alembic
[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic
[formatter_generic]
format = %(levelname)-5.5s [%(name)s] %(message)s
datefmt = %H:%M:%S

View File

@@ -0,0 +1 @@
Generic single-database configuration.

View File

View File

@@ -0,0 +1,81 @@
import os
from logging.config import fileConfig
from alembic import context
from sqlalchemy import engine_from_config
from sqlalchemy import pool
from app.db.models import Base
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
# Interpret the config file for Python logging.
# This line sets up loggers basically.
fileConfig(config.config_file_name)
# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
target_metadata = Base.metadata
# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
# ... etc.
def get_url():
return os.getenv("DATABASE_URL")
def run_migrations_offline():
"""Run migrations in 'offline' mode.
This configures the context with just a URL
and not an Engine, though an Engine is acceptable
here as well. By skipping the Engine creation
we don't even need a DBAPI to be available.
Calls to context.execute() here emit the given string to the
script output.
"""
# url = config.get_main_option("sqlalchemy.url")
url = get_url()
context.configure(
url=url,
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
)
with context.begin_transaction():
context.run_migrations()
def run_migrations_online():
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
and associate a connection with the context.
"""
configuration = config.get_section(config.config_ini_section)
configuration["sqlalchemy.url"] = get_url()
connectable = engine_from_config(
configuration,
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)
with connectable.connect() as connection:
context.configure(
connection=connection, target_metadata=target_metadata
)
with context.begin_transaction():
context.run_migrations()
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()

View File

@@ -0,0 +1,24 @@
"""${message}
Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}
"""
from alembic import op
import sqlalchemy as sa
${imports if imports else ""}
# revision identifiers, used by Alembic.
revision = ${repr(up_revision)}
down_revision = ${repr(down_revision)}
branch_labels = ${repr(branch_labels)}
depends_on = ${repr(depends_on)}
def upgrade():
${upgrades if upgrades else "pass"}
def downgrade():
${downgrades if downgrades else "pass"}

View File

@@ -0,0 +1,34 @@
"""create users table
Revision ID: 91979b40eb38
Revises:
Create Date: 2020-03-23 14:53:53.101322
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = "91979b40eb38"
down_revision = None
branch_labels = None
depends_on = None
def upgrade():
op.create_table(
"user",
sa.Column("id", sa.Integer, primary_key=True),
sa.Column("email", sa.String(50), nullable=False),
sa.Column("first_name", sa.String(100)),
sa.Column("last_name", sa.String(100)),
sa.Column("address", sa.String(100)),
sa.Column("hashed_password", sa.String(100), nullable=False),
sa.Column("is_active", sa.Boolean, nullable=False),
sa.Column("is_superuser", sa.Boolean, nullable=False),
)
def downgrade():
op.drop_table("user")

View File

View File

View File

@@ -0,0 +1,63 @@
from fastapi.security import OAuth2PasswordRequestForm
from fastapi import APIRouter, Depends, HTTPException, status
from datetime import timedelta
from app.db.session import get_db
from app.core import security
from app.core.auth import authenticate_user, sign_up_new_user
auth_router = r = APIRouter()
@r.post("/token")
async def login(
db=Depends(get_db), form_data: OAuth2PasswordRequestForm = Depends()
):
user = authenticate_user(db, form_data.username, form_data.password)
if not user:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Incorrect username or password",
headers={"WWW-Authenticate": "Bearer"},
)
access_token_expires = timedelta(
minutes=security.ACCESS_TOKEN_EXPIRE_MINUTES
)
if user.is_superuser:
permissions = "admin"
else:
permissions = "user"
access_token = security.create_access_token(
data={"sub": user.email, "permissions": permissions},
expires_delta=access_token_expires,
)
return {"access_token": access_token, "token_type": "bearer"}
@r.post("/signup")
async def signup(
db=Depends(get_db), form_data: OAuth2PasswordRequestForm = Depends()
):
user = sign_up_new_user(db, form_data.username, form_data.password)
if not user:
raise HTTPException(
status_code=status.HTTP_409_CONFLICT,
detail="Account already exists",
headers={"WWW-Authenticate": "Bearer"},
)
access_token_expires = timedelta(
minutes=security.ACCESS_TOKEN_EXPIRE_MINUTES
)
if user.is_superuser:
permissions = "admin"
else:
permissions = "user"
access_token = security.create_access_token(
data={"sub": user.email, "permissions": permissions},
expires_delta=access_token_expires,
)
return {"access_token": access_token, "token_type": "bearer"}

View File

@@ -0,0 +1,66 @@
from app.core import security
# Monkey patch function we can use to shave a second off our tests by skipping the password hashing check
def verify_password_mock(first: str, second: str):
return True
def test_login(client, test_user, monkeypatch):
# Patch the test to skip password hashing check for speed
monkeypatch.setattr(security, "verify_password", verify_password_mock)
response = client.post(
"/api/token",
data={"username": test_user.email, "password": "nottheactualpass"},
)
assert response.status_code == 200
def test_signup(client, monkeypatch):
def get_password_hash_mock(first: str, second: str):
return True
monkeypatch.setattr(security, "get_password_hash", get_password_hash_mock)
response = client.post(
"/api/signup",
data={"username": "some@email.com", "password": "randompassword"},
)
assert response.status_code == 200
def test_resignup(client, test_user, monkeypatch):
# Patch the test to skip password hashing check for speed
monkeypatch.setattr(security, "verify_password", verify_password_mock)
response = client.post(
"/api/signup",
data={
"username": test_user.email,
"password": "password_hashing_is_skipped_via_monkey_patch",
},
)
assert response.status_code == 409
def test_wrong_password(
client, test_db, test_user, test_password, monkeypatch
):
def verify_password_failed_mock(first: str, second: str):
return False
monkeypatch.setattr(
security, "verify_password", verify_password_failed_mock
)
response = client.post(
"/api/token", data={"username": test_user.email, "password": "wrong"}
)
assert response.status_code == 401
def test_wrong_login(client, test_db, test_user, test_password):
response = client.post(
"/api/token", data={"username": "fakeuser", "password": test_password}
)
assert response.status_code == 401

View File

@@ -0,0 +1,110 @@
from app.db import models
def test_get_users(client, test_superuser, superuser_token_headers):
response = client.get("/api/v1/users", headers=superuser_token_headers)
assert response.status_code == 200
assert response.json() == [
{
"id": test_superuser.id,
"email": test_superuser.email,
"is_active": test_superuser.is_active,
"is_superuser": test_superuser.is_superuser,
}
]
def test_delete_user(client, test_superuser, test_db, superuser_token_headers):
response = client.delete(
f"/api/v1/users/{test_superuser.id}", headers=superuser_token_headers
)
assert response.status_code == 200
assert test_db.query(models.User).all() == []
def test_delete_user_not_found(client, superuser_token_headers):
response = client.delete(
"/api/v1/users/4321", headers=superuser_token_headers
)
assert response.status_code == 404
def test_edit_user(client, test_superuser, superuser_token_headers):
new_user = {
"email": "newemail@email.com",
"is_active": False,
"is_superuser": True,
"first_name": "Joe",
"last_name": "Smith",
"password": "new_password",
}
response = client.put(
f"/api/v1/users/{test_superuser.id}",
json=new_user,
headers=superuser_token_headers,
)
assert response.status_code == 200
new_user["id"] = test_superuser.id
new_user.pop("password")
assert response.json() == new_user
def test_edit_user_not_found(client, test_db, superuser_token_headers):
new_user = {
"email": "newemail@email.com",
"is_active": False,
"is_superuser": False,
"password": "new_password",
}
response = client.put(
"/api/v1/users/1234", json=new_user, headers=superuser_token_headers
)
assert response.status_code == 404
def test_get_user(
client,
test_user,
superuser_token_headers,
):
response = client.get(
f"/api/v1/users/{test_user.id}", headers=superuser_token_headers
)
assert response.status_code == 200
assert response.json() == {
"id": test_user.id,
"email": test_user.email,
"is_active": bool(test_user.is_active),
"is_superuser": test_user.is_superuser,
}
def test_user_not_found(client, superuser_token_headers):
response = client.get("/api/v1/users/123", headers=superuser_token_headers)
assert response.status_code == 404
def test_authenticated_user_me(client, user_token_headers):
response = client.get("/api/v1/users/me", headers=user_token_headers)
assert response.status_code == 200
def test_unauthenticated_routes(client):
response = client.get("/api/v1/users/me")
assert response.status_code == 401
response = client.get("/api/v1/users")
assert response.status_code == 401
response = client.get("/api/v1/users/123")
assert response.status_code == 401
response = client.put("/api/v1/users/123")
assert response.status_code == 401
response = client.delete("/api/v1/users/123")
assert response.status_code == 401
def test_unauthorized_routes(client, user_token_headers):
response = client.get("/api/v1/users", headers=user_token_headers)
assert response.status_code == 403
response = client.get("/api/v1/users/123", headers=user_token_headers)
assert response.status_code == 403

View File

@@ -0,0 +1,107 @@
from fastapi import APIRouter, Request, Depends, Response, encoders
import typing as t
from app.db.session import get_db
from app.db.crud import (
get_users,
get_user,
create_user,
delete_user,
edit_user,
)
from app.db.schemas import UserCreate, UserEdit, User, UserOut
from app.core.auth import get_current_active_user, get_current_active_superuser
users_router = r = APIRouter()
@r.get(
"/users",
response_model=t.List[User],
response_model_exclude_none=True,
)
async def users_list(
response: Response,
db=Depends(get_db),
current_user=Depends(get_current_active_superuser),
):
"""
Get all users
"""
users = get_users(db)
# This is necessary for react-admin to work
response.headers["Content-Range"] = f"0-9/{len(users)}"
return users
@r.get("/users/me", response_model=User, response_model_exclude_none=True)
async def user_me(current_user=Depends(get_current_active_user)):
"""
Get own user
"""
return current_user
@r.get(
"/users/{user_id}",
response_model=User,
response_model_exclude_none=True,
)
async def user_details(
request: Request,
user_id: int,
db=Depends(get_db),
current_user=Depends(get_current_active_superuser),
):
"""
Get any user details
"""
user = get_user(db, user_id)
return user
# return encoders.jsonable_encoder(
# user, skip_defaults=True, exclude_none=True,
# )
@r.post("/users", response_model=User, response_model_exclude_none=True)
async def user_create(
request: Request,
user: UserCreate,
db=Depends(get_db),
current_user=Depends(get_current_active_superuser),
):
"""
Create a new user
"""
return create_user(db, user)
@r.put(
"/users/{user_id}", response_model=User, response_model_exclude_none=True
)
async def user_edit(
request: Request,
user_id: int,
user: UserEdit,
db=Depends(get_db),
current_user=Depends(get_current_active_superuser),
):
"""
Update existing user
"""
return edit_user(db, user_id, user)
@r.delete(
"/users/{user_id}", response_model=User, response_model_exclude_none=True
)
async def user_delete(
request: Request,
user_id: int,
db=Depends(get_db),
current_user=Depends(get_current_active_superuser),
):
"""
Delete existing user
"""
return delete_user(db, user_id)

View File

View File

75
backend/app/core/auth.py Normal file
View File

@@ -0,0 +1,75 @@
import jwt
from fastapi import Depends, HTTPException, status
from jwt import PyJWTError
from app.db import models, schemas, session
from app.db.crud import get_user_by_email, create_user
from app.core import security
async def get_current_user(
db=Depends(session.get_db), token: str = Depends(security.oauth2_scheme)
):
credentials_exception = HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Could not validate credentials",
headers={"WWW-Authenticate": "Bearer"},
)
try:
payload = jwt.decode(
token, security.SECRET_KEY, algorithms=[security.ALGORITHM]
)
email: str = payload.get("sub")
if email is None:
raise credentials_exception
permissions: str = payload.get("permissions")
token_data = schemas.TokenData(email=email, permissions=permissions)
except PyJWTError:
raise credentials_exception
user = get_user_by_email(db, token_data.email)
if user is None:
raise credentials_exception
return user
async def get_current_active_user(
current_user: models.User = Depends(get_current_user),
):
if not current_user.is_active:
raise HTTPException(status_code=400, detail="Inactive user")
return current_user
async def get_current_active_superuser(
current_user: models.User = Depends(get_current_user),
) -> models.User:
if not current_user.is_superuser:
raise HTTPException(
status_code=403, detail="The user doesn't have enough privileges"
)
return current_user
def authenticate_user(db, email: str, password: str):
user = get_user_by_email(db, email)
if not user:
return False
if not security.verify_password(password, user.hashed_password):
return False
return user
def sign_up_new_user(db, email: str, password: str):
user = get_user_by_email(db, email)
if user:
return False # User already exists
new_user = create_user(
db,
schemas.UserCreate(
email=email,
password=password,
is_active=True,
is_superuser=False,
),
)
return new_user

View File

@@ -0,0 +1,5 @@
from celery import Celery
celery_app = Celery("worker", broker="redis://redis:6379/0")
celery_app.conf.task_routes = {"app.tasks.*": "main-queue"}

View File

@@ -0,0 +1,7 @@
import os
PROJECT_NAME = "KintoneAppBuilder"
SQLALCHEMY_DATABASE_URI = os.getenv("DATABASE_URL")
API_V1_STR = "/api/v1"

View File

@@ -0,0 +1,31 @@
import jwt
from fastapi.security import OAuth2PasswordBearer
from passlib.context import CryptContext
from datetime import datetime, timedelta
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/api/token")
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
SECRET_KEY = "alicorns"
ALGORITHM = "HS256"
ACCESS_TOKEN_EXPIRE_MINUTES = 30
def get_password_hash(password: str) -> str:
return pwd_context.hash(password)
def verify_password(plain_password: str, hashed_password: str) -> bool:
return pwd_context.verify(plain_password, hashed_password)
def create_access_token(*, data: dict, expires_delta: timedelta = None):
to_encode = data.copy()
if expires_delta:
expire = datetime.utcnow() + expires_delta
else:
expire = datetime.utcnow() + timedelta(minutes=15)
to_encode.update({"exp": expire})
encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
return encoded_jwt

View File

69
backend/app/db/crud.py Normal file
View File

@@ -0,0 +1,69 @@
from fastapi import HTTPException, status
from sqlalchemy.orm import Session
import typing as t
from . import models, schemas
from app.core.security import get_password_hash
def get_user(db: Session, user_id: int):
user = db.query(models.User).filter(models.User.id == user_id).first()
if not user:
raise HTTPException(status_code=404, detail="User not found")
return user
def get_user_by_email(db: Session, email: str) -> schemas.UserBase:
return db.query(models.User).filter(models.User.email == email).first()
def get_users(
db: Session, skip: int = 0, limit: int = 100
) -> t.List[schemas.UserOut]:
return db.query(models.User).offset(skip).limit(limit).all()
def create_user(db: Session, user: schemas.UserCreate):
hashed_password = get_password_hash(user.password)
db_user = models.User(
first_name=user.first_name,
last_name=user.last_name,
email=user.email,
is_active=user.is_active,
is_superuser=user.is_superuser,
hashed_password=hashed_password,
)
db.add(db_user)
db.commit()
db.refresh(db_user)
return db_user
def delete_user(db: Session, user_id: int):
user = get_user(db, user_id)
if not user:
raise HTTPException(status.HTTP_404_NOT_FOUND, detail="User not found")
db.delete(user)
db.commit()
return user
def edit_user(
db: Session, user_id: int, user: schemas.UserEdit
) -> schemas.User:
db_user = get_user(db, user_id)
if not db_user:
raise HTTPException(status.HTTP_404_NOT_FOUND, detail="User not found")
update_data = user.dict(exclude_unset=True)
if "password" in update_data:
update_data["hashed_password"] = get_password_hash(user.password)
del update_data["password"]
for key, value in update_data.items():
setattr(db_user, key, value)
db.add(db_user)
db.commit()
db.refresh(db_user)
return db_user

15
backend/app/db/models.py Normal file
View File

@@ -0,0 +1,15 @@
from sqlalchemy import Boolean, Column, Integer, String
from .session import Base
class User(Base):
__tablename__ = "user"
id = Column(Integer, primary_key=True, index=True)
email = Column(String, unique=True, index=True, nullable=False)
first_name = Column(String)
last_name = Column(String)
hashed_password = Column(String, nullable=False)
is_active = Column(Boolean, default=True)
is_superuser = Column(Boolean, default=False)

45
backend/app/db/schemas.py Normal file
View File

@@ -0,0 +1,45 @@
from pydantic import BaseModel
import typing as t
class UserBase(BaseModel):
email: str
is_active: bool = True
is_superuser: bool = False
first_name: str = None
last_name: str = None
class UserOut(UserBase):
pass
class UserCreate(UserBase):
password: str
class Config:
orm_mode = True
class UserEdit(UserBase):
password: t.Optional[str] = None
class Config:
orm_mode = True
class User(UserBase):
id: int
class Config:
orm_mode = True
class Token(BaseModel):
access_token: str
token_type: str
class TokenData(BaseModel):
email: str = None
permissions: str = "user"

21
backend/app/db/session.py Normal file
View File

@@ -0,0 +1,21 @@
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from app.core import config
engine = create_engine(
config.SQLALCHEMY_DATABASE_URI,
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()
# Dependency
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()

View File

@@ -0,0 +1,26 @@
#!/usr/bin/env python3
from app.db.session import get_db
from app.db.crud import create_user
from app.db.schemas import UserCreate
from app.db.session import SessionLocal
def init() -> None:
db = SessionLocal()
create_user(
db,
UserCreate(
email="maxiaozhe@alicorns.co.jp",
password="maxz1205",
is_active=True,
is_superuser=True,
),
)
if __name__ == "__main__":
print("Creating superuser maxiaozhe@alicorns.co.jp")
init()
print("Superuser created")

49
backend/app/main.py Normal file
View File

@@ -0,0 +1,49 @@
from fastapi import FastAPI, Depends
from starlette.requests import Request
import uvicorn
from app.api.api_v1.routers.users import users_router
from app.api.api_v1.routers.auth import auth_router
from app.core import config
from app.db.session import SessionLocal
from app.core.auth import get_current_active_user
from app.core.celery_app import celery_app
from app import tasks
app = FastAPI(
title=config.PROJECT_NAME, docs_url="/api/docs", openapi_url="/api"
)
@app.middleware("http")
async def db_session_middleware(request: Request, call_next):
request.state.db = SessionLocal()
response = await call_next(request)
request.state.db.close()
return response
@app.get("/api/v1")
async def root():
return {"message": "Hello World"}
@app.get("/api/v1/task")
async def example_task():
celery_app.send_task("app.tasks.example_task", args=["Hello World"])
return {"message": "success"}
# Routers
app.include_router(
users_router,
prefix="/api/v1",
tags=["users"],
dependencies=[Depends(get_current_active_user)],
)
app.include_router(auth_router, prefix="/api", tags=["auth"])
if __name__ == "__main__":
uvicorn.run("main:app", host="0.0.0.0", reload=True, port=8888)

6
backend/app/tasks.py Normal file
View File

@@ -0,0 +1,6 @@
from app.core.celery_app import celery_app
@celery_app.task(acks_late=True)
def example_task(word: str) -> str:
return f"test task returns {word}"

View File

View File

@@ -0,0 +1,4 @@
def test_read_main(client):
response = client.get("/api/v1")
assert response.status_code == 200
assert response.json() == {"message": "Hello World"}

View File

@@ -0,0 +1,6 @@
from app import tasks
def test_example_task():
task_output = tasks.example_task("Hello World")
assert task_output == "test task returns Hello World"

169
backend/conftest.py Normal file
View File

@@ -0,0 +1,169 @@
import pytest
from sqlalchemy import create_engine, event
from sqlalchemy.orm import sessionmaker
from sqlalchemy_utils import database_exists, create_database, drop_database
from fastapi.testclient import TestClient
import typing as t
from app.core import config, security
from app.db.session import Base, get_db
from app.db import models
from app.main import app
def get_test_db_url() -> str:
return f"{config.SQLALCHEMY_DATABASE_URI}_test"
@pytest.fixture
def test_db():
"""
Modify the db session to automatically roll back after each test.
This is to avoid tests affecting the database state of other tests.
"""
# Connect to the test database
engine = create_engine(
get_test_db_url(),
)
connection = engine.connect()
trans = connection.begin()
# Run a parent transaction that can roll back all changes
test_session_maker = sessionmaker(
autocommit=False, autoflush=False, bind=engine
)
test_session = test_session_maker()
test_session.begin_nested()
@event.listens_for(test_session, "after_transaction_end")
def restart_savepoint(s, transaction):
if transaction.nested and not transaction._parent.nested:
s.expire_all()
s.begin_nested()
yield test_session
# Roll back the parent transaction after the test is complete
test_session.close()
trans.rollback()
connection.close()
@pytest.fixture(scope="session", autouse=True)
def create_test_db():
"""
Create a test database and use it for the whole test session.
"""
test_db_url = get_test_db_url()
# Create the test database
assert not database_exists(
test_db_url
), "Test database already exists. Aborting tests."
create_database(test_db_url)
test_engine = create_engine(test_db_url)
Base.metadata.create_all(test_engine)
# Run the tests
yield
# Drop the test database
drop_database(test_db_url)
@pytest.fixture
def client(test_db):
"""
Get a TestClient instance that reads/write to the test database.
"""
def get_test_db():
yield test_db
app.dependency_overrides[get_db] = get_test_db
yield TestClient(app)
@pytest.fixture
def test_password() -> str:
return "securepassword"
def get_password_hash() -> str:
"""
Password hashing can be expensive so a mock will be much faster
"""
return "supersecrethash"
@pytest.fixture
def test_user(test_db) -> models.User:
"""
Make a test user in the database
"""
user = models.User(
email="fake@email.com",
hashed_password=get_password_hash(),
is_active=True,
)
test_db.add(user)
test_db.commit()
return user
@pytest.fixture
def test_superuser(test_db) -> models.User:
"""
Superuser for testing
"""
user = models.User(
email="fakeadmin@email.com",
hashed_password=get_password_hash(),
is_superuser=True,
)
test_db.add(user)
test_db.commit()
return user
def verify_password_mock(first: str, second: str) -> bool:
return True
@pytest.fixture
def user_token_headers(
client: TestClient, test_user, test_password, monkeypatch
) -> t.Dict[str, str]:
monkeypatch.setattr(security, "verify_password", verify_password_mock)
login_data = {
"username": test_user.email,
"password": test_password,
}
r = client.post("/api/token", data=login_data)
tokens = r.json()
a_token = tokens["access_token"]
headers = {"Authorization": f"Bearer {a_token}"}
return headers
@pytest.fixture
def superuser_token_headers(
client: TestClient, test_superuser, test_password, monkeypatch
) -> t.Dict[str, str]:
monkeypatch.setattr(security, "verify_password", verify_password_mock)
login_data = {
"username": test_superuser.email,
"password": test_password,
}
r = client.post("/api/token", data=login_data)
tokens = r.json()
a_token = tokens["access_token"]
headers = {"Authorization": f"Bearer {a_token}"}
return headers

2
backend/pyproject.toml Normal file
View File

@@ -0,0 +1,2 @@
[tool.black]
line-length = 80

27
backend/readme.md Normal file
View File

@@ -0,0 +1,27 @@
### 1: Pythonインストール
https://www.python.org/downloads/
### 2. venv設定
```
#仮想環境を設定する
python -m venv env
#仮想環境をアクティベート
.\env\Scripts\activate
```
### 4FastAPIとUvicornのインストール
仮想環境にFastAPIとUvicornASGIサーバーをインストールします。
1. fastapi
```bash
pip install fastapi
```
2. Uvicornをインストールします
```bash
pip install uvicorn
```
3. すべて依頼をインストール
```bash
pip install -r requirements.txt
```

159
backend/readme.zh.md Normal file
View File

@@ -0,0 +1,159 @@
# KintoneAppBuilder
## 特性
- 使用 Python 3.8 的 **FastAPI**
- 结合 Typescript、Redux 和 react-router 的 **React 16**
- Postgres 数据库
- 用于迁移的 SqlAlchemy 和 Alembic
- 后端测试的 Pytest
- 前端测试的 Jest
- 遵循 Airbnb 代码风格的 Prettier/Eslint
- 方便开发的 Docker compose
- 用于实现后端和前端在同一端口上的 Nginx 反向代理
## 开发
此项目唯一的依赖应为 docker 和 docker-compose。
### 快速开始
启动项目并启用热加载
(首次操作可能需要一些时间):
```bash
docker-compose up -d
```
运行 alembic 迁移(针对 users 表):
```bash
docker-compose run --rm backend alembic upgrade head
```
然后,导航到 http://localhost:8000
_注如果你最初看到一个带有 `502Bad Gateway` 页面的 Nginx 错误,你可能需要等待 webpack 构建开发服务器nginx 容器构建得更快。_
自动生成的文档将位于
http://localhost:8000/api/docs
### 重建容器:
```
docker-compose build
```
### 重启容器:
```
docker-compose restart
```
### 关闭容器:
```
docker-compose down
```
### 前端开发
作为 docker 内部运行的替代方案,直接使用 npm 有时更简单,
以便于更快的重加载。使用 npm 运行:
```
cd frontend
npm install
npm start
```
这应将你重定向到 http://localhost:3000
### 前端测试
```
cd frontend
npm install
npm test
```
## 迁移
使用 alembic 运行迁移。运行所有迁移:
```
docker-compose run --rm backend alembic upgrade head
```
创建新的迁移:
```
alembic revision -m "create users table"
```
并填写 `upgrade``downgrade` 方法。更多信息请参阅
[Alembic 的官方文档](https://alembic.sqlalchemy.org/en/latest/tutorial.html#create-a-migration-script)。
## 测试
存在一个用于前端和后端测试的辅助脚本:
```
./scripts/test.sh
```
### 后端测试
```
docker-compose run backend pytest
```
此命令之后也可以传递给 pytest 的任何参数
### 前端测试
```
docker-compose run frontend test
```
这与从前端目录内运行 npm test 是相同的
## 日志
```
docker-compose logs
```
或针对特定服务:
```
docker-compose logs -f name_of_service # frontend|backend|db
```
## 项目布局
```
backend
└── app
├── alembic
│ └── versions # 迁移位置
├── api
│ └── api_v1
│ └── endpoints
├── core # 配置
├── db # 数据库模型
├── tests # pytest
└── main.py # 后端入口点
frontend
└── public
└── src
├── components
│ └── Home.tsx
├── config
│ └── index.tsx # 常量
├── __tests__
│ └── test_home.tsx
├── index.tsx # 入口点
└── App.tsx # 处理路由
```

19
backend/requirements.txt Normal file
View File

@@ -0,0 +1,19 @@
alembic==1.4.3
Authlib==0.14.3
fastapi==0.65.2
celery==5.0.0
redis==3.5.3
httpx==0.15.5
ipython==7.31.1
itsdangerous==1.1.0
Jinja2==2.11.3
psycopg2
pytest==6.1.0
requests==2.24.0
SQLAlchemy==1.3.19
uvicorn==0.12.1
passlib==1.7.2
bcrypt==3.2.0
sqlalchemy-utils==0.36.8
python-multipart==0.0.5
pyjwt==1.7.1