123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- # -*- coding: UTF-8 -*-
- import requests
- import sys
- import json
- import random
- import string
- import os
- from csv import reader
- from user_test import get_user, get_all_users, delete_user
- from group_test import get_group_by_name, get_group, create_group, add_user_to_group
- from exam_test import create_exam, create_question, get_question, add_question_to_exam
- from category_test import create_material, get_material, create_category, get_category, add_material_to_category, update_category, create_question_category, get_question_category, questions_in_category, add_question_to_question_category, update_question_category
- from learningmaterial_test import add_comment
- USERNAME_RANDOM_LENGTH = 10
- override = False
- want_material = False
- want_user = False
- want_question = False
- if len(sys.argv) > 1:
- for arg in sys.argv:
- if arg == 'override':
- override = True
- if arg == 'material':
- want_material = True
- if arg == 'user':
- want_user = True
- if arg == 'question':
- want_question = True
- def _random_string(length):
- letters = string.ascii_letters
- return ''.join(random.choice(letters) for i in range(length))
- def _text(resp):
- return json.loads(resp.text)
- # 创建 10 个 前缀为namePrefix 密码为password的 用户
- def add_dummy_users(namePrefix = 'testuser-', password = 'test', number = 10):
- groups = add_groups()
- users = []
- nicknames = ['小强', '小张', '小李子', '大王']
- firstNames = ['一', '二', '三', '四', '五']
- lastNames = ['张', '王', '李', '杜', '靳']
- for i in range(number):
- user_name = namePrefix + _random_string(USERNAME_RANDOM_LENGTH)
- # register_user(user_name, password)
- new_user = {
- 'userName': user_name,
- 'password': password,
- 'nickName': nicknames[i % len(nicknames)],
- 'studyId': 'U0000' + str(i),
- 'email': 'test@gmail.com',
- 'firstName': firstNames[i % len(firstNames)],
- 'lastName': lastNames[i % len(lastNames)],
- 'phone': '13060901234',
- 'birthday': '1999-01-01',
- }
- register_user(new_user)
- user = get_user(user_name)
- random_group = groups[random.randint(0, len(groups)-1)]
- add_user_to_group(random_group, _text(user))
- users.append(_text(user)['userName'])
- print('created ' + str(len(users)) + ' users: ' + ','.join(users))
- global all_users
- all_users = users
- return users
- # 创建 3个二级的group
- # orgtest-level1
- # orgtest-level2-a orgtest-level2-b orgtest-level2-c
- def add_groups(namePrefix = 'orgtest-', number=3):
- # creating level1 group
- level1_name = namePrefix + 'level1-' + _random_string(3)
- level1_group_body = {
- 'name': level1_name
- }
- create_group(level1_group_body)
- level1_group = _text(get_group_by_name(level1_name))
- groups = []
- for i in range(number):
- level2_group_body = {
- 'name': namePrefix + 'level2-' + _random_string(3),
- 'parentGroupId': level1_group['id']
- }
- resp = create_group(level2_group_body)
- groupId = _text(resp)['id']
- groups.append(groupId)
- print ('created ' + str(len(groups)) + ' groups: ' + ','.join(groups))
- return groups
-
- def randomly_pick_user():
- global all_users
- rand_index = random.randint(0, len(all_users) - 1)
- return all_users[rand_index]
- def create_exams(namePrefix='测试考试-', number=2):
- exams = []
- for i in range(number):
- new_exam = {
- 'name': namePrefix + _random_string(3),
- 'description': '可以回家做',
- 'duration': random.randint(0, 863) * 100, # seconds
- }
- resp = create_exam(new_exam)
- exams.append(_text(resp)['id'])
- print ('created ' + str(len(exams)) + ' exams: ' + ','.join(exams))
- return exams
- def create_questions(namePrefix='qtest-', number=10, from_file=False):
- exams = create_exams()
- questions = []
- question_categories = create_question_categories()
- if from_file:
- files = os.listdir('./demo-questions/')
- for file_name in files:
- with open('./demo-questions/' + file_name, 'r') as read_obj:
- print('loading ' + file_name + ' to db')
- csv_reader = reader(read_obj)
- for row in csv_reader:
- qNumber = row[0]
- if qNumber == '题号' or len(qNumber) < 1:
- continue
- content = row[1] # required
- temp_type = row[2] # required
- qType = 'DanXuan'
- if temp_type == '2':
- qType = 'DuoXuan'
- if temp_type == '3':
- qType = 'PanDuan'
- answers = [] # required
- for i in [3, 4, 5, 6]:
- if len(row[i]) > 0:
- answers.append(row[i])
- finalAnswer = row[7] # required
- answerAnalysis = row[8]
- keyword = row[9];
- level = row[10];
- tags = []
- if len(row[11]) > 0:
- tags.append(row[11])
- if len(content) == 0 or len(qType) == 0 or len(answers) == 0 or len(finalAnswer) == 0:
- print('Skip due to required info missing')
- continue
- question = {
- 'userId': randomly_pick_user(),
- 'content': content,
- 'answers': answers,
- 'finalAnswer': [finalAnswer],
- 'keyword': keyword,
- 'type': qType,
- 'questionLevel': level,
- 'tags': tags,
- 'answerAnalysis': answerAnalysis,
- }
- resp = create_question(question)
- random_exam_id = exams[random.randint(0, len(exams)-1)]
- # add newly created question to a random exam
- add_question_to_exam(random_exam_id, [_text(resp)])
- questions.append(_text(resp)['id'])
- else:
- for i in range(number):
- question = {
- 'userId': randomly_pick_user(),
- 'content': namePrefix + '天上什么星星最亮?-' + str(i),
- 'answers': ['太阳', '小天狼星', '北极星'],
- 'finalAnswer': ['小天狼星'],
- 'type': 'DanXuan' # 单选题
- }
- resp = create_question(question)
- random_exam_id = exams[random.randint(0, len(exams)-1)]
- # add newly created question to a random exam
- add_question_to_exam(random_exam_id, [_text(resp)])
- questions.append(_text(resp)['id'])
- tiankong_question = {
- 'userId': randomly_pick_user(),
- 'content': namePrefix + '天上$PH$最亮?-' + str(i),
- 'finalAnswer': ['小天狼星'],
- 'type': 'TianKong' # 填空题
- }
- resp = create_question(tiankong_question)
- random_exam_id = exams[random.randint(0, len(exams)-1)]
- # add newly created question to a random exam
- add_question_to_exam(random_exam_id, [_text(resp)])
- random_category_id = question_categories[random.randint(0, len(question_categories)-1)]
- add_question_to_question_category(random_category_id, _text(resp))
- questions.append(_text(resp)['id'])
- print ('created ' + str(len(questions)) + ' questions')
- return questions
- def create_question_categories(namePrefix= '知识类-', number=3):
- resp = create_question_category(namePrefix + _random_string(3))
- question_root_id = _text(resp)['id']
- categories = []
- names = ['科普类-', '技术类-', '历史类-', '天文类-', '兴趣类-']
- for i in range(number):
- child = create_question_category(names[i % len(names)] + _random_string(3))
- child_id = _text(child)['id']
- update_question_category(child_id, {
- 'parentId': question_root_id
- })
- categories.append(child_id)
- print ('created ' + str(len(categories)) + ' question categories: ' + ','.join(categories))
- return categories
- def create_categories(namePrefix= 'testCategory-', number=3):
- resp = create_category(namePrefix + 'root-' + _random_string(3))
- root_id = _text(resp)['id']
- categories = []
- for i in range(number):
- child = create_category(namePrefix + 'child-' + _random_string(3))
- child_id = _text(child)['id']
- update_category(child_id, {
- 'parentId': root_id
- })
- categories.append(child_id)
- print ('created ' + str(len(categories)) + ' categories: ' + ','.join(categories))
- return categories
- def read_material_from_file(file_name):
- file = open('./demo-materials/' + file_name, 'r')
- file_lines = file.readlines()
- count = 0
- content = ''
- title = ''
- for line in file_lines:
- line = line.strip()
- if len(line) < 1:
- continue # whitespaces
- if count == 0:
- title = line
- else:
- content += line + '\n'
- count+=1
- return title, content
- def create_learning_materials(namePrefix='测试资料-', number=20, from_file=False):
- categories = create_categories()
- materials = []
- if from_file:
- files = os.listdir('./demo-materials/')
- for file_name in files:
- title, content = read_material_from_file(file_name)
- material = create_material({
- 'name': title,
- 'userId': randomly_pick_user(),
- 'type': 'ARTICLE',
- 'contents': content,
- })
- materialId = _text(material)['id']
- materials.append(materialId)
- random_category = categories[random.randint(0, len(categories)-1)]
- add_material_to_category(random_category, _text(material))
- create_comments(materialId)
- else:
- for i in range(number):
- user = randomly_pick_user()
- material = create_material({
- 'name': namePrefix + _random_string(5),
- 'userId': randomly_pick_user(),
- 'description': '好看的资料',
- 'type': 'ARTICLE',
- 'contents': '十年高考三年模拟-' + _random_string(20),
- 'links': [_random_string(3), _random_string(3), _random_string(3)]
- })
- materialId = _text(material)['id']
- materials.append(materialId)
- random_category = categories[random.randint(0, len(categories)-1)]
- add_material_to_category(random_category, _text(material))
- create_comments(materialId)
- print ('created ' + len(materials) + ' materials: ' + ','.join(materials))
- return materials
- def create_comments(materialId, namePrefix='[自动生成]', number=5):
- dummy_comments = ['我去,这也太酷了!', '我太爱学这个了,老牛了', '这太有意思了,收藏了', '好文,顶一个', '我踩']
- for i in range(number):
- comment = {
- 'userId': randomly_pick_user(),
- 'content': dummy_comments[i % len(dummy_comments)]
- }
- add_comment(materialId, comment)
- print('created ' + str(number) + ' comments to material: ' + materialId)
- def register_user(userData):
- url = 'http://localhost:8080/user/register'
- return requests.post(url, json=userData)
- if override:
- all_users = json.loads(get_all_users().text)
- for user in all_users:
- delete_user(user['userName'])
- print('deleted all users')
- if want_user:
- add_dummy_users()
- else:
- print('Skip creating users')
- if want_question:
- create_questions(from_file=True)
- else:
- print('Skip creating questions')
- if want_material:
- create_learning_materials(from_file=True)
- else:
- print('Skip creating materials')
|