Skip to content

Commit

Permalink
Merge pull request #12 from Angel-Dijoux/angel/scrapt_onisep
Browse files Browse the repository at this point in the history
[Refacto] Onisep calls in backend.
  • Loading branch information
Angel-Dijoux authored Oct 30, 2023
2 parents 68ebac6 + e350b38 commit a0e06f6
Show file tree
Hide file tree
Showing 25 changed files with 744 additions and 96 deletions.
22 changes: 22 additions & 0 deletions .vscode/settings.default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"editor.formatOnSave": true,
"editor.formatOnType": false,
"editor.formatOnPaste": true,
"[json]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[python]": {
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll": true,
"source.organizeImports": true
}
},
"python.formatting.provider": "black",
"python.formatting.blackArgs": [
"--config",
"${workspaceFolder}/pyproject.toml"
],
"python.analysis.extraPaths": ["backend_flask"],
"python.analysis.autoImportCompletions": true
}
6 changes: 2 additions & 4 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
{
"editor.tabCompletion": "on",
"diffEditor.codeLens": true,
"python.analysis.typeCheckingMode": "basic"
}
"python.analysis.autoImportCompletions": true
}
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ flask-migrate = "==3.1.0"
xmltodict = "==0.13.0"
secure = "==0.3.0"
loguru = "*"
sqlalchemy-serializer = "*"

[dev-packages]

Expand Down
9 changes: 8 additions & 1 deletion Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

100 changes: 100 additions & 0 deletions migrations/versions/16c8bc08a922_create_user_favori_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
"""Delete and re-create favori table
Revision ID: 16c8bc08a922
Revises: a3fe3092f50c
Create Date: 2023-10-29 18:56:52.112998
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import mysql

# revision identifiers, used by Alembic.
revision = "16c8bc08a922"
down_revision = "a3fe3092f50c"
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table(
"user_favori",
sa.Column("formation_id", sa.String(length=36), nullable=False),
sa.Column("user_id", sa.Integer(), nullable=False),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.Column("updated_at", sa.DateTime(), nullable=False),
sa.ForeignKeyConstraint(
["formation_id"],
["formation.id"],
ondelete="CASCADE",
name="favori_formation_id_fk",
),
sa.ForeignKeyConstraint(
["user_id"], ["user.id"], ondelete="CASCADE", name="favori_user_id_fk"
),
sa.PrimaryKeyConstraint("formation_id", "user_id", name="favori_pk"),
)
op.create_index(
op.f("ix_user_favori_formation_id"),
"user_favori",
["formation_id"],
unique=False,
)
op.create_index(
op.f("ix_user_favori_user_id"), "user_favori", ["user_id"], unique=False
)

op.execute(
"""
INSERT INTO user_favori (formation_id, user_id, created_at, updated_at)
SELECT fr.id, f.request_user_id, f.created_at, f.updated_at
FROM favori f
JOIN formation fr ON f.code_nsf = fr.code_nsf
AND f.sigle_type_formation = fr.type
AND f.libelle_formation_principal = fr.libelle
AND f.tutelle = fr.tutelle
AND f.url_et_id_onisep = fr.url
AND f.niveau_de_sortie_indicatif = fr.niveau_de_sortie
AND f.duree = fr.duree
AND f.created_at = fr.created_at
AND f.updated_at = fr.updated_at;
"""
)

op.drop_table("favori")
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table(
"favori",
sa.Column("id", mysql.INTEGER(), autoincrement=True, nullable=False),
sa.Column("code_nsf", mysql.TEXT(), nullable=True),
sa.Column("sigle_type_formation", mysql.TEXT(), nullable=True),
sa.Column("libelle_type_formation", mysql.TEXT(), nullable=True),
sa.Column("libelle_formation_principal", mysql.TEXT(), nullable=True),
sa.Column("sigle_formation", mysql.TEXT(), nullable=True),
sa.Column("duree", mysql.TEXT(), nullable=True),
sa.Column("niveau_de_sortie_indicatif", mysql.TEXT(), nullable=True),
sa.Column("code_rncp", mysql.TEXT(), nullable=True),
sa.Column("niveau_de_certification", mysql.TEXT(), nullable=True),
sa.Column("libelle_niveau_de_certification", mysql.TEXT(), nullable=True),
sa.Column("tutelle", mysql.TEXT(), nullable=True),
sa.Column("url_et_id_onisep", mysql.TEXT(), nullable=False),
sa.Column(
"request_user_id", mysql.INTEGER(), autoincrement=False, nullable=True
),
sa.Column("created_at", mysql.DATETIME(), nullable=False),
sa.Column("updated_at", mysql.DATETIME(), nullable=False),
sa.ForeignKeyConstraint(["request_user_id"], ["user.id"], name="favori_ibfk_1"),
sa.PrimaryKeyConstraint("id"),
mysql_collate="utf8mb4_0900_ai_ci",
mysql_default_charset="utf8mb4",
mysql_engine="InnoDB",
)
op.drop_index(op.f("ix_user_favori_user_id"), table_name="user_favori")
op.drop_index(op.f("ix_user_favori_formation_id"), table_name="user_favori")
op.drop_table("user_favori")
# ### end Alembic commands ###
61 changes: 61 additions & 0 deletions migrations/versions/a3fe3092f50c_create_formation_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"""Create formation table
Revision ID: a3fe3092f50c
Revises: 8ce7337ffbf4
Create Date: 2023-10-29 15:59:03.950861
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = "a3fe3092f50c"
down_revision = "8ce7337ffbf4"
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table(
"formation",
sa.Column("id", sa.String(length=36), nullable=False),
sa.Column("code_nsf", sa.Integer(), nullable=False),
sa.Column("type", sa.String(length=120), nullable=False),
sa.Column("libelle", sa.String(length=120), nullable=False),
sa.Column("tutelle", sa.String(length=120), nullable=False),
sa.Column("url", sa.String(length=255), nullable=False),
sa.Column("domain", sa.String(length=255), nullable=False),
sa.Column("niveau_de_sortie", sa.String(length=120), nullable=False),
sa.Column("duree", sa.String(length=15), nullable=False),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.Column("updated_at", sa.DateTime(), nullable=False),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("url"),
)
op.execute(
"""
INSERT INTO formation (id, code_nsf, type, libelle, tutelle, url, domain, niveau_de_sortie, duree, created_at, updated_at)
SELECT
UUID(),
f.code_nsf,
f.sigle_type_formation,
f.libelle_formation_principal,
f.tutelle,
f.url_et_id_onisep,
"NULL",
f.niveau_de_sortie_indicatif,
f.duree,
f.created_at,
f.updated_at
FROM favori f;
"""
)
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table("formation")
# ### end Alembic commands ###
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
"""Add UUID in formation table.
Revision ID: ee56a7eaab4a
Revises: 16c8bc08a922
Create Date: 2023-10-29 22:09:53.444244
"""
from alembic import op
from sqlalchemy.dialects import mysql

from src.models.helpers.UUIDType import UUIDType

# revision identifiers, used by Alembic.
revision = "ee56a7eaab4a"
down_revision = "16c8bc08a922"
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###

op.drop_constraint("favori_formation_id_fk", "user_favori", type_="foreignkey")

op.alter_column(
"formation",
"id",
existing_type=mysql.VARCHAR(length=36),
type_=UUIDType(),
existing_nullable=False,
)
op.alter_column(
"user_favori",
"formation_id",
existing_type=mysql.VARCHAR(length=36),
type_=UUIDType(),
existing_nullable=False,
)

op.create_foreign_key(
"favori_formation_id_fk", "user_favori", "formation", ["formation_id"], ["id"]
)
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint("favori_formation_id_fk", "user_favori", type_="foreignkey")

op.alter_column(
"user_favori",
"formation_id",
existing_type=UUIDType(),
type_=mysql.VARCHAR(length=36),
existing_nullable=False,
)
op.alter_column(
"formation",
"id",
existing_type=UUIDType(),
type_=mysql.VARCHAR(length=36),
existing_nullable=False,
)

op.create_foreign_key(
"favori_formation_id_fk", "user_favori", "formation", ["formation_id"], ["id"]
)
# ### end Alembic commands ###
45 changes: 45 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
[tool.isort]
profile = "black"

[tool.black]
py36 = true
include = '\.pyi?$'
exclude = '''
/(
\.git
| \.hg
| \.mypy_cache
| \.tox
| \.venv
| _build
| buck-out
| build
| dist
# The following are specific to Black, you probably don't want those.
| blib2to3
| tests/data
)/
'''

[tool.ruff]
ignore = ["E501"]
line-length = 89
select = [
"B",
"B9",
"C",
"E",
"F",
"W",
]
target-version = "py39"

[tool.ruff.mccabe]
max-complexity = 18

[tool.ruff.per-file-ignores]
"__init__.py" = [
"F401",
"F403",
]
6 changes: 3 additions & 3 deletions src/blueprints/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
HTTP_409_CONFLICT,
)
from src import db
from src.models import User, Favori
from src.models import User, UserFavori

import re
from typing import Tuple
Expand Down Expand Up @@ -212,8 +212,8 @@ def edit_user() -> Tuple[Response, int] | HTTPException:


def remove_favoris(user_id: int) -> None:
favoris = Favori.query.filter_by(request_user_id=user_id).all()
list(map(lambda f: db.session.delete(f), favoris))
favoris = UserFavori.query.filter(UserFavori.user_id == user_id).all()
[db.session.delete(f) for f in favoris]
db.session.commit()


Expand Down
Loading

0 comments on commit a0e06f6

Please sign in to comment.