def approve(package): """ Processing the request of creating a school """ user = package.get('user') params = package.get('params') user_id = user.get('id') school_id = PermissionHelper.get_user_school(user_id) if school_id == 0: return Response.error_response("You are not in a school") permission = PermissionHelper.get_permission(user_id, school_id) if not PermissionManager.check_permission(permission, ActionType.Approve): return Response.error_response('Access Denied') params = package.get('params') apply_id = int(params.get(ParamType.ApplyId)) apply = SchoolApplyHelper.get_apply_by_id(apply_id) apply_user_id = apply.get('userid') if apply is None: return Response.error_response('No Apply') status = params.get(ParamType.Approve) if status == 'true': status = 1 else: status = 2 SchoolApplyHelper.judge_apply(apply_id, user_id, status) if status == 1: PermissionHelper.user_join_school(apply_user_id, school_id) return Response.checked_response('Approve Successed')
def delete_theme(package): #pylint: disable-msg=too-many-return-statements """delete theme """ user = package.get('user') user_id = user.get('id') school_id = PermissionHelper.get_user_school(user_id) params = package.get('params') theme_id = int(params.get(ParamType.ThemeId)) theme = SubjectHelper.get_subject_with_schoolid(theme_id) if theme is None: return Response.error_response('No Subject') theme_schoolid = theme.get('school_id') private_permission = PermissionHelper.get_permission(user_id, school_id) public_permission = user['permission'] if private_permission > 4: #为超级用户 SubjectHelper.delete_subject(theme_id) return Response.checked_response('Deleted Success') if theme_schoolid == 0: if public_permission < 4: return Response.error_response('Access Denied') SubjectHelper.delete_subject(theme_id) return Response.checked_response('Deleted') if private_permission < 4: #非高级管理员 return Response.error_response('Access Denied') if school_id != theme_schoolid: #学校必须匹配 return Response.error_response('Acess Denied') SubjectHelper.delete_subject(theme_id) return Response.checked_response('Delete Success')
def add_school(user_id, schoolname, description, headmaster_id): """add school """ school = School(schoolname=schoolname, description=description, creator=user_id) school.save() PermissionHelper.set_permission(headmaster_id, school.id, 4) return school.id
def promote_user_in_school(testcase, permission): """promote a user in school """ response = testcase.client.get('/user/info/get', {'token': testcase.token}) response = analyse_response(response) data = response.get('data') user_id = data.get('user').get('id') school = PermissionHelper.get_user_school(user_id) PermissionHelper.set_permission(user_id, school, permission)
def add_user_to_school(testcase, schoolname): """add a user to school """ school = SchoolHelper.get_school_by_name(schoolname) school_id = school.get('id') response = testcase.client.get('/user/info/get', {'token': testcase.token}) response = analyse_response(response) data = response.get('data') user_id = data.get('user').get('id') PermissionHelper.user_join_school(user_id, school_id)
def get_school_by_name(school_name): """get schoolname """ schools = School.objects.filter(schoolname=school_name) if schools.exists(): school = schools.last() headmaster_id = PermissionHelper.get_school_headmaster(school.id) return { 'id': school.id, 'schoolname': school.schoolname, 'description': school.description, 'headmaster': UserHelper.get_name_by_id(headmaster_id), 'population': PermissionHelper.get_school_population(school.id) } return None
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 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 modify_theme(package): #pylint: disable-msg=too-many-return-statements """modify theme """ user = package.get('user') user_id = user.get('id') school_id = PermissionHelper.get_user_school(user_id) params = package.get('params') theme_id = int(params.get(ParamType.ThemeId)) title = params.get(ParamType.ThemeNameWithDefault) description = params.get(ParamType.ThemeDescriptionWithDefault) deadline = params.get(ParamType.ThemeDeadlineWithDefault) theme = SubjectHelper.get_subject_with_schoolid(theme_id) if theme is None: return Response.error_response('No Subject') theme_schoolid = theme.get('school_id') private_permission = PermissionHelper.get_permission(user_id, school_id) public_permission = user['permission'] if private_permission > 4: #为超级用户 SubjectHelper.modify_subject(theme_id, title, description, deadline) return Response.checked_response('Modified') if theme_schoolid == 0: if public_permission < 4: return Response.error_response('Access Denied') SubjectHelper.modify_subject(theme_id, title, description, deadline) return Response.checked_response('Modified') if private_permission < 4: #非高级管理员 return Response.error_response('Access Denied') if school_id != theme_schoolid: #学校必须匹配 return Response.error_response('Acess Denied') SubjectHelper.modify_subject(theme_id, title, description, deadline) return Response.checked_response('Modify 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 check_session(package): """process the request of check session """ user = package.get('user') user = UserHelper.user_filter(user) if user is None: return Response.success_response({'user': None}) user_id = user.get('id') permission_public = user.get('permission') del user['permission'] school_id = PermissionHelper.get_user_school(user_id) if school_id == 0: school_name = 'public area' if permission_public > 4: permission_private = permission_public else: permission_private = -1 else: school = SchoolHelper.get_school(school_id) if school is None: school_name = '-' else: school_name = school.get('schoolname') permission_private = PermissionHelper.get_permission( user_id, school_id) school = { 'id': school_id, 'name': school_name, } ret_user = { 'username': user.get('username'), 'school': school, 'permission_private': permission_private, 'permission_public': permission_public } return Response.success_response({'user': ret_user})
def create_theme(package): #pylint: disable-msg=too-many-return-statements """create a theme """ user = package.get('user') params = package.get('params') target_schoolid = params.get(ParamType.SchoolIdWithDefault) name = params.get(ParamType.ThemeName) msg = params.get(ParamType.ThemeDescription) deadline = params.get(ParamType.ThemeDeadline) userid = user.get('id') school_id = PermissionHelper.get_user_school(userid) private_permission = PermissionHelper.get_permission(userid, school_id) public_permission = user['permission'] if public_permission < 2 and private_permission < 2: return Response.error_response('Access Denied') if public_permission > 1 and private_permission > 1: #如果这是一个双重管理员 if target_schoolid is None: SubjectHelper.add_subject(0, name, msg, deadline) return Response.checked_response('Create Successful') target_schoolid = (int)(target_schoolid) SubjectHelper.add_subject(target_schoolid, name, msg, deadline) return Response.checked_response('Create Successful') if public_permission > 1: #如果这只是一个在野管理员 if school_id is not None: return Response.error_response('Access Denied') SubjectHelper.add_subject(0, name, msg, deadline) return Response.checked_response('Create Successful') if school_id is None: #此时必须一个schoolid return Response.error_response('Invalid School Id') if school_id != target_schoolid: return Response.error_response('Not the Same School') SubjectHelper.add_subject(school_id, name, msg, deadline) return Response.checked_response('Create Successful')
def get_school(school_id): """get school """ if school_id == 0: return { 'id': 0, 'schoolname': '-', 'description': '-', 'headmaster': '-', 'population': -1 } schools = School.objects.filter(id=school_id) headmaster_id = PermissionHelper.get_school_headmaster(school_id) if schools.exists(): school = schools.last() return { 'id': school_id, 'schoolname': school.schoolname, 'description': school.description, 'headmaster': UserHelper.get_name_by_id(headmaster_id), 'population': PermissionHelper.get_school_population(school_id) } return None
def get_apply_list(package): # pylint: disable-msg=too-many-return-statements """ Processing the request of getting apply list """ user = package.get('user') if user is None: return Response.error_response('No User') user_id = user.get('id') # school_id = PermissionHelper.get_user_school(user_id) params = package.get('params') list_type = params.get(ParamType.ApplyListType) page_num = params.get(ParamType.Page) target_schoolid = int(params.get(ParamType.SchoolId)) if target_schoolid == 0: return Response.error_response('Invalid SchoolId') permission = PermissionHelper.get_permission(user_id, target_schoolid) if not PermissionManager.check_permission(permission, ActionType.GetApplyList): return Response.error_response('Access Denied') if list_type is None: list_type = 0 list_type = int(list_type) if page_num is None: page_num = 1 page_num = int(page_num) if list_type not in [0, 1, 2]: return Response.error_response('Invalid list type') if page_num < 1: return Response.error_response('Invalid page number') apply_list = SchoolApplyHelper.get_applies(target_schoolid, list_type, page_num) school = SchoolHelper.get_school(target_schoolid) if school is None: return Response.error_response('No School') ret = { 'tot_count': SchoolApplyHelper.get_applies_count(target_schoolid, list_type), 'now_count': len(apply_list), 'apply_list': apply_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 modify_info(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 """Process the request of modyfying user's info """ user = package.get('user') if user is None: return Response.error_response('User Not Logged In') user_id = user.get('id') params = package.get('params') username = params.get(ParamType.UsernameWithDefault) realname = params.get(ParamType.RealnameForModify) motto = params.get(ParamType.MottoForModify) modify_private_permission = params.get( ParamType.PermissionPrivateForModify) modify_public_permission = params.get(ParamType.PermissionPublicForModify) if modify_private_permission is not None: modify_private_permission = int(modify_private_permission) if modify_public_permission is not None: modify_public_permission = int(modify_public_permission) if username is None: #修改本人信息 if modify_private_permission is not None: #不能修改个人权限 return Response.error_response( 'Access Denied: Can\'t Modify Your Permission') if modify_public_permission is not None: return Response.error_response( 'Access Denied: Can\'t Modify Your Permission ') UserHelper.modify_user(user_id, { 'realname': realname, 'motto': motto, }) return Response.checked_response('Modify Success') schoolid = PermissionHelper.get_user_school(user_id) private_permission = PermissionHelper.get_permission(user_id, schoolid) public_permission = user.get('permission') if public_permission <= 1 and private_permission <= 1: #如果是屌丝 return Response.error_response('Access Denied') if modify_private_permission == 4: return Response.error_response('Can\'t Set Someone to Headmaster') #现在修改人员有一个权限 >= 2 target_user = UserHelper.get_user_by_username(username) target_userid = target_user.get('id') target_schoolid = PermissionHelper.get_user_school(target_userid) target_public_permission = target_user.get('permission') target_private_permission = PermissionHelper.get_permission( target_userid, target_schoolid) if target_private_permission == 4 and modify_private_permission is not None: #如果更改人是校长 return Response.error_response('Cannot Modify Headmaster') if modify_private_permission is not None: if modify_private_permission >= private_permission: #不能越界 return Response.error_response( 'Access Denied: Cannot Promote Someone to Superior') if modify_private_permission < 0: #不能直接退学 return Response.error_response( 'Access Denied: Cannot Tuixue Student Here') if modify_public_permission is not None: if modify_public_permission >= public_permission: #不能越界 return Response.error_response( 'Access Denied: Cannot Promote Someone to Superior') if public_permission > 4: #现在是超级用户,可以随意修改 if target_public_permission >= public_permission: #超级用户也不能修改root权限 return Response.error_response( 'Access Denied: Can\'t modify your superior') if target_private_permission == 4: if modify_private_permission is not None: return Response.error_response( 'Modify Denied: Cannot Demote or Promote Headmaster Here') if target_schoolid == 0 and modify_private_permission is not None: return Response.error_response( 'Access Denied: Cannot Modify Schoolless User\'s private permission' ) UserHelper.modify_user( target_userid, { 'permission': modify_public_permission, 'realname': realname, 'motto': motto }) if modify_private_permission is not None: PermissionHelper.set_permission(target_userid, target_schoolid, modify_private_permission) return Response.checked_response('Modify Success') #之后都是管理员 这时候的权限 < 8 if realname is not None: return Response.error_response( 'Access Denied: Cannot Modify User Realname') if motto is not None: return Response.error_response( 'Access Denied: Cannot Modify User Motto') if schoolid == 0 and private_permission <= 1: #如果是在野管理员,在学校是屌丝, 则只能修改在野权限 if target_public_permission >= public_permission: #不能改领导权限 或者 同事s return Response.error_response( 'Access Denied: Can\'t modify your superior') if modify_private_permission is not None and schoolid == 0: #在野管理员不能修改学校权限 return Response.error_response( 'Access Denied: Not The Same School') if modify_public_permission is not None: #只可修改在野权限 UserHelper.modify_user(target_userid, {'permission': modify_public_permission}) return Response.checked_response('Modify Success') if modify_private_permission is not None and modify_public_permission is not None: if private_permission < 2 or public_permission < 2: return Response.error_response('Access Denied: Permission Error') if target_private_permission >= private_permission: return Response.error_response( 'Access Denied: Cannot Modify Your Superior') if target_public_permission >= public_permission: return Response.error_response( 'Access Denied: Cannot Modify Your Superior') UserHelper.modify_user(target_userid, {'permission': modify_public_permission}) if modify_private_permission is not None: PermissionHelper.set_permission(target_userid, target_schoolid, modify_private_permission) return Response.checked_response('Modify Success') #现在完全是在野屌丝 if target_private_permission >= private_permission: #不能该领导权限 或者 同事 return Response.error_response( 'Access Denied: Can\'t modify your superior') #现在是有学校的管理员 if target_schoolid != schoolid: #不是一个学校 return Response.error_response('Access Denied: Not The Same School') if modify_public_permission is not None: #不能改变在野权限 return Response.error_response( 'Access Denied: Can\'t modify public permission') if modify_private_permission is not None: PermissionHelper.set_permission(target_userid, target_schoolid, modify_private_permission) return Response.checked_response('Modify Success')
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 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 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)