feat: faker data: query

This commit is contained in:
Zhaolong
2022-02-07 16:28:14 +08:00
parent a9c08e3257
commit 5f56feffb2
6 changed files with 81 additions and 20 deletions

View File

@@ -134,21 +134,17 @@ def faker_contacts(num_hotel: int, num_railway: int):
persons = faker_identifications(num_railway + num_hotel + 1)
patient = persons[0]
contacts_hotel = persons[1:1 + num_hotel]
contacts_railway = persons[-1-num_railway: -1]
print(persons)
print(patient)
print(contacts_railway)
print(contacts_hotel)
contacts_railway = persons[0 - num_railway:]
hotel_patient = faker_hotel(patient)
railway_patient = faker_railway(patient)
def assemble_hotel(contact: Person) -> Hotel:
hotel_contact = copy.copy(hotel_patient)
hotel_contact = copy.deepcopy(hotel_patient)
hotel_contact.identification = contact.identification
return hotel_contact
def assemble_railway(contact: Person) -> Railway:
railway_contact = copy.copy(railway_patient)
railway_contact = copy.deepcopy(railway_patient)
railway_contact.identification = contact.identification
railway_contact.name = contact.name
return railway_contact
@@ -171,3 +167,45 @@ def faker_contacts(num_hotel: int, num_railway: int):
}
}
def query_patients() -> list[dict]:
patients = db.session.query(Person) \
.join(Hotel, Person.identification == Hotel.identification) \
.join(Railway, Person.identification == Railway.identification) \
.all()
return [patient.__to_dict__() for patient in patients]
def query_contacts_hotel(identification: str) -> list[dict]:
patient_subquery = Hotel.query.filter(Hotel.identification == identification).subquery()
contacts = db.session.query(Hotel, Person) \
.join(Hotel, Hotel.identification == Person.identification) \
.filter(Person.identification != identification) \
.filter(Hotel.out_data > patient_subquery.c.in_data)
return [merge_object2dict(hotel, person) for hotel, person in contacts]
def query_contacts_railway(identification: str) -> list[dict]:
patient_subquery = Railway.query.filter(Railway.identification == identification).subquery()
contacts = db.session.query(Railway, Person) \
.join(Railway, Railway.identification == Person.identification) \
.filter(Person.identification != identification) \
.filter(Railway.launch == patient_subquery.c.launch)
return [merge_object2dict(railway, person) for railway, person in contacts]
def query_contacts_classify(identification: str) -> dict[str, list[dict]]:
contacts = {'hotel': query_contacts_hotel(identification), 'railway': query_contacts_railway(identification)}
return contacts
def query_contacts(identification: str) -> list[dict]:
contacts = [*query_contacts_hotel(identification), *query_contacts_railway(identification)]
return contacts
def merge_object2dict(*objs: db.Model) -> dict:
res = {}
for obj in objs:
res.update(obj.__to_dict__())
return res