feat: faker contacts
This commit is contained in:
2
Pipfile
2
Pipfile
@@ -36,4 +36,4 @@ deprecated = "*"
|
|||||||
[dev-packages]
|
[dev-packages]
|
||||||
|
|
||||||
[requires]
|
[requires]
|
||||||
python_version = "3.9"
|
python_version = "3.10"
|
||||||
|
|||||||
4
Pipfile.lock
generated
4
Pipfile.lock
generated
@@ -1,11 +1,11 @@
|
|||||||
{
|
{
|
||||||
"_meta": {
|
"_meta": {
|
||||||
"hash": {
|
"hash": {
|
||||||
"sha256": "2fe4324591b62180dbf2b2ca8c54c357d397257cdb0ed7fa01937a119be5ea6a"
|
"sha256": "23773f4bbfa489415484a32b77751e98de78d5a91ad30ac56bea7f0fe86c2ef5"
|
||||||
},
|
},
|
||||||
"pipfile-spec": 6,
|
"pipfile-spec": 6,
|
||||||
"requires": {
|
"requires": {
|
||||||
"python_version": "3.9"
|
"python_version": "3.10"
|
||||||
},
|
},
|
||||||
"sources": [
|
"sources": [
|
||||||
{
|
{
|
||||||
|
|||||||
33
app.py
33
app.py
@@ -3,14 +3,13 @@ import config
|
|||||||
import utils.response as response
|
import utils.response as response
|
||||||
import service.faker_data as faker
|
import service.faker_data as faker
|
||||||
from base import db
|
from base import db
|
||||||
from model import Person, SexEnum, City
|
from model import Person, SexEnum, City, Hotel
|
||||||
|
from sqlalchemy import select, join
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
config.init_config(app)
|
config.init_config(app)
|
||||||
db.init_app(app)
|
db.init_app(app)
|
||||||
db.app = app
|
|
||||||
with app.app_context():
|
|
||||||
db.create_all()
|
|
||||||
|
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
def hello_world(): # put application's code here
|
def hello_world(): # put application's code here
|
||||||
@@ -18,13 +17,27 @@ def hello_world(): # put application's code here
|
|||||||
return 'Hello World!'
|
return 'Hello World!'
|
||||||
|
|
||||||
|
|
||||||
@app.route("/faker")
|
@app.route("/faker/railway/<number>")
|
||||||
def faker_data():
|
def faker_railway(number):
|
||||||
# persons = faker.store_faker_identification(1)
|
return response.succ(faker.faker_railways(int(number)))
|
||||||
# results = [person.__to_dict__() for person in persons]
|
|
||||||
# return response.succ(persons)
|
|
||||||
return response.succ(faker.faker_railways(30))
|
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/faker/hotel/<number>")
|
||||||
|
def faker_hotel(number):
|
||||||
|
return response.succ(faker.faker_hotels(int(number)))
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/faker/contacts/<num_hotel>/<num_railway>")
|
||||||
|
def faker_contacts(num_hotel, num_railway):
|
||||||
|
return response.succ(faker.faker_contacts(int(num_hotel), int(num_railway)))
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/test")
|
||||||
|
def test():
|
||||||
|
print(
|
||||||
|
select(Hotel.identification).
|
||||||
|
join(Hotel, )
|
||||||
|
)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app.run()
|
app.run()
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import copy
|
||||||
import random
|
import random
|
||||||
import requests
|
import requests
|
||||||
import threading
|
import threading
|
||||||
@@ -5,6 +6,8 @@ import random
|
|||||||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||||
from lxml import etree
|
from lxml import etree
|
||||||
from faker import Faker
|
from faker import Faker
|
||||||
|
|
||||||
|
import utils.model
|
||||||
from model import SexEnum, Person, Hotel, Railway, City
|
from model import SexEnum, Person, Hotel, Railway, City
|
||||||
from base import db
|
from base import db
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
@@ -78,7 +81,9 @@ def faker_identifications(num: int) -> list[Person]:
|
|||||||
persons = [faker_identification() for i in range(num)]
|
persons = [faker_identification() for i in range(num)]
|
||||||
area_codes = [person.identification[0:6] for person in persons]
|
area_codes = [person.identification[0:6] for person in persons]
|
||||||
rows = City.query.filter(City.ad_code.in_(area_codes))
|
rows = City.query.filter(City.ad_code.in_(area_codes))
|
||||||
code_area_dict = {row.ad_code: '{} {} {} {}'.format(row.country_cn, row.province_cn, row.admin_district_cn, row.city_cn) for row in rows}
|
code_area_dict = {
|
||||||
|
row.ad_code: '{} {} {} {}'.format(row.country_cn, row.province_cn, row.admin_district_cn, row.city_cn) for row
|
||||||
|
in rows}
|
||||||
for person in persons:
|
for person in persons:
|
||||||
person.address = code_area_dict[person.identification[0:6]]
|
person.address = code_area_dict[person.identification[0:6]]
|
||||||
db.session.bulk_save_objects(persons)
|
db.session.bulk_save_objects(persons)
|
||||||
@@ -108,4 +113,61 @@ def generate_train_id():
|
|||||||
num = int(random.random() * 10000).__str__()
|
num = int(random.random() * 10000).__str__()
|
||||||
return prefix + num
|
return prefix + num
|
||||||
|
|
||||||
# def faker_hotel(num: int):
|
|
||||||
|
def faker_hotels(num: int) -> list[Hotel]:
|
||||||
|
persons = faker_identifications(num)
|
||||||
|
hotels = [faker_hotel(person) for person in persons]
|
||||||
|
db.session.bulk_save_objects(hotels)
|
||||||
|
db.session.commit()
|
||||||
|
return hotels
|
||||||
|
|
||||||
|
|
||||||
|
def faker_hotel(person: Person) -> Hotel:
|
||||||
|
hotel = faker_hotel_infos[random.randint(0, len(faker_hotel_infos) - 1)]
|
||||||
|
hotel.identification = person.identification
|
||||||
|
hotel.in_data = datetime.today() + timedelta(days=(int(random.random() * 21) - 10))
|
||||||
|
hotel.out_data = hotel.in_data + timedelta(days=3)
|
||||||
|
return hotel
|
||||||
|
|
||||||
|
|
||||||
|
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)
|
||||||
|
hotel_patient = faker_hotel(patient)
|
||||||
|
railway_patient = faker_railway(patient)
|
||||||
|
|
||||||
|
def assemble_hotel(contact: Person) -> Hotel:
|
||||||
|
hotel_contact = copy.copy(hotel_patient)
|
||||||
|
hotel_contact.identification = contact.identification
|
||||||
|
return hotel_contact
|
||||||
|
|
||||||
|
def assemble_railway(contact: Person) -> Railway:
|
||||||
|
railway_contact = copy.copy(railway_patient)
|
||||||
|
railway_contact.identification = contact.identification
|
||||||
|
railway_contact.name = contact.name
|
||||||
|
return railway_contact
|
||||||
|
|
||||||
|
hotel_contacts = [assemble_hotel(contact) for contact in contacts_hotel]
|
||||||
|
railway_contacts = [assemble_railway(contact) for contact in contacts_railway]
|
||||||
|
hotels = [hotel_patient] + hotel_contacts
|
||||||
|
railways = [railway_patient] + railway_contacts
|
||||||
|
db.session.bulk_save_objects(hotels)
|
||||||
|
db.session.bulk_save_objects(railways)
|
||||||
|
db.session.commit()
|
||||||
|
return {
|
||||||
|
"patient": {
|
||||||
|
"hotel": utils.model.model2dict(hotel_patient),
|
||||||
|
"railway": utils.model.model2dict(railway_patient)
|
||||||
|
},
|
||||||
|
"contacts": {
|
||||||
|
"hotel": utils.model.models2dicts(hotel_contacts),
|
||||||
|
"railway": utils.model.models2dicts(railway_contacts)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user