From eed9c9d2c0f5ade3450306765e140aba090b2362 Mon Sep 17 00:00:00 2001 From: elena perez Date: Mon, 21 Nov 2022 17:07:34 +0100 Subject: [PATCH] committing my implementation of the challenge --- .gitignore | 2 + flask_test.py | 117 +++++++++++++++++++++++++++++++++++++++++++++++ main.py | 98 +++++++++++++++++++++++++++++++++++++++ planning.db | Bin 0 -> 16384 bytes requirements.txt | 25 ++++++++++ 5 files changed, 242 insertions(+) create mode 100644 .gitignore create mode 100644 flask_test.py create mode 100644 main.py create mode 100644 planning.db create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0540009 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +__pycache__/ +venv/ \ No newline at end of file diff --git a/flask_test.py b/flask_test.py new file mode 100644 index 0000000..f8bf99d --- /dev/null +++ b/flask_test.py @@ -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"" + + 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() diff --git a/main.py b/main.py new file mode 100644 index 0000000..cd2c9e9 --- /dev/null +++ b/main.py @@ -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} diff --git a/planning.db b/planning.db new file mode 100644 index 0000000000000000000000000000000000000000..4a4dd0381c80c5437f0af5f9f959380aa95442f7 GIT binary patch literal 16384 zcmeI#L2uJA6bEo;oq$!Ux?3bpJ}weM0?tTeQv@9$Wm$`~1G3!44Q6TF*^UG!E?oE; zd>_6KM-sEBO}C08QuU9NM0qc^pMP-8Nkk{%X&g}(q9^?$nbA=)iC#x3O~wquXG~m!fU75zaNwI`NrO*RssnorGEm&25vaN@?4!y7esGYGTv}woe^jeRpEz5Yl zJ-v@il-cg&ypWCOb}_f%bse1nWtmmR>W+XGHPW9C{Gg`ww{mBnx(4sl(Yr9cqHm)sdLpx+>13LWE~e3)$Bo4j^n;-Hz<=p`q^?eU zF*ilMU1z+q$~`l;@Yr*AI}8r~{pvIKYOnsQ9yNc~pt&437aS0P00bZa0SG_<0uX=z z1Rwwb2<)psznK{J|NHuRF)s)}00Izz00bZa0SG_<0uX?J6Tto-3jqQUfB*y_009U< P00Izz00bbg{{nviz!d1) literal 0 HcmV?d00001 diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..d510c75 --- /dev/null +++ b/requirements.txt @@ -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