Skip to content

Commit

Permalink
start configure strawberry
Browse files Browse the repository at this point in the history
  • Loading branch information
Angel-Dijoux committed Jan 20, 2024
1 parent c6b128e commit 06a72db
Show file tree
Hide file tree
Showing 12 changed files with 309 additions and 92 deletions.
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ marshmallow-sqlalchemy = "*"
apispec = "*"
chardet = "*"
charset-normalizer = "*"
strawberry-graphql = {extras = ["debug-server"], version = "*"}

[dev-packages]

Expand Down
303 changes: 224 additions & 79 deletions Pipfile.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from apispec.ext.marshmallow import MarshmallowPlugin
from apispec_webframeworks.flask import FlaskPlugin


db: SQLAlchemy = SQLAlchemy()
plugins = [
FlaskPlugin(),
Expand Down Expand Up @@ -63,12 +64,14 @@ def register_blueprints(app: Flask):
from src.blueprints.formations import formations
from src.blueprints.utils import utils
from src.blueprints.legal.views import legal
from src.blueprints.graphql import graphql

app.register_blueprint(utils)
app.register_blueprint(auth)
app.register_blueprint(legal)
app.register_blueprint(favoris)
app.register_blueprint(formations)
app.register_blueprint(graphql)


def _set_log_levels():
Expand Down
14 changes: 14 additions & 0 deletions src/api/form.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import typing
from src.models.formation import Formation
import strawberry
from src import db


@strawberry.type
class Book:
title: str
author: str


def get_formations():
return db.session.query(Formation).all()
29 changes: 29 additions & 0 deletions src/api/schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from pydoc import resolve
import typing
from src.business_logic.formation.get_formation_details import (
Formation,
get_formation_by_id,
)
from src.models import User
import strawberry

from src import db


@strawberry.type
class Book:
title: str
author: str


def get_users():
return db.session.query(User).all()


@strawberry.type
class Query:
users: typing.List[User] = strawberry.field(resolver=get_users)
formation: Formation = strawberry.field(resolver=get_formation_by_id)


schema = strawberry.Schema(Query)
14 changes: 14 additions & 0 deletions src/blueprints/graphql.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from flask import Blueprint, Response, jsonify
from strawberry.flask.views import GraphQLView

from src.constants.http_status_codes import HTTP_200_OK

from src.api.schema import schema

graphql = Blueprint("graphql", __name__, url_prefix="/api/graphql")


@graphql.route("/", methods=["POST", "GET"])
def gql() -> tuple[Response, int]:
view = GraphQLView.as_view("graphql_view", schema=schema)
return view()
6 changes: 4 additions & 2 deletions src/business_logic/formation/get_formation_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
process_continuation_studies,
)

import strawberry


@dataclass
@strawberry.type
class Formation:
id: str
exceptions: Optional[ParcourSupExpectations]
Expand Down Expand Up @@ -60,7 +63,6 @@ def _process_formation(for_id: str) -> Formation:
certificat = formation["nature_certificat"]["libelle_nature_certificat"]
sigle = formation["sigle"] if formation["sigle"] else None
type_sigle = formation["type_Formation"]["type_formation_sigle"]

metier = formation["metiers_formation"]["metier"]
jobs = process_jobs(metier if metier else None)

Expand All @@ -84,7 +86,7 @@ def _process_formation(for_id: str) -> Formation:
)


def get_formation_by_id(for_id: str) -> Formation:
def get_formation_by_id(for_id: str = "FOR.1234") -> Formation:
try:
return _process_formation(for_id)
except Exception as e:
Expand Down
2 changes: 2 additions & 0 deletions src/business_logic/formation/job/get_job_by_formation.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from dataclasses import dataclass
import strawberry


@dataclass
@strawberry.type
class Job:
id: str
libelle: str
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@
from typing import Optional
from bs4 import BeautifulSoup

import strawberry


@dataclass
@strawberry.type
class Expectation:
title: str
sub_expectations: Optional[list[str]] = field(default_factory=list)


@dataclass
@strawberry.type
class ParcourSupExpectations:
title: str
expectations: list[Expectation]
Expand Down Expand Up @@ -57,7 +61,6 @@ def _extract_expectations(self) -> list[Expectation]:

if current_expectation:
self.__expectations.append(current_expectation)

return self.__expectations

def _extract_title(self) -> str:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from dataclasses import dataclass, field
from typing import Optional

import strawberry


@strawberry.type
@dataclass
class ContinuationOfStudies:
title: str = ""
Expand Down
2 changes: 2 additions & 0 deletions src/models/formation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from sqlalchemy import Column, Integer, String, Text
from src.models.base_model import BaseModel
from src.models.helpers.UUIDType import UUIDType
import strawberry


def default_uuid5():
Expand All @@ -11,6 +12,7 @@ def default_uuid5():
return uuid.uuid5(namespace, name)


@strawberry.type
class Formation(BaseModel):
__tablename__ = "formation"

Expand Down
19 changes: 9 additions & 10 deletions src/models/user.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
from dataclasses import dataclass
from typing import List, Optional

from sqlalchemy import Column, Integer, String, Text
from src.models.base_model import BaseModel
from src.models.user_favori import UserFavori
from sqlalchemy.orm import relationship
from sqlalchemy.orm import Mapped

import strawberry


# Create User row


@dataclass
@strawberry.type
class User(BaseModel):
__tablename__ = "user"

id: int
username: str
email: str
profile_pic_url: str

id = Column(Integer, primary_key=True)
username = Column(String(80), unique=True, nullable=False)
email = Column(String(200), unique=True, nullable=False)
id: int = Column(Integer, primary_key=True)
username: str = Column(String(80), unique=True, nullable=False)
email: str = Column(String(200), unique=True, nullable=False)
password = Column(Text(), nullable=False)
profile_pic_url = Column(Text)
profile_pic_url: Optional[str] = Column(Text)

favoris = relationship(
"UserFavori",
Expand Down

0 comments on commit 06a72db

Please sign in to comment.