Aggiunto modulo per la conversione degli oggetti in JSON

This commit is contained in:
Fabio Scotto di Santolo
2021-06-24 20:59:13 +02:00
parent a3e5229bfa
commit b53629ae6e
5 changed files with 15 additions and 9 deletions

9
app.py
View File

@@ -4,6 +4,7 @@ from typing import List, Dict
from dateutil import relativedelta as period from dateutil import relativedelta as period
from flask import Flask from flask import Flask
from flask_json import FlaskJSON
from db.access import fetch_european_departments, \ from db.access import fetch_european_departments, \
fetch_department_employees, \ fetch_department_employees, \
@@ -14,6 +15,10 @@ from db.access import fetch_european_departments, \
from models.department import Department from models.department import Department
app = Flask(__name__) app = Flask(__name__)
FlaskJSON(app)
app.config['JSON_DATE_FORMAT'] = '%d/%m/%Y'
app.config['JSON_USE_ENCODE_METHODS'] = True
@app.get("/") @app.get("/")
@@ -33,7 +38,7 @@ def handle_error(error):
@app.get("/employee") @app.get("/employee")
def all_employees(): 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]: 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, employee_group_by_years = itertools.groupby(department_employees,
lambda e: period.relativedelta(date.today(), e.hire_date).years) 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} for (year, employees) in employee_group_by_years}
return result return result

View File

@@ -19,11 +19,11 @@ class Department:
def location_id(self) -> Location: def location_id(self) -> Location:
return self.__location return self.__location
def to_json(self): def __json__(self):
return { return {
'department_id': self.__department_id, 'department_id': self.__department_id,
'name': self.__name, '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): def __str__(self):

View File

@@ -62,14 +62,14 @@ class Employee(Person):
def manager_id(self) -> int: def manager_id(self) -> int:
return self.__manager_id return self.__manager_id
def to_json(self): def __json__(self):
return { return {
'employee_id': self.__employee_id, 'employee_id': self.__employee_id,
'first_name': self.first_name, 'first_name': self.first_name,
'last_name': self.last_name, 'last_name': self.last_name,
'email': self.__email, 'email': self.__email,
'hire_date': f"{self.__hire_date: %Y-%m-%d}", 'hire_date': self.__hire_date,
'department': self.__department.to_json() if self.__department is not None else None, 'department': self.__department if self.__department is not None else None,
'job_id': self.__job_id, 'job_id': self.__job_id,
'salary': f"{self.__salary:,.2f}", 'salary': f"{self.__salary:,.2f}",
'manager_id': self.__manager_id 'manager_id': self.__manager_id

View File

@@ -32,7 +32,7 @@ class Location:
def country_id(self) -> int: def country_id(self) -> int:
return self.__country_id return self.__country_id
def to_json(self): def __json__(self):
return { return {
'location_id': self.__location_id, 'location_id': self.__location_id,
'street_address': self.__street_address, 'street_address': self.__street_address,

View File

@@ -2,4 +2,5 @@ records~=0.5.3
psycopg2-binary~=2.9.1 psycopg2-binary~=2.9.1
setuptools~=57.0.0 setuptools~=57.0.0
python-dateutil~=2.8.1 python-dateutil~=2.8.1
Flask~=2.0.1 Flask~=2.0.1
Flask-JSON~=0.3.4