Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

committing my implementation of the challenge #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
__pycache__/
venv/
117 changes: 117 additions & 0 deletions flask_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
"""Trying out with Flask before using FastAPI"""

import json
import os

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

basedir = os.path.abspath(os.path.dirname(__file__))
app = Flask(__name__)

app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///" + os.path.join(
basedir, "planning.db"
)
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False

db = SQLAlchemy(app)

with open("planning.json") as file:
data = json.load(file)


class User(db.Model):
__tablename__ = "users"

id = db.Column(db.Integer, primary_key=True, unique=True, nullable=False)
original_id = db.Column(db.String(100), unique=True, nullable=False)
talent_id = db.Column(db.String(100))
talent_name = db.Column(db.String(50))
talent_grade = db.Column(db.String(50))
booking_grade = db.Column(db.String(50))
operating_unit = db.Column(db.String(50), nullable=False)
office_city = db.Column(db.String(50))
office_postal_code = db.Column(db.String(50), nullable=False)
job_manager_name = db.Column(db.String(50))
job_manager_id = db.Column(db.String(100))
total_hours = db.Column(db.Float(80), nullable=False)
start_date = db.Column(db.String, nullable=False)
end_date = db.Column(db.String, nullable=False)
client_name = db.Column(db.String(50))
client_id = db.Column(db.String(100), nullable=False)
industry = db.Column(db.String(50))
required_skills = db.Column(db.JSON)
optional_skills = db.Column(db.JSON)
is_unassigned = db.Column(db.Boolean(50))

def __repr__(self):
return f"<User {self.originalId}>"

def __init__(
self,
original_id,
talent_id,
talent_name,
talent_grade,
booking_grade,
operating_unit,
office_city,
office_postal_code,
job_manager_name,
job_manager_id,
total_hours,
start_date,
end_date,
client_name,
client_id,
industry,
required_skills,
optional_skills,
is_unassigned,
):
self.original_id = original_id
self.talent_id = talent_id
self.talent_name = talent_name
self.talent_grade = talent_grade
self.booking_grade = booking_grade
self.operating_unit = operating_unit
self.office_city = office_city
self.office_postal_code = office_postal_code
self.job_manager_name = job_manager_name
self.job_manager_id = job_manager_id
self.total_hours = total_hours
self.start_date = start_date
self.end_date = end_date
self.client_name = client_name
self.client_id = client_id
self.industry = industry
self.required_skills = required_skills
self.optional_skills = optional_skills
self.is_unassigned = is_unassigned


with app.app_conterecordt():
for record in data:
entry = User(
record["originalId"],
record["talentId"],
record["talentName"],
record["talentGrade"],
record["bookingGrade"],
record["operatingUnit"],
record["officeCity"],
record["officePostalCode"],
record["jobManagerName"],
record["jobManagerId"],
record["totalHours"],
record["startDate"],
record["endDate"],
record["clientName"],
record["clientId"],
record["industry"],
record["isUnassigned"],
record["requiredSkills"],
record["isUnassigned"],
)
db.session.add(entry)
db.session.commit()
98 changes: 98 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import json
from typing import List

import databases
import sqlalchemy
from fastapi import FastAPI
from pydantic import BaseModel

DATABASE_URL = "sqlite:///./planning.db"

database = databases.Database(DATABASE_URL)

metadata = sqlalchemy.MetaData()

with open("planning.json") as file:
data = json.load(file)

users = sqlalchemy.Table(
"users",
metadata,
sqlalchemy.Column(
"id", sqlalchemy.Integer, primary_key=True, unique=True, nullable=False
),
sqlalchemy.Column("original_id", sqlalchemy.String, unique=True, nullable=False),
sqlalchemy.Column("talent_id", sqlalchemy.String),
sqlalchemy.Column("talent_name", sqlalchemy.String),
sqlalchemy.Column("talent_grade", sqlalchemy.String),
sqlalchemy.Column("booking_grade", sqlalchemy.String),
sqlalchemy.Column("operating_unit", sqlalchemy.String, nullable=False),
sqlalchemy.Column("office_city", sqlalchemy.String),
sqlalchemy.Column("office_postal_code", sqlalchemy.String, nullable=False),
sqlalchemy.Column("job_manager_name", sqlalchemy.String),
sqlalchemy.Column("job_manager_id", sqlalchemy.String),
sqlalchemy.Column("total_hours", sqlalchemy.String, nullable=False),
sqlalchemy.Column("start_date", sqlalchemy.String, nullable=False),
sqlalchemy.Column("end_date", sqlalchemy.String, nullable=False),
sqlalchemy.Column("client_name", sqlalchemy.String),
sqlalchemy.Column("client_id", sqlalchemy.String, nullable=False),
sqlalchemy.Column("industry", sqlalchemy.String),
sqlalchemy.Column("required_skills", sqlalchemy.JSON),
sqlalchemy.Column("optional_skills", sqlalchemy.JSON),
sqlalchemy.Column("is_unassigned", sqlalchemy.Boolean),
)


engine = sqlalchemy.create_engine(
DATABASE_URL, connect_args={"check_same_thread": False}
)
metadata.create_all(engine)


class User(BaseModel):
id: int
original_id: str
talent_id: str
talent_name: str
talent_grade: str
booking_grade: str
operating_unit: str
office_city: str
office_postal_code: str
job_manager_name: str
job_manager_id: str
total_hours: float
start_date: str
end_date: str
client_name: str
client_id: str
industry: str
required_skills: list[dict]
optional_skills: list[dict]
is_unassigned: bool


app = FastAPI()


@app.on_event("startup")
async def startup():
await database.connect()


@app.on_event("shutdown")
async def shutdown():
await database.disconnect()


@app.get("/users/", response_model=List[User])
async def read_entries():
query = users.select()
return await database.fetch_all(query)


@app.post("/users/", response_model=User)
async def create_entry(user: User):
query = users.insert().values(text=user.text, completed=user.completed)
last_record_id = await database.execute(query)
return {**user.dict(), "id": last_record_id}
Binary file added planning.db
Binary file not shown.
25 changes: 25 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
aiosqlite==0.17.0
anyio==3.6.2
black==22.10.0
click==8.1.3
databases==0.6.2
fastapi==0.87.0
Flask==2.2.2
Flask-SQLAlchemy==3.0.2
greenlet==2.0.1
h11==0.14.0
idna==3.4
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.1
mypy-extensions==0.4.3
pathspec==0.10.2
platformdirs==2.5.4
pydantic==1.10.2
sniffio==1.3.0
SQLAlchemy==1.4.41
starlette==0.21.0
tomli==2.0.1
typing_extensions==4.4.0
uvicorn==0.20.0
Werkzeug==2.2.2