diff --git a/app.py b/app.py index 101d6d7..ca59d28 100644 --- a/app.py +++ b/app.py @@ -8,9 +8,9 @@ from model import Person, SexEnum, City app = Flask(__name__) config.init_config(app) db.init_app(app) -# with app.app_context(): -# print(db.session.query(City).one()) - +db.app = app +with app.app_context(): + db.create_all() @app.route('/') def hello_world(): # put application's code here diff --git a/service/faker_data.py b/service/faker_data.py index f1030b1..199fba4 100644 --- a/service/faker_data.py +++ b/service/faker_data.py @@ -25,6 +25,11 @@ faker_hotel_infos = [ faker_train_ids = ['D7359', 'T4108', 'D776', 'D3023', 'K1461', 'L1500', 'G662'] +ident_valid_ratio = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2] +ident_valid_result = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'] +ident_area = ['320700', '320722', '320707', '320723', '320724', '320706', '320703', '320500', '320581', '320582', + '320583', '320506', '320505', '320509', '320585', '320507', '320508'] + # Due to the limit of reptile, turn to query the city by city code @deprecated @@ -46,16 +51,22 @@ def faker_identification_old() -> Person: def faker_identification() -> Person: - identification = faker.ssn(max_age=70) - birthday = parser.parse(identification[6:14]) - age = rrule.rrule(rrule.YEARLY, dtstart=birthday, until=datetime.today()) + area = ident_area[random.randint(0, len(ident_area) - 1)] + birthdate = datetime( + year=2022 - random.randint(18, 70), + month=random.randint(1, 12), + day=random.randint(1, 28) + ) + birthday = birthdate.__format__("%Y%m%d") + random_num = random.randint(100, 999).__str__() + identification = area + birthday + random_num + valid = ident_valid_result[sum([int(identification[i]) * ident_valid_ratio[i] for i in range(17)]) % 11] + identification = identification + valid + birthdate = parser.parse(identification[6:14]) + age = rrule.rrule(rrule.YEARLY, dtstart=birthdate, until=datetime.today()).count() sex = 0 if int(identification[16:17]) % 2 == 0 else 1 - # Better out of cycle - city = db.session.query(City).filter(City.ad_code == identification[0:6]).one() - address = '{} {} {} {}'.format(city.country_cn, city.province_cn, city.admin_district_cn, city.city_cn) return Person( identification=identification, - address=address, sex=sex, name=faker.last_name() + (faker.first_name_male() if sex == SexEnum.MALE.sex else faker.first_name_female()), age=age, @@ -64,18 +75,20 @@ def faker_identification() -> Person: def faker_identifications(num: int) -> list[Person]: - all_tasks = [executor.submit(faker_identification) for i in range(num)] - futures = as_completed(all_tasks) - persons = [future.result() for future in futures] - db.session.add_all(persons) - db.session.commit() + persons = [faker_identification() for i in range(num)] + area_codes = [person.identification[0:6] for person in persons] + 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} + for person in persons: + person.address = code_area_dict[person.identification[0:6]] + db.session.bulk_save_objects(persons) return persons def faker_railways(num: int) -> list[Railway]: persons = faker_identifications(num) railways = [faker_railway(person) for person in persons] - db.session.add_all(railways) + db.session.bulk_save_objects(railways) db.session.commit() return railways