def like(package): """process the request of like program """ user = package.get('user') params = package.get('params') prog_id = (int)(params.get(ParamType.ProgramId)) program = ProgramHelper.get_program(prog_id) if program is None: return Response.error_response('No Program') if program.get('status') != 5: return Response.error_response('Program not valid') user_id = user.get('id') if ProgramLikeHelper.check_like(user_id, prog_id): return Response.checked_response('User liked before') ProgramLikeHelper.add_like(user_id, prog_id) like_count = ProgramLikeHelper.count_like(prog_id) ProgramHelper.set_likes(prog_id, like_count) # program = ProgramHelper.get_program(prog_id) # return Response.checked_response(str(program.get('likes'))) return Response.checked_response('Like Successful')
def test_0002(self): """this is a test of like """ school = SchoolHelper.get_school_by_name('测试大学') school_id = school.get('id') theme = SubjectHelper.get_subject_by_name('计算机科学与技术') theme_id = theme.get('id') response = self.client.post( '/program/user/submit', { 'token': self.token, 'code_name': 'TestProgram', 'code_content': 'print("Hello World")', 'code_readme': 'Hello World', 'school_id': school_id, 'theme_id': theme_id }) data = analyse_response(response) self.assertEqual(data.get('status'), 1) program = ProgramHelper.get_program_by_name('TestProgram') code_id = program.get('id') ProgramHelper.upload(code_id) response = self.client.post('/program/user/like', { 'token': self.token, 'code_id': code_id }) data = analyse_response(response) self.assertEqual(data.get('status'), 1)
def get_list(package): """get theme list """ user = package.get('user') params = package.get('params') target_schoolid = int(params.get(ParamType.SchoolId)) page = params.get(ParamType.Page) if page is None: page = 1 page = int(page) user_id = user.get('id') school_id = PermissionHelper.get_user_school(user_id) public_permission = user.get('permission') if target_schoolid != 0: if target_schoolid != school_id and public_permission < 8: return Response.error_response('Access Denied') theme_list = SubjectHelper.get_subjects(target_schoolid, 0, page) for theme in theme_list: theme.update({ 'count': ProgramHelper.get_subject_programs_count(theme.get('id')) }) ret = { 'tot_count': SubjectHelper.get_subject_count(target_schoolid, 0), 'now_count': len(theme_list), 'theme_list': theme_list } return Response.success_response(ret)
def submit(package): """process the request of submitting program """ user = package.get('user') params = package.get('params') program_name = params.get(ParamType.ProgramName) program_code = params.get(ParamType.ProgramCode) program_doc = params.get(ParamType.ProgramDoc) program_school = int(params.get(ParamType.SchoolId)) program_subject = int(params.get(ParamType.ThemeId)) school = PermissionHelper.get_user_school(user['id']) if program_school != 0: if school != program_school: return Response.error_response('Access Denied') ProgramHelper.add_program(user['id'], program_name, program_code, program_doc, program_school, program_subject) return Response.checked_response('Submit Success')
def test_0001(self): """this is a test for admin download """ code_id = ProgramHelper.get_program_by_name('测试代码').get('id') response = self.client.get('/program/admin/download', { 'token': self.token, 'code_id': code_id }) data = analyse_response(response) self.assertEqual(data.get('msg'), 'Success')
def download(package): """process the request of downloading """ user = package.get('user') user_id = user.get('id') params = package.get('params') program_id = (int)(params.get(ParamType.ProgramId)) program = ProgramHelper.get_program(program_id) if program is None: return Response.error_response('No Program') prog_schoolid = program.get('schoolid') prog_status = program.get('status') if prog_status not in [0, 1, 2, 3]: return Response.error_response('Status not Valid') school_id = PermissionHelper.get_user_school(user_id) permission = PermissionHelper.get_permission(user_id, school_id) if permission > 4: if program.get('status') == 0: ProgramHelper.judging(program_id) info = {'content': program['code'], 'readme': program['doc']} return Response.success_response({'code': info}) if prog_schoolid == 0: if user.get('permission') < 2: return Response.error_response('Access Denied') if school_id != prog_schoolid or permission < 2: return Response.error_response('Access Denied') if program.get('status') == 0: ProgramHelper.judging(program_id) info = {'content': program['code'], 'readme': program['doc']} return Response.success_response({'code': info})
def download(package): """user download program """ user = package.get('user') params = package.get('params') prog_id = (int)(params.get(ParamType.ProgramId)) user_id = user.get('id') program = ProgramHelper.get_program(prog_id) if program is None: return Response.error_response('No Program') if not DownloadLogHelper.check_download(user_id, prog_id): DownloadLogHelper.add_downloadlog(user_id, prog_id) log_count = DownloadLogHelper.count_downloadlog(prog_id) ProgramHelper.set_downloads(prog_id, log_count) info = {'content': program.get('code'), 'readme': program.get('doc')} # program = ProgramHelper.get_program(prog_id) # return Response.checked_response(str(program.get('downloads'))) return Response.success_response({'code': info})
def test0002(self): """this is a test for admin status """ code_id = ProgramHelper.get_program_by_name('测试代码').get('id') source = 0 target = 1 response = self.client.post( '/program/admin/status', { 'token': self.token, 'code_id': code_id, 'source': source, 'target': target }) data = analyse_response(response) self.assertEqual(data.get('msg'), 'Status Changed Successful')
def test0007(self): """test for some errors """ code_id = ProgramHelper.get_program_by_name('测试代码').get('id') source = 0 target = 2 response = self.client.post( '/program/admin/status', { 'token': self.token, 'code_id': code_id, 'source': source, 'target': target }) data = analyse_response(response) self.assertEqual(data.get('status'), 0)
def test0004(self): """this a test for admin status """ Initialization.disconnect(self) self.token, self.ip_addr = Initialization.start_session(self) Initialization.register(self, 'testadmin', 'Test666', '11011011011') Initialization.login(self, 'testadmin', 'Test666') Initialization.promote_user(self, 4) code_id = ProgramHelper.get_program_by_name('在野代码').get('id') source = 0 target = 1 response = self.client.post( '/program/admin/status', { 'token': self.token, 'code_id': code_id, 'source': source, 'target': target }) data = analyse_response(response) self.assertEqual(data.get('msg'), 'Status Changed Successful')
def get_info(package): """process the request of getting user's info """ params = package.get('params') username = params.get(ParamType.UsernameWithDefault) if username is None: user = package.get('user') else: user = UserHelper.get_user_by_username(username) if user is None: return Response.error_response("No User") user = UserHelper.user_filter(user) permission_public = user.get('permission') user_id = user.get('id') school_id = PermissionHelper.get_user_school(user_id) if school_id == 0: if permission_public >= 8: permission_private = permission_public else: permission_private = -1 schoolname = 'public area' else: permission_private = PermissionHelper.get_permission( user_id, school_id) school = SchoolHelper.get_school(school_id) if school is None: schoolname = '-' else: schoolname = school.get('schoolname') download = ProgramHelper.count_user_downloadlog(user_id) del user['permission'] user.update({ 'school_name': schoolname, 'permission_public': permission_public, 'permission_private': permission_private, 'download': download }) return Response.success_response({'user': user})
def get_program_list(package): #pylint: disable-msg=too-many-locals #pylint: disable-msg=too-many-return-statements #pylint: disable-msg=too-many-branches #pylint: disable-msg=too-many-statements """All list """ params = package.get('params') user = package.get('user') mine = params.get(ParamType.Mine) schoolid = params.get(ParamType.SchoolIdWithDefault) status_up = params.get(ParamType.StatusUp) status_low = params.get(ParamType.StatusDown) subjectid = params.get(ParamType.ThemeIdWithDefault) listtype = params.get(ParamType.Listype) page = params.get(ParamType.Page) if schoolid is not None: schoolid = int(schoolid) if subjectid is not None: subjectid = int(subjectid) if status_up is not None: status_up = int(status_up) else: status_up = 6 if status_low is not None: status_low = int(status_low) else: status_low = -3 if listtype is not None: listtype = int(listtype) else: listtype = 0 if page is None: page = 1 else: page = int(page) if status_up not in range(-4, 7): return Response.error_response('Illegel Status Upper Limit') if status_low not in range(-4, 7): return Response.error_response('Illegal Status Lower Limit') if mine == 'true': user_id = user.get('id') progs_list = ProgramHelper.get_user_programs(user_id, page, listtype) if len(progs_list) == 0: data = { 'tot_count' : 0, 'now_count' : 0, 'code_list' : [] } return Response.success_response(data) username = user.get('username') codelist = [] for prog in progs_list: prog_id = prog.get('id') liked = ProgramLikeHelper.check_like(user_id, prog_id) downloaded = DownloadLogHelper.check_download(user_id, prog_id) schoolid = PermissionHelper.get_user_school(user_id) schoolname = SchoolHelper.get_school_name(schoolid) info = ProgramHelper.prog_filter( prog, username, downloaded, liked, schoolname ) codelist.append(info) data = { 'tot_count' : ProgramHelper.get_user_programs_count(user_id), 'now_count' : len(progs_list), 'code_list' : codelist } return Response.success_response(data) if schoolid is None: return Response.error_response('Invalid School') user_id = user.get('id') progs_list = ProgramHelper.get_programs_school( status_up, status_low, schoolid, subjectid, page, listtype ) if len(progs_list) == 0: data = { 'tot_count' : 0, 'now_count' : 0, 'code_list' : [] } return Response.success_response(data) username = user.get('username') codelist = [] for prog in progs_list: prog_id = prog.get('id') liked = ProgramLikeHelper.check_like(user_id, prog_id) downloaded = DownloadLogHelper.check_download(user_id, prog_id) schoolid = PermissionHelper.get_user_school(user_id) schoolname = SchoolHelper.get_school_name(schoolid) info = ProgramHelper.prog_filter( prog, username, downloaded, liked, schoolname ) codelist.append(info) data = { 'tot_count' : ProgramHelper.get_programs_school_count( status_up, status_low, schoolid, subjectid ), 'now_count' : len(progs_list), 'code_list' : codelist } return Response.success_response(data)
def change_status(package): #pylint: disable-msg=too-many-return-statements #pylint: disable-msg=too-many-branches """proecess the request of change status """ user = package.get('user') params = package.get('params') code_id = int(params.get(ParamType.ProgramId)) source = int(params.get(ParamType.SourceStatus)) target = int(params.get(ParamType.TargetStatus)) check = (source, target) program = ProgramHelper.get_program(code_id) program_schoolid = program.get('schoolid') user_id = user.get('id') school_id = PermissionHelper.get_user_school(user_id) permission = PermissionHelper.get_permission(user_id, school_id) public_permission = user.get('permission') if permission > 4: if check not in [(0, 1), (1, 2), (1, -1), (2, 3), (3, 4), (4, 5)]: return Response.error_response('Cannot Change Status') if ProgramHelper.change_status(code_id, source, target) is False: return Response.error_response('Source Status Wrong') return Response.checked_response('Status Changed Successful') if program_schoolid == 0: if public_permission < 2: return Response.error_response('Access Denied') #如果是 在野审查员 不能上传 if public_permission < 4: if check not in [(0, 1), (1, 2), (1, -1), (2, 3)]: return Response.error_response('Can\'t change status') if ProgramHelper.change_status(code_id, source, target) is False: return Response.error_response('Source Status Wrong') if check == (4, 5): ProgramHelper.upload(code_id) if check == (1, 2): ProgramHelper.judge_program(code_id, 2, user_id) return Response.checked_response('Status Changed Successful') #如果是 在野头目 则可以进行上传 if check not in [(0, 1), (1, 2), (1, -1), (2, 3), (3, 4), (4, 5)]: return Response.error_response('Cannot Change Status') if ProgramHelper.change_status(code_id, source, target) is False: return Response.error_response('Source Status Wrong') if check == (4, 5): ProgramHelper.upload(code_id) if check == (1, 2): ProgramHelper.judge_program(code_id, 2, user_id) return Response.checked_response('Status Changed Successful') if school_id != program_schoolid: return Response.error_response('Access Denied: Not the same School') if permission < 2: return Response.error_response('Access Denied') if permission < 4: #如果只是普通管理员 if check not in [(0, 1), (1, 2), (1, -1), (2, 3)]: return Response.error_response('Can\'t change status') if ProgramHelper.change_status(code_id, source, target) is False: return Response.error_response('Source Status Wrong') if check == (4, 5): ProgramHelper.upload(code_id) if check == (1, 2): ProgramHelper.judge_program(code_id, 2, user_id) return Response.checked_response('Status Changed Successful') if check not in [(0, 1), (1, 2), (1, -1), (2, 3), (3, 4), (4, 5)]: return Response.error_response('Cannot Change Status') if ProgramHelper.change_status(code_id, source, target) is False: return Response.error_response('Source Status Wrong') if check == (4, 5): ProgramHelper.upload(code_id) if check == (1, 2): ProgramHelper.judge_program(code_id, 2, user_id) return Response.checked_response('Status Changed Successful')
def getlist(package): #pylint: disable-msg=too-many-locals """process the request of getting user's info """ params = package.get('params') show_invalid = params.get(ParamType.ShowInvalid) == 'true' manager_first = params.get(ParamType.ManagerFirst) == 'true' school_id = int(params.get(ParamType.SchoolId)) page = params.get(ParamType.Page) if page is None: page = 1 page = int(page) if school_id == 0: user_list = UserHelper.user_list(page, show_invalid, manager_first) ret_list = [] if len(user_list) == 0: data = { 'tot_count': UserHelper.user_count(show_invalid), 'now_count': 0, 'user_list': [] } return Response.success_response(data) for user in user_list: download = ProgramHelper.count_user_downloadlog(user.get('id')) ret_list.append({ 'username': user.get('username'), 'motto': user.get('motto'), 'permission': user.get('permission'), 'download': download }) data = { 'tot_count': UserHelper.user_count(show_invalid), 'now_count': len(ret_list), 'user_list': ret_list } return Response.success_response(data) buf_userlist = UserHelper.get_all(show_invalid, manager_first) userlist = [] for user in buf_userlist: user_id = user.get('id') school = PermissionHelper.get_user_school(user_id) if school_id != school: continue download = ProgramHelper.count_user_downloadlog(user.get('id')) permission_private = PermissionHelper.get_permission(user_id, school) print('permission private', permission_private) userlist.append({ 'username': user['username'], 'motto': user['motto'], 'permission': permission_private, 'download': download }) if len(userlist) < (page - 1) * 20: data = {'tot_count': len(userlist), 'now_count': 0, 'user_list': []} return Response.success_response(data) pagelist = userlist[(page - 1) * 20:page * 20] data = { 'tot_count': len(userlist), 'now_count': len(pagelist), 'user_list': userlist, } return Response.success_response(data)