From b53629ae6e0121a7d691fa4663a91a0dbf0f780e Mon Sep 17 00:00:00 2001 From: Fabio Scotto di Santolo Date: Thu, 24 Jun 2021 20:59:13 +0200 Subject: [PATCH] Aggiunto modulo per la conversione degli oggetti in JSON --- app.py | 9 +++++++-- models/department.py | 4 ++-- models/employee.py | 6 +++--- models/location.py | 2 +- requirements.txt | 3 ++- 5 files changed, 15 insertions(+), 9 deletions(-) diff --git a/app.py b/app.py index d678510..7b84d0d 100644 --- a/app.py +++ b/app.py @@ -4,6 +4,7 @@ from typing import List, Dict from dateutil import relativedelta as period from flask import Flask +from flask_json import FlaskJSON from db.access import fetch_european_departments, \ fetch_department_employees, \ @@ -14,6 +15,10 @@ from db.access import fetch_european_departments, \ from models.department import Department app = Flask(__name__) +FlaskJSON(app) + +app.config['JSON_DATE_FORMAT'] = '%d/%m/%Y' +app.config['JSON_USE_ENCODE_METHODS'] = True @app.get("/") @@ -33,7 +38,7 @@ def handle_error(error): @app.get("/employee") def all_employees(): - return {"employees": [e.to_json() for e in fetch_employees()]} + return {"employees": [e for e in fetch_employees()]} def __employees_group_by_seniority(departments: List[Department]) -> Dict[str, dict]: @@ -46,7 +51,7 @@ def __employees_group_by_seniority(departments: List[Department]) -> Dict[str, d employee_group_by_years = itertools.groupby(department_employees, lambda e: period.relativedelta(date.today(), e.hire_date).years) - result[department.name] = {year: [e.to_json() for e in employees] + result[department.name] = {year: [e for e in employees] for (year, employees) in employee_group_by_years} return result diff --git a/models/department.py b/models/department.py index 6b7194a..65ea1e9 100644 --- a/models/department.py +++ b/models/department.py @@ -19,11 +19,11 @@ class Department: def location_id(self) -> Location: return self.__location - def to_json(self): + def __json__(self): return { 'department_id': self.__department_id, 'name': self.__name, - 'location': self.__location.to_json() if self.__location is not None else None + 'location': self.__location if self.__location is not None else None } def __str__(self): diff --git a/models/employee.py b/models/employee.py index 7241209..706008f 100644 --- a/models/employee.py +++ b/models/employee.py @@ -62,14 +62,14 @@ class Employee(Person): def manager_id(self) -> int: return self.__manager_id - def to_json(self): + def __json__(self): return { 'employee_id': self.__employee_id, 'first_name': self.first_name, 'last_name': self.last_name, 'email': self.__email, - 'hire_date': f"{self.__hire_date: %Y-%m-%d}", - 'department': self.__department.to_json() if self.__department is not None else None, + 'hire_date': self.__hire_date, + 'department': self.__department if self.__department is not None else None, 'job_id': self.__job_id, 'salary': f"{self.__salary:,.2f}", 'manager_id': self.__manager_id diff --git a/models/location.py b/models/location.py index 7d73cb2..125dc63 100644 --- a/models/location.py +++ b/models/location.py @@ -32,7 +32,7 @@ class Location: def country_id(self) -> int: return self.__country_id - def to_json(self): + def __json__(self): return { 'location_id': self.__location_id, 'street_address': self.__street_address, diff --git a/requirements.txt b/requirements.txt index cbe7c79..e0bc9b0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,4 +2,5 @@ records~=0.5.3 psycopg2-binary~=2.9.1 setuptools~=57.0.0 python-dateutil~=2.8.1 -Flask~=2.0.1 \ No newline at end of file +Flask~=2.0.1 +Flask-JSON~=0.3.4 \ No newline at end of file