add project base
This commit is contained in:
4
.env.example
Normal file
4
.env.example
Normal file
@@ -0,0 +1,4 @@
|
||||
POSTGRES_USER=postgres
|
||||
POSTGRES_PASSWORD=postgres
|
||||
POSTGRES_DB=amap
|
||||
ROOT_FQDN=http://localhost
|
||||
176
.gitignore
vendored
Normal file
176
.gitignore
vendored
Normal file
@@ -0,0 +1,176 @@
|
||||
# Created by https://www.toptal.com/developers/gitignore/api/python
|
||||
# Edit at https://www.toptal.com/developers/gitignore?templates=python
|
||||
|
||||
### Python ###
|
||||
# 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/
|
||||
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
|
||||
*.py,cover
|
||||
.hypothesis/
|
||||
.pytest_cache/
|
||||
cover/
|
||||
|
||||
# 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
|
||||
.pybuilder/
|
||||
target/
|
||||
|
||||
# Jupyter Notebook
|
||||
.ipynb_checkpoints
|
||||
|
||||
# IPython
|
||||
profile_default/
|
||||
ipython_config.py
|
||||
|
||||
# pyenv
|
||||
# For a library or package, you might want to ignore these files since the code is
|
||||
# intended to run in multiple environments; otherwise, check them in:
|
||||
# .python-version
|
||||
|
||||
# pipenv
|
||||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
||||
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
||||
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
||||
# install all needed dependencies.
|
||||
#Pipfile.lock
|
||||
|
||||
# poetry
|
||||
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
||||
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
||||
# commonly ignored for libraries.
|
||||
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
||||
#poetry.lock
|
||||
|
||||
# pdm
|
||||
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
||||
#pdm.lock
|
||||
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
||||
# in version control.
|
||||
# https://pdm.fming.dev/#use-with-ide
|
||||
.pdm.toml
|
||||
|
||||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
||||
__pypackages__/
|
||||
|
||||
# Celery stuff
|
||||
celerybeat-schedule
|
||||
celerybeat.pid
|
||||
|
||||
# SageMath parsed files
|
||||
*.sage.py
|
||||
|
||||
# Environments
|
||||
.env
|
||||
.venv
|
||||
env/
|
||||
venv/
|
||||
ENV/
|
||||
env.bak/
|
||||
venv.bak/
|
||||
|
||||
# Spyder project settings
|
||||
.spyderproject
|
||||
.spyproject
|
||||
|
||||
# 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/
|
||||
|
||||
# PyCharm
|
||||
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
||||
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
||||
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
||||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||
#.idea/
|
||||
|
||||
### Python Patch ###
|
||||
# Poetry local configuration file - https://python-poetry.org/docs/configuration/#local-configuration
|
||||
poetry.toml
|
||||
|
||||
# ruff
|
||||
.ruff_cache/
|
||||
|
||||
# LSP config files
|
||||
pyrightconfig.json
|
||||
|
||||
# End of https://www.toptal.com/developers/gitignore/api/python
|
||||
9
amapcontract/bruno.json
Normal file
9
amapcontract/bruno.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"version": "1",
|
||||
"name": "amapcontract",
|
||||
"type": "collection",
|
||||
"ignore": [
|
||||
"node_modules",
|
||||
".git"
|
||||
]
|
||||
}
|
||||
11
amapcontract/collection.bru
Normal file
11
amapcontract/collection.bru
Normal file
@@ -0,0 +1,11 @@
|
||||
auth {
|
||||
mode: bearer
|
||||
}
|
||||
|
||||
auth:bearer {
|
||||
token: eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJtNUtSQkp1T3VqMnFiUElySlRldFVISGVWMWRTLTEzUG5saU1PSWRLcWFvIn0.eyJleHAiOjE3NzA2NTQwMzYsImlhdCI6MTc3MDY1MzczNiwiYXV0aF90aW1lIjoxNzcwNjUzMDU2LCJqdGkiOiJvbnJ0YWM6YmFiMDZiNGMtMjM5ZC00NzM3LTliYWEtNjE2MjBmMzVjM2NhIiwiaXNzIjoiaHR0cHM6Ly9rZXljbG9hay5hbGRvbi5mci9yZWFsbXMvYWxkb24uZnIiLCJhdWQiOlsibmV4dGNsb3VkIiwiYWNjb3VudCJdLCJzdWIiOiJlM2VkN2NiNC1iMDYxLTRiZWQtYTY5YS1lMGQ5ZWJmNDJhZWIiLCJ0eXAiOiJCZWFyZXIiLCJhenAiOiJhbWFwIiwic2lkIjoiMTkwY2I4YzAtMGYxZC00MWJiLTkwYWYtZjNmMGJkZWMzYjA3IiwiYWNyIjoiMCIsImFsbG93ZWQtb3JpZ2lucyI6WyJodHRwOi8vbG9jYWxob3N0OjgwMDAiXSwicmVhbG1fYWNjZXNzIjp7InJvbGVzIjpbImRlZmF1bHQtcm9sZXMtZ2lyYXNvbCIsIm9mZmxpbmVfYWNjZXNzIiwidW1hX2F1dGhvcml6YXRpb24iXX0sInJlc291cmNlX2FjY2VzcyI6eyJuZXh0Y2xvdWQiOnsicm9sZXMiOlsicGVsYXJnbyIsImZlZmFuIiwiYWRtaW4iLCJnaXJhc29sIl19LCJhY2NvdW50Ijp7InJvbGVzIjpbIm1hbmFnZS1hY2NvdW50IiwibWFuYWdlLWFjY291bnQtbGlua3MiLCJ2aWV3LXByb2ZpbGUiXX19LCJzY29wZSI6Im9wZW5pZCBwcm9maWxlIGVtYWlsIiwiZW1haWxfdmVyaWZpZWQiOmZhbHNlLCJuYW1lIjoiSnVsaWVuIEFsZG9uIiwicHJlZmVycmVkX3VzZXJuYW1lIjoianVsaWVuIiwiZ2l2ZW5fbmFtZSI6Ikp1bGllbiIsImZhbWlseV9uYW1lIjoiQWxkb24iLCJlbWFpbCI6Imp1bGllbi5hbGRvbkB3YW5hZG9vLmZyIn0.bq-EUtK_UqsIOwI6KDHB8eELMirWPDfTMta904XNeffj_v_ptEnHbecCf1OG6zzwanrBUyl_On7z95zVvVuKX6fQM9iaqxDqm7VlAK1O6n97367evTjQTOggkl3eTgX3xkfbCjJyzP_8RhTPXBsL_Nao8h5kgCnDwUHKEZ547oeoPKVEzlc82SgPi2rsiTVyvznJxGyJkQOTcDDMqTUxj4OVqWD5FMEDCfLnisUNPADhq0Umyw8hU4YwtI1-3hn6aXbnVcDekk2oWVli_6MeJHyejI8_yPnnQMvcp9OqciXRMtCGml1vMHcb5kUh4U9OeAhewzBFb_Mk9KDOspktSQ
|
||||
}
|
||||
|
||||
vars:pre-request {
|
||||
Service: http://localhost:8000
|
||||
}
|
||||
20
amapcontract/productors/Create.bru
Normal file
20
amapcontract/productors/Create.bru
Normal file
@@ -0,0 +1,20 @@
|
||||
meta {
|
||||
name: Create
|
||||
type: http
|
||||
seq: 1
|
||||
}
|
||||
|
||||
post {
|
||||
url: {{Service}}/{{Route}}
|
||||
body: json
|
||||
auth: inherit
|
||||
}
|
||||
|
||||
body:json {
|
||||
{{ExamplePOSTBody}}
|
||||
}
|
||||
|
||||
settings {
|
||||
encodeUrl: true
|
||||
timeout: 0
|
||||
}
|
||||
16
amapcontract/productors/Delete one.bru
Normal file
16
amapcontract/productors/Delete one.bru
Normal file
@@ -0,0 +1,16 @@
|
||||
meta {
|
||||
name: Delete one
|
||||
type: http
|
||||
seq: 2
|
||||
}
|
||||
|
||||
delete {
|
||||
url: {{Service}}/{{Route}}/2
|
||||
body: none
|
||||
auth: inherit
|
||||
}
|
||||
|
||||
settings {
|
||||
encodeUrl: true
|
||||
timeout: 0
|
||||
}
|
||||
16
amapcontract/productors/Get all.bru
Normal file
16
amapcontract/productors/Get all.bru
Normal file
@@ -0,0 +1,16 @@
|
||||
meta {
|
||||
name: Get all
|
||||
type: http
|
||||
seq: 2
|
||||
}
|
||||
|
||||
get {
|
||||
url: {{Service}}/{{Route}}
|
||||
body: none
|
||||
auth: inherit
|
||||
}
|
||||
|
||||
settings {
|
||||
encodeUrl: true
|
||||
timeout: 0
|
||||
}
|
||||
16
amapcontract/productors/Get one.bru
Normal file
16
amapcontract/productors/Get one.bru
Normal file
@@ -0,0 +1,16 @@
|
||||
meta {
|
||||
name: Get one
|
||||
type: http
|
||||
seq: 2
|
||||
}
|
||||
|
||||
get {
|
||||
url: {{Service}}/{{Route}}/1
|
||||
body: none
|
||||
auth: inherit
|
||||
}
|
||||
|
||||
settings {
|
||||
encodeUrl: true
|
||||
timeout: 0
|
||||
}
|
||||
20
amapcontract/productors/Update one.bru
Normal file
20
amapcontract/productors/Update one.bru
Normal file
@@ -0,0 +1,20 @@
|
||||
meta {
|
||||
name: Update one
|
||||
type: http
|
||||
seq: 2
|
||||
}
|
||||
|
||||
put {
|
||||
url: {{Service}}/{{Route}}/1
|
||||
body: json
|
||||
auth: inherit
|
||||
}
|
||||
|
||||
body:json {
|
||||
{{ExamplePUTBody}}
|
||||
}
|
||||
|
||||
settings {
|
||||
encodeUrl: true
|
||||
timeout: 0
|
||||
}
|
||||
13
amapcontract/productors/folder.bru
Normal file
13
amapcontract/productors/folder.bru
Normal file
@@ -0,0 +1,13 @@
|
||||
meta {
|
||||
name: productors
|
||||
}
|
||||
|
||||
auth {
|
||||
mode: inherit
|
||||
}
|
||||
|
||||
vars:pre-request {
|
||||
Route: productors
|
||||
ExamplePOSTBody: {"name": "test", "address": "test", "payment": "test"}
|
||||
ExamplePUTBody: {"name": "updatetestt", "address": "updatetestt"}
|
||||
}
|
||||
20
amapcontract/products/Create.bru
Normal file
20
amapcontract/products/Create.bru
Normal file
@@ -0,0 +1,20 @@
|
||||
meta {
|
||||
name: Create
|
||||
type: http
|
||||
seq: 1
|
||||
}
|
||||
|
||||
post {
|
||||
url: {{Service}}/{{Route}}
|
||||
body: json
|
||||
auth: inherit
|
||||
}
|
||||
|
||||
body:json {
|
||||
{{ExamplePOSTBody}}
|
||||
}
|
||||
|
||||
settings {
|
||||
encodeUrl: true
|
||||
timeout: 0
|
||||
}
|
||||
16
amapcontract/products/Delete one.bru
Normal file
16
amapcontract/products/Delete one.bru
Normal file
@@ -0,0 +1,16 @@
|
||||
meta {
|
||||
name: Delete one
|
||||
type: http
|
||||
seq: 2
|
||||
}
|
||||
|
||||
delete {
|
||||
url: {{Service}}/{{Route}}/2
|
||||
body: none
|
||||
auth: inherit
|
||||
}
|
||||
|
||||
settings {
|
||||
encodeUrl: true
|
||||
timeout: 0
|
||||
}
|
||||
16
amapcontract/products/Get all.bru
Normal file
16
amapcontract/products/Get all.bru
Normal file
@@ -0,0 +1,16 @@
|
||||
meta {
|
||||
name: Get all
|
||||
type: http
|
||||
seq: 2
|
||||
}
|
||||
|
||||
get {
|
||||
url: {{Service}}/{{Route}}
|
||||
body: none
|
||||
auth: inherit
|
||||
}
|
||||
|
||||
settings {
|
||||
encodeUrl: true
|
||||
timeout: 0
|
||||
}
|
||||
16
amapcontract/products/Get one.bru
Normal file
16
amapcontract/products/Get one.bru
Normal file
@@ -0,0 +1,16 @@
|
||||
meta {
|
||||
name: Get one
|
||||
type: http
|
||||
seq: 2
|
||||
}
|
||||
|
||||
get {
|
||||
url: {{Service}}/{{Route}}/1
|
||||
body: none
|
||||
auth: inherit
|
||||
}
|
||||
|
||||
settings {
|
||||
encodeUrl: true
|
||||
timeout: 0
|
||||
}
|
||||
20
amapcontract/products/Update one.bru
Normal file
20
amapcontract/products/Update one.bru
Normal file
@@ -0,0 +1,20 @@
|
||||
meta {
|
||||
name: Update one
|
||||
type: http
|
||||
seq: 2
|
||||
}
|
||||
|
||||
put {
|
||||
url: {{Service}}/{{Route}}/1
|
||||
body: json
|
||||
auth: inherit
|
||||
}
|
||||
|
||||
body:json {
|
||||
{{ExamplePUTBody}}
|
||||
}
|
||||
|
||||
settings {
|
||||
encodeUrl: true
|
||||
timeout: 0
|
||||
}
|
||||
13
amapcontract/products/folder.bru
Normal file
13
amapcontract/products/folder.bru
Normal file
@@ -0,0 +1,13 @@
|
||||
meta {
|
||||
name: products
|
||||
}
|
||||
|
||||
auth {
|
||||
mode: inherit
|
||||
}
|
||||
|
||||
vars:pre-request {
|
||||
Route: products
|
||||
ExamplePOSTBody: {"name": "test", "unit": "KILO", "price": 3.50, "price_kg": 3.50, "weight": "1.0", "productor_id": 1}
|
||||
ExamplePUTBody: {"name": "updatetestt", "address": "updatetestt"}
|
||||
}
|
||||
9
backend/LICENSE.txt
Normal file
9
backend/LICENSE.txt
Normal file
@@ -0,0 +1,9 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2026-present Julien Aldon <julien.aldon@wanadoo.fr>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
21
backend/README.md
Normal file
21
backend/README.md
Normal file
@@ -0,0 +1,21 @@
|
||||
# backend
|
||||
|
||||
[](https://pypi.org/project/backend)
|
||||
[](https://pypi.org/project/backend)
|
||||
|
||||
-----
|
||||
|
||||
## Table of Contents
|
||||
|
||||
- [Installation](#installation)
|
||||
- [License](#license)
|
||||
|
||||
## Installation
|
||||
|
||||
```console
|
||||
pip install backend
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
`backend` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license.
|
||||
65
backend/pyproject.toml
Normal file
65
backend/pyproject.toml
Normal file
@@ -0,0 +1,65 @@
|
||||
[build-system]
|
||||
requires = ["hatchling"]
|
||||
build-backend = "hatchling.build"
|
||||
|
||||
[project]
|
||||
name = "backend"
|
||||
dynamic = ["version"]
|
||||
description = ''
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.11"
|
||||
license = "MIT"
|
||||
keywords = []
|
||||
authors = [
|
||||
{ name = "Julien Aldon", email = "julien.aldon@wanadoo.fr" },
|
||||
]
|
||||
classifiers = [
|
||||
"Development Status :: 4 - Beta",
|
||||
"Programming Language :: Python",
|
||||
"Programming Language :: Python :: 3.11",
|
||||
"Programming Language :: Python :: 3.12",
|
||||
"Programming Language :: Python :: Implementation :: CPython",
|
||||
"Programming Language :: Python :: Implementation :: PyPy",
|
||||
]
|
||||
dependencies = [
|
||||
"fastapi",
|
||||
"sqlmodel",
|
||||
"psycopg2",
|
||||
"PyJWT",
|
||||
"cryptography",
|
||||
"requests"
|
||||
]
|
||||
|
||||
[project.urls]
|
||||
Documentation = "https://github.com/Julien Aldon/backend#readme"
|
||||
Issues = "https://github.com/Julien Aldon/backend/issues"
|
||||
Source = "https://github.com/Julien Aldon/backend"
|
||||
|
||||
[tool.hatch.version]
|
||||
path = "src/__about__.py"
|
||||
|
||||
[tool.hatch.envs.types]
|
||||
extra-dependencies = [
|
||||
"mypy>=1.0.0",
|
||||
]
|
||||
[tool.hatch.envs.types.scripts]
|
||||
check = "mypy --install-types --non-interactive {args:src/backend tests}"
|
||||
|
||||
[tool.coverage.run]
|
||||
source_pkgs = ["backend", "tests"]
|
||||
branch = true
|
||||
parallel = true
|
||||
omit = [
|
||||
"src/__about__.py",
|
||||
]
|
||||
|
||||
[tool.coverage.paths]
|
||||
backend = ["src", "*/backend/src/"]
|
||||
tests = ["tests", "*/backend/tests"]
|
||||
|
||||
[tool.coverage.report]
|
||||
exclude_lines = [
|
||||
"no cov",
|
||||
"if __name__ == .__main__.:",
|
||||
"if TYPE_CHECKING:",
|
||||
]
|
||||
4
backend/src/__about__.py
Normal file
4
backend/src/__about__.py
Normal file
@@ -0,0 +1,4 @@
|
||||
# SPDX-FileCopyrightText: 2026-present Julien Aldon <julien.aldon@wanadoo.fr>
|
||||
#
|
||||
# SPDX-License-Identifier: MIT
|
||||
__version__ = "0.0.1"
|
||||
3
backend/src/__init__.py
Normal file
3
backend/src/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
# SPDX-FileCopyrightText: 2026-present Julien Aldon <julien.aldon@wanadoo.fr>
|
||||
#
|
||||
# SPDX-License-Identifier: MIT
|
||||
80
backend/src/auth/auth.py
Normal file
80
backend/src/auth/auth.py
Normal file
@@ -0,0 +1,80 @@
|
||||
from fastapi import APIRouter, Security, HTTPException
|
||||
from fastapi.responses import RedirectResponse
|
||||
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
||||
from src.secrets import CLIENT_ID, REDIRECT_URI, AUTH_URL, CLIENT_SECRET, TOKEN_URL, JWKS_URL, ISSUER
|
||||
import secrets
|
||||
import jwt
|
||||
from jwt import PyJWKClient
|
||||
import requests
|
||||
|
||||
router = APIRouter(prefix="/auth")
|
||||
|
||||
jwk_client = PyJWKClient(JWKS_URL)
|
||||
security = HTTPBearer()
|
||||
|
||||
@router.get('/login')
|
||||
def login():
|
||||
state = secrets.token_urlsafe(16)
|
||||
|
||||
params = {
|
||||
"client_id": CLIENT_ID,
|
||||
"response_type": "code",
|
||||
"scope": "openid",
|
||||
"redirect_uri": REDIRECT_URI,
|
||||
"state": state,
|
||||
}
|
||||
|
||||
request_url = requests.Request('GET', AUTH_URL, params=params).prepare().url
|
||||
return RedirectResponse(request_url)
|
||||
|
||||
@router.get("/callback")
|
||||
def callback(code: str):
|
||||
data = {
|
||||
"grant_type": "authorization_code",
|
||||
"code": code,
|
||||
"redirect_uri": REDIRECT_URI,
|
||||
"client_id": CLIENT_ID,
|
||||
"client_secret": CLIENT_SECRET,
|
||||
}
|
||||
headers = {
|
||||
"Content-Type": "application/x-www-form-urlencoded"
|
||||
}
|
||||
response = requests.post(TOKEN_URL, data=data, headers=headers)
|
||||
if response.status_code != 200:
|
||||
return JSONResponse(
|
||||
{"error": "Failed to get token"},
|
||||
status_code=400
|
||||
)
|
||||
token_data = response.json()
|
||||
return {
|
||||
"access_token": token_data["access_token"],
|
||||
"id_token": token_data["id_token"],
|
||||
"refresh_token": token_data["refresh_token"],
|
||||
}
|
||||
|
||||
def verify_token(token: str):
|
||||
try:
|
||||
signing_key = jwk_client.get_signing_key_from_jwt(token)
|
||||
decoded = jwt.decode(token, options={"verify_signature": False})
|
||||
print(decoded, ISSUER)
|
||||
print(decoded["exp"])
|
||||
payload = jwt.decode(
|
||||
token,
|
||||
signing_key.key,
|
||||
algorithms=["RS256"],
|
||||
audience=CLIENT_ID,
|
||||
issuer=ISSUER,
|
||||
)
|
||||
return payload
|
||||
|
||||
except jwt.ExpiredSignatureError:
|
||||
raise HTTPException(status_code=401, detail="Token expired")
|
||||
|
||||
except jwt.InvalidTokenError:
|
||||
raise HTTPException(status_code=401, detail="Invalid token")
|
||||
|
||||
|
||||
def get_current_user(
|
||||
credentials: HTTPAuthorizationCredentials = Security(security)
|
||||
):
|
||||
return verify_token(credentials.credentials)
|
||||
3
backend/src/contracts/__init__.py
Normal file
3
backend/src/contracts/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
# SPDX-FileCopyrightText: 2026-present Julien Aldon <julien.aldon@wanadoo.fr>
|
||||
#
|
||||
# SPDX-License-Identifier: MIT
|
||||
3
backend/src/contracts/contracts.py
Normal file
3
backend/src/contracts/contracts.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
router = APIRouter(prefix='/contracts')
|
||||
9
backend/src/database.py
Normal file
9
backend/src/database.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from sqlmodel import create_engine, SQLModel, Session
|
||||
from src.secrets import dbname, dbhost, dbuser, dbpass
|
||||
|
||||
engine = create_engine(f'postgresql://{dbuser}:{dbpass}@{dbhost}:54321/{dbname}')
|
||||
# SQLModel.metadata.create_all(engine)
|
||||
|
||||
def get_session():
|
||||
with Session(engine) as session:
|
||||
yield session
|
||||
3
backend/src/forms/__init__.py
Normal file
3
backend/src/forms/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
# SPDX-FileCopyrightText: 2026-present Julien Aldon <julien.aldon@wanadoo.fr>
|
||||
#
|
||||
# SPDX-License-Identifier: MIT
|
||||
19
backend/src/forms/forms.py
Normal file
19
backend/src/forms/forms.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
router = APIRouter(prefix='/forms')
|
||||
|
||||
@router.get('/')
|
||||
def get_forms():
|
||||
return []
|
||||
|
||||
@router.post('/')
|
||||
def create_form():
|
||||
return {}
|
||||
|
||||
@router.put('/')
|
||||
def update_form():
|
||||
return {}
|
||||
|
||||
@router.delete('/')
|
||||
def delete_form():
|
||||
return {}
|
||||
10
backend/src/forms/model.py
Normal file
10
backend/src/forms/model.py
Normal file
@@ -0,0 +1,10 @@
|
||||
from sqlmodel import Field, SQLModel
|
||||
form src.productors.model import Productor
|
||||
|
||||
class Form(SQLModel, table=True):
|
||||
id: int | None = Field(default=None, primary_key=True)
|
||||
name: str
|
||||
productor_id: int | None = Field(default=None, foreign_key="productor.id")
|
||||
shipment_number: int
|
||||
season: str
|
||||
|
||||
36
backend/src/main.py
Normal file
36
backend/src/main.py
Normal file
@@ -0,0 +1,36 @@
|
||||
from sqlmodel import SQLModel
|
||||
|
||||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
|
||||
from src.templates.templates import router as template_router
|
||||
from src.contracts.contracts import router as contracts_router
|
||||
from src.forms.forms import router as forms_router
|
||||
from src.productors.productors import router as productors_router
|
||||
from src.products.products import router as products_router
|
||||
from src.users.users import router as users_router
|
||||
from src.auth.auth import router as auth_router
|
||||
from src.secrets import origins
|
||||
from src.database import engine
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=origins,
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
expose_headers=['x-nbpage']
|
||||
)
|
||||
|
||||
|
||||
app.include_router(template_router)
|
||||
app.include_router(contracts_router)
|
||||
app.include_router(forms_router)
|
||||
app.include_router(productors_router)
|
||||
app.include_router(products_router)
|
||||
app.include_router(users_router)
|
||||
app.include_router(auth_router)
|
||||
|
||||
SQLModel.metadata.create_all(engine)
|
||||
1
backend/src/messages.py
Normal file
1
backend/src/messages.py
Normal file
@@ -0,0 +1 @@
|
||||
notfound = "Resource was not found."
|
||||
100
backend/src/models.py
Normal file
100
backend/src/models.py
Normal file
@@ -0,0 +1,100 @@
|
||||
from sqlmodel import Field, SQLModel, Relationship
|
||||
from enum import Enum
|
||||
from typing import Optional
|
||||
import datetime
|
||||
|
||||
class Unit(Enum):
|
||||
GRAMS = 1
|
||||
KILO = 2
|
||||
|
||||
class ProductBase(SQLModel):
|
||||
name: str
|
||||
unit: Unit
|
||||
price: float
|
||||
price_kg: float | None
|
||||
weight: float
|
||||
productor_id: int | None = Field(default=None, foreign_key="productor.id")
|
||||
|
||||
class ProductPublic(ProductBase):
|
||||
id: int
|
||||
|
||||
class Product(ProductBase, table=True):
|
||||
id: int | None = Field(default=None, primary_key=True)
|
||||
|
||||
productor: Optional['Productor'] = Relationship(back_populates="products")
|
||||
|
||||
class ProductUpdate(SQLModel):
|
||||
name: str | None
|
||||
unit: Unit | None
|
||||
price: float | None
|
||||
price_kg: float | None
|
||||
weight: float | None
|
||||
productor_id: int | None = Field(default=None, foreign_key="productor.id")
|
||||
|
||||
|
||||
class ProductCreate(ProductBase):
|
||||
pass
|
||||
|
||||
class ProductorBase(SQLModel):
|
||||
name: str
|
||||
address: str
|
||||
payment: str
|
||||
|
||||
class ProductorPublic(ProductorBase):
|
||||
id: int
|
||||
products: list[Product] = []
|
||||
|
||||
class Productor(ProductorBase, table=True):
|
||||
id: int | None = Field(default=None, primary_key=True)
|
||||
|
||||
products: list[Product] = Relationship(back_populates='productor')
|
||||
|
||||
class ProductorUpdate(SQLModel):
|
||||
name: str | None
|
||||
address: str | None
|
||||
payment: str | None
|
||||
|
||||
class ProductorCreate(ProductorBase):
|
||||
pass
|
||||
|
||||
class FormBase(SQLModel):
|
||||
productor_id: int | None = Field(default=None, foreign_key="productor.id")
|
||||
referer_id: int | None = Field(default=None, foreign_key="referer.id")
|
||||
season: str
|
||||
shipments: int
|
||||
start: datetime.date
|
||||
end: datetime.date
|
||||
|
||||
class FormPublic(FormBase):
|
||||
id: int
|
||||
|
||||
class Form(FormBase, table=True):
|
||||
id: int | None = Field(default=None, primary_key=True)
|
||||
|
||||
class FormUpdate(SQLModel):
|
||||
productor_id: int | None = Field(default=None, foreign_key="productor.id")
|
||||
referer_id: int | None = Field(default=None, foreign_key="user.id")
|
||||
season: str | None
|
||||
shipments: int | None
|
||||
start: datetime.date | None
|
||||
end: datetime.date | None
|
||||
|
||||
class FormCreate(FormBase):
|
||||
pass
|
||||
|
||||
class UserBase(SQLModel):
|
||||
name: str
|
||||
email: str
|
||||
|
||||
class UserPublic(UserBase):
|
||||
id: int
|
||||
|
||||
class User(UserBase, table=True):
|
||||
id: int | None = Field(default=None, primary_key=True)
|
||||
|
||||
class UserUpdate(SQLModel):
|
||||
name: str | None
|
||||
email: str | None
|
||||
|
||||
class UserCreate(UserBase):
|
||||
pass
|
||||
3
backend/src/productors/__init__.py
Normal file
3
backend/src/productors/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
# SPDX-FileCopyrightText: 2026-present Julien Aldon <julien.aldon@wanadoo.fr>
|
||||
#
|
||||
# SPDX-License-Identifier: MIT
|
||||
37
backend/src/productors/productors.py
Normal file
37
backend/src/productors/productors.py
Normal file
@@ -0,0 +1,37 @@
|
||||
from fastapi import APIRouter, HTTPException, Depends
|
||||
import src.messages as messages
|
||||
import src.models as models
|
||||
from src.database import get_session
|
||||
from sqlmodel import Session
|
||||
import src.productors.service as service
|
||||
|
||||
router = APIRouter(prefix='/productors')
|
||||
|
||||
@router.get('/', response_model=list[models.ProductorPublic])
|
||||
def get_productors(session: Session = Depends(get_session)):
|
||||
return service.get_all(session)
|
||||
|
||||
@router.get('/{id}', response_model=models.ProductorPublic)
|
||||
def get_productors(id: int, session: Session = Depends(get_session)):
|
||||
result = service.get_one(session, id)
|
||||
if result is None:
|
||||
raise HTTPException(status_code=404, detail=messages.notfound)
|
||||
return result
|
||||
|
||||
@router.post('/', response_model=models.ProductorPublic)
|
||||
def create_productor(productor: models.ProductorCreate, session: Session = Depends(get_session)):
|
||||
return service.create_one(session, productor)
|
||||
|
||||
@router.put('/{id}', response_model=models.ProductorPublic)
|
||||
def update_productor(id: int, productor: models.ProductorUpdate, session: Session = Depends(get_session)):
|
||||
result = service.update_one(session, id, productor)
|
||||
if result is None:
|
||||
raise HTTPException(status_code=404, detail=messages.notfound)
|
||||
return result
|
||||
|
||||
@router.delete('/{id}', response_model=models.ProductorPublic)
|
||||
def delete_productor(id: int, session: Session = Depends(get_session)):
|
||||
result = service.delete_one(session, id)
|
||||
if result is None:
|
||||
raise HTTPException(status_code=404, detail=messages.notfound)
|
||||
return result
|
||||
41
backend/src/productors/service.py
Normal file
41
backend/src/productors/service.py
Normal file
@@ -0,0 +1,41 @@
|
||||
from sqlmodel import Session, select
|
||||
from src.models import Productor
|
||||
|
||||
def get_all(session: Session) -> list[Productor]:
|
||||
statement = select(Productor)
|
||||
return session.exec(statement).all()
|
||||
|
||||
def get_one(session: Session, productor_id: int) -> Productor:
|
||||
return session.get(Productor, productor_id)
|
||||
|
||||
def create_one(session: Session, productor: Productor) -> Productor:
|
||||
productor_create = productor.model_dump(exclude_unset=True)
|
||||
new_productor = Productor(**productor_create)
|
||||
session.add(new_productor)
|
||||
session.commit()
|
||||
session.refresh(new_productor)
|
||||
return new_productor
|
||||
|
||||
def update_one(session: Session, id: int, productor: Productor) -> Productor:
|
||||
statement = select(Productor).where(Productor.id == id)
|
||||
result = session.exec(statement)
|
||||
new_productor = result.first()
|
||||
if not new_productor:
|
||||
return None
|
||||
productor_updates = productor.model_dump(exclude_unset=True)
|
||||
for key, value in productor_updates.items():
|
||||
setattr(new_productor, key, value)
|
||||
session.add(new_productor)
|
||||
session.commit()
|
||||
session.refresh(new_productor)
|
||||
return new_productor
|
||||
|
||||
def delete_one(session: Session, id: int) -> Productor:
|
||||
statement = select(Productor).where(Productor.id == id)
|
||||
result = session.exec(statement)
|
||||
productor = result.first()
|
||||
if not productor:
|
||||
return None
|
||||
session.delete(productor)
|
||||
session.commit()
|
||||
return productor
|
||||
3
backend/src/products/__init__.py
Normal file
3
backend/src/products/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
# SPDX-FileCopyrightText: 2026-present Julien Aldon <julien.aldon@wanadoo.fr>
|
||||
#
|
||||
# SPDX-License-Identifier: MIT
|
||||
37
backend/src/products/products.py
Normal file
37
backend/src/products/products.py
Normal file
@@ -0,0 +1,37 @@
|
||||
from fastapi import APIRouter, HTTPException, Depends
|
||||
import src.messages as messages
|
||||
import src.models as models
|
||||
from src.database import get_session
|
||||
from sqlmodel import Session
|
||||
import src.products.service as service
|
||||
from src.auth.auth import get_current_user
|
||||
router = APIRouter(prefix='/products')
|
||||
|
||||
@router.get('/', response_model=list[models.ProductPublic], )
|
||||
def get_products(session: Session = Depends(get_session), user=Depends(get_current_user)):
|
||||
return service.get_all(session)
|
||||
|
||||
@router.get('/{id}', response_model=models.ProductPublic)
|
||||
def get_product(id: int, session: Session = Depends(get_session)):
|
||||
result = service.get_one(session, id)
|
||||
if result is None:
|
||||
raise HTTPException(status_code=404, detail=messages.notfound)
|
||||
return result
|
||||
|
||||
@router.post('/', response_model=models.ProductPublic)
|
||||
def create_product(product: models.ProductCreate, session: Session = Depends(get_session)):
|
||||
return service.create_one(session, product)
|
||||
|
||||
@router.put('/{id}', response_model=models.ProductPublic)
|
||||
def update_product(id: int, product: models.ProductUpdate, session: Session = Depends(get_session)):
|
||||
result = service.update_one(session, id, product)
|
||||
if result is None:
|
||||
raise HTTPException(status_code=404, detail=messages.notfound)
|
||||
return result
|
||||
|
||||
@router.delete('/{id}', response_model=models.ProductPublic)
|
||||
def delete_product(id: int, session: Session = Depends(get_session)):
|
||||
result = service.delete_one(session, id)
|
||||
if result is None:
|
||||
raise HTTPException(status_code=404, detail=messages.notfound)
|
||||
return result
|
||||
41
backend/src/products/service.py
Normal file
41
backend/src/products/service.py
Normal file
@@ -0,0 +1,41 @@
|
||||
from sqlmodel import Session, select
|
||||
from src.models import Product
|
||||
|
||||
def get_all(session: Session) -> list[Product]:
|
||||
statement = select(Product)
|
||||
return session.exec(statement).all()
|
||||
|
||||
def get_one(session: Session, product_id: int) -> Product:
|
||||
return session.get(Product, product_id)
|
||||
|
||||
def create_one(session: Session, product: Product) -> Product:
|
||||
product_create = product.model_dump(exclude_unset=True)
|
||||
new_product = Product(**product_create)
|
||||
session.add(new_product)
|
||||
session.commit()
|
||||
session.refresh(new_product)
|
||||
return new_product
|
||||
|
||||
def update_one(session: Session, id: int, product: Product) -> Product:
|
||||
statement = select(Product).where(Product.id == id)
|
||||
result = session.exec(statement)
|
||||
new_product = result.first()
|
||||
if not new_product:
|
||||
return None
|
||||
product_updates = product.model_dump(exclude_unset=True)
|
||||
for key, value in product_updates.items():
|
||||
setattr(new_product, key, value)
|
||||
session.add(new_product)
|
||||
session.commit()
|
||||
session.refresh(new_product)
|
||||
return new_product
|
||||
|
||||
def delete_one(session: Session, id: int) -> Product:
|
||||
statement = select(Product).where(Product.id == id)
|
||||
result = session.exec(statement)
|
||||
product = result.first()
|
||||
if not product:
|
||||
return None
|
||||
session.delete(product)
|
||||
session.commit()
|
||||
return product
|
||||
29
backend/src/secrets.py
Normal file
29
backend/src/secrets.py
Normal file
@@ -0,0 +1,29 @@
|
||||
import os
|
||||
|
||||
origins = [
|
||||
os.environ.get('SERVICE_ORIGIN') or 'http://localhost'
|
||||
]
|
||||
|
||||
dbhost = os.environ.get('DB_HOST') or 'localhost'
|
||||
dbuser = os.environ.get('PGSQL_USER') or 'postgres'
|
||||
dbpass = os.environ.get('PGSQL_PASSWORD') or 'postgres'
|
||||
dbname = os.environ.get('PGSQL_DATABASE') or 'amap'
|
||||
|
||||
# openssl rand -hex 32
|
||||
SECRET_KEY = os.environ.get('SERVICE_SECRET_KEY') or 'test'
|
||||
ALGORITHM = 'HS256'
|
||||
ACCESS_TOKEN_EXPIRE_MINUTES = 600
|
||||
|
||||
|
||||
KEYCLOAK_SERVER = ""
|
||||
REALM = ""
|
||||
CLIENT_ID = ""
|
||||
CLIENT_SECRET = ""
|
||||
|
||||
REDIRECT_URI = "http://localhost:8000/auth/callback"
|
||||
|
||||
AUTH_URL = f"{KEYCLOAK_SERVER}/realms/{REALM}/protocol/openid-connect/auth"
|
||||
TOKEN_URL = f"{KEYCLOAK_SERVER}/realms/{REALM}/protocol/openid-connect/token"
|
||||
|
||||
ISSUER = f"{KEYCLOAK_SERVER}/realms/{REALM}"
|
||||
JWKS_URL = f"{ISSUER}/protocol/openid-connect/certs"
|
||||
3
backend/src/templates/__init__.py
Normal file
3
backend/src/templates/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
# SPDX-FileCopyrightText: 2026-present Julien Aldon <julien.aldon@wanadoo.fr>
|
||||
#
|
||||
# SPDX-License-Identifier: MIT
|
||||
19
backend/src/templates/templates.py
Normal file
19
backend/src/templates/templates.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
router = APIRouter(prefix='/templates')
|
||||
|
||||
@router.get('/')
|
||||
def get_templates():
|
||||
return []
|
||||
|
||||
@router.post('/')
|
||||
def create_template():
|
||||
return {}
|
||||
|
||||
@router.put('/')
|
||||
def update_template():
|
||||
return {}
|
||||
|
||||
@router.delete('/')
|
||||
def delete_template():
|
||||
return {}
|
||||
19
backend/src/users/users.py
Normal file
19
backend/src/users/users.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
router = APIRouter(prefix='/users')
|
||||
|
||||
@router.get('/')
|
||||
def get_users():
|
||||
return []
|
||||
|
||||
@router.post('/')
|
||||
def create_user():
|
||||
return {}
|
||||
|
||||
@router.put('/')
|
||||
def update_user():
|
||||
return {}
|
||||
|
||||
@router.delete('/')
|
||||
def delete_user():
|
||||
return {}
|
||||
3
backend/tests/__init__.py
Normal file
3
backend/tests/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
# SPDX-FileCopyrightText: 2026-present Julien Aldon <julien.aldon@wanadoo.fr>
|
||||
#
|
||||
# SPDX-License-Identifier: MIT
|
||||
42
docker-compose.yaml
Normal file
42
docker-compose.yaml
Normal file
@@ -0,0 +1,42 @@
|
||||
version: "3.9"
|
||||
services:
|
||||
# nginx:
|
||||
# restart: always
|
||||
# build:
|
||||
# context: .
|
||||
# dockerfile: front/Dockerfile
|
||||
# args:
|
||||
# VUE_APP_ROOT_FQDN: ${SERVICE_ROOT_FQDN}
|
||||
# ports:
|
||||
# - 80:80
|
||||
# depends_on:
|
||||
# - back
|
||||
# back:
|
||||
# build:
|
||||
# context: .
|
||||
# dockerfile: back/Dockerfile
|
||||
# restart: always
|
||||
# environment:
|
||||
# SERVICE_ORIGIN: ${SERVICE_ORIGIN}
|
||||
# DB_HOST: database
|
||||
# MARIADB_USER: ${MARIADB_USER}
|
||||
# MARIADB_PASSWORD: ${MARIADB_PASSWORD}
|
||||
# MARIADB_DATABASE: ${MARIADB_DATABASE}
|
||||
# SERVICE_SECRET_KEY: ${SERVICE_SECRET_KEY}
|
||||
# ports:
|
||||
# - 8000:8000
|
||||
# depends_on:
|
||||
# - database
|
||||
database:
|
||||
image: postgres
|
||||
restart: always
|
||||
shm_size: 128mb
|
||||
environment:
|
||||
POSTGRES_USER: ${POSTGRES_USER}
|
||||
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
|
||||
POSTGRES_DB: ${POSTGRES_DB}
|
||||
ROOT_FQDN: ${ROOT_FQDN}
|
||||
ports:
|
||||
- "54321:5432"
|
||||
volumes:
|
||||
db:
|
||||
Reference in New Issue
Block a user