diff --git a/app/views.py b/app/views.py index 36aeb9a22..105b8c9e2 100644 --- a/app/views.py +++ b/app/views.py @@ -1665,11 +1665,18 @@ def subscribeOrganization(request: UserRequest): me = get_person_or_org(request.user) # orgava_list = [(org, utils.get_user_ava(org, UTYPE_ORG)) for org in org_list] - otype_infos = [( - otype, - list(Organization.objects.activated().filter(otype=otype) - .select_related("organization_id")), - ) for otype in OrganizationType.objects.all().order_by('-otype_id')] + # 获取所有组织类型 + organization_types = list(OrganizationType.objects.all().order_by('-otype_id')) + + # 强制只执行一次查询,prefetch防止多次查询 + organizations = list(Organization.objects.activated().select_related('otype').prefetch_related('organization_id')) + + # 获取组织信息 + otype_infos_dict = {otype: [] for otype in organization_types} + for org in organizations: + otype_infos_dict[org.otype].append(org) + otype_infos = [(otype, otype_infos_dict[otype]) for otype in organization_types] + # 获取不订阅列表(数据库里的是不订阅列表) if request.user.is_person(): diff --git a/scripts/generate_testdata.py b/scripts/generate_testdata.py new file mode 100644 index 000000000..612b73f77 --- /dev/null +++ b/scripts/generate_testdata.py @@ -0,0 +1,114 @@ +import os +import sys +import django + +sys.path.append(os.path.abspath('.')) +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'boot.settings') +django.setup() + +from tqdm import tqdm +from faker import Faker +import faker as faker +from app.models import NaturalPerson, User, Organization, OrganizationType + + +# STOP SAVING THIS FILE AUTOMATICALLY +# As it will mess up the import list + + +USER_COUNT = 200 +ORGANIZATION_COUNT = 100 +ORGANIZATION_TYPE_COUNT = 10 + +fk = faker.Faker(locale='zh_CN') +fake = Faker('zh_CN') + + +def create_fake_user_for_orgnizaiton(): + username = fake.user_name() + email = fake.email() + password = fake.password() + stuid = fake.unique.random_number(digits=8) + user = User.objects.create_user( + username=stuid, name=username, email=email, password=password, usertype=User.Type.ORG) + return user + + +def create_fake_user_for_natural_person(): + username = fake.user_name() + email = fake.email() + password = fake.password() + stuid = fake.unique.random_number(digits=8) + user = User.objects.create_user( + username=stuid, name=username, email=email, password=password, usertype=User.Type.STUDENT) + return user + + +def create_fake_natural_person(): + user = create_fake_user_for_natural_person() + person = NaturalPerson( + person_id=user, + name=fake.unique.name(), + nickname=fake.first_name(), + gender=fake.random_element(elements=[0, 1]), + birthday=fake.date_of_birth(), + email=fake.email(), + telephone=fake.phone_number(), + biography=fake.text(max_nb_chars=200), + inform_share=fake.boolean(), + last_time_login=fake.date_time_this_year(), + identity=fake.random_element(elements=[0, 1]), + stu_class=fake.random_int(min=1, max=5), + stu_major=fake.text(max_nb_chars=25), + stu_grade=fake.random_int(min=1, max=5), + stu_dorm=fake.random_int(min=100, max=999), + status=0, + show_nickname=fake.boolean(), + show_birthday=fake.boolean(), + show_gender=fake.boolean(), + show_email=fake.boolean(), + show_tel=fake.boolean(), + show_major=fake.boolean(), + show_dorm=fake.boolean(), + ) + person.save() + + +def create_fake_organization_type(n: int): + otype = OrganizationType( + otype_id=n, + otype_name=fake.unique.company(), + ) + otype.save() + + +def create_fake_organization(): + user = create_fake_user_for_orgnizaiton() + organization_type = OrganizationType.objects.get( + otype_id=fake.random_int(min=0, max=ORGANIZATION_TYPE_COUNT-1)) + organization = Organization( + organization_id=user, + oname=fake.unique.company(), + otype=organization_type, + ) + organization.save() + + +if __name__ == '__main__': + print('Generating fake data...') + # 生成小组类型 + for i in tqdm(range(ORGANIZATION_TYPE_COUNT), desc='Creating Organization Types'): + create_fake_organization_type(i) + actucal_user_count = 0 + # 生成自然人 + for _ in tqdm(range(USER_COUNT), desc='Creating Users'): + try: + create_fake_natural_person() + actucal_user_count += 1 + except Exception as e: + pass # We don't care about duplicate users + # 生成小组 + for _ in tqdm(range(ORGANIZATION_COUNT), desc='Creating Organizations'): + create_fake_organization() + print('Done!') + print(f'Created {actucal_user_count} users, {ORGANIZATION_COUNT} organizations with {ORGANIZATION_TYPE_COUNT} organization types.')